package com.nerdiex.englishconversation;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.nerdiex.AppController;
import com.nerdiex.R;
import com.nerdiex.dictionary.lib.AlertDialogManager;
import com.nerdiex.dictionary.model.ConversationModel;
import com.nerdiex.englishconversation.adapter.ConversationAdapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.util.Pair;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class EnglishConversationFragment extends Fragment {
    private static final String TAG = EnglishConversationActivity.class.getSimpleName();
    private RecyclerView list;
    ProgressBar pBar;
    public static int FRAGMENT_ID;
    CharSequence mTitle;
    AlertDialogManager alert;
    public static final String baseURL = "http://androiderblog.com/nerdiex/dictionary/";
    public static final String URL_CONVERSATION = baseURL + "?type=conversation";
    public static final String ID = "id";
    public static final String IMAGE = "image";
    public static final String TITLE = "title";
    public static final String SOUND = "sound";
    ArrayList<ConversationModel> conversationList;
    private ConversationAdapter adapter;

    private Context context;
    private Activity activity;
    @SuppressLint("NewApi")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle icicle) {

        View view = inflater.inflate(R.layout.conversation_main,container,false);
        context = container.getContext();
        list = (RecyclerView) view.findViewById(R.id.conversation_list);
        pBar = (ProgressBar) view.findViewById(R.id.progressBar);
        conversationList = new ArrayList<ConversationModel>();

        requestConversationJSONArray();
        return view;
    }

    private void requestConversationJSONArray() {

        pBar.setVisibility(View.VISIBLE);
        JsonArrayRequest jsonArrayReq = new JsonArrayRequest(
                URL_CONVERSATION, new Response.Listener<JSONArray>() {

            @Override
            public void onResponse(JSONArray response) {
                //Log.d(TAG, response.toString());

                if (response != null) {
                    parseJSONArray(response);
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                // sembunyikan  progress dialog
                pBar.setVisibility(View.GONE);
                alert = new AlertDialogManager();
                String header = "Koneksi Gagal!";
                alert.showAlertDialog(context, header, getString(R.string.no_connection), false);

            }
        });
        pBar.setVisibility(View.GONE);
        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonArrayReq);

    }

    private void parseJSONArray(JSONArray response) {
        try {
            // Parsing json object
            Log.d(TAG, response.toString());

            for (int i = 0; i < response.length(); i++) {
                JSONObject c = response.getJSONObject(i);
                ConversationModel item = new ConversationModel();
                item.setId(c.getString(ID));
                item.setTitle(c.getString(TITLE));
                item.setImage(baseURL + c.getString(IMAGE));
                item.setSound(baseURL + c.getString(SOUND));
                conversationList.add(item);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            Toast.makeText(context,
                    "Error: " + e.getMessage(),
                    Toast.LENGTH_LONG).show();
            pBar.setVisibility(View.GONE);
        }
        showListView();

    }

    private void showListView() {
        adapter = new ConversationAdapter(getActivity(), conversationList);
        list.setAdapter(adapter);
    }
    @Override
    public void onAttach(Activity activity){
        super.onAttach(activity);
        activity = activity;
    }

    public void onSectionAttached(int number) {
        switch (number) {
            case 1:
                mTitle = getString(R.string.englishconversation);
                break;
            case 2:
                mTitle = getString(R.string.appointment);
                break;
            case 3:
                mTitle = getString(R.string.breakfast);
                break;
            case 4:
                mTitle = getString(R.string.buying);
                break;
            case 5:
                mTitle = getString(R.string.trip);
                break;
            case 6:
                mTitle = getString(R.string.toilet);
                break;
            case 7:
                mTitle = getString(R.string.shop);
                break;
            case 8:
                mTitle = getString(R.string.phone);
                break;
            case 9:
                mTitle = getString(R.string.order);
                break;
            case 10:
                mTitle = getString(R.string.movie);
                break;
            case 11:
                mTitle = getString(R.string.greetings);
                break;
        }

    }
}