package com.hikapro.backpack.view.fragments;


import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SearchView;
import android.widget.Toast;

import com.hikapro.backpack.R;
import com.hikapro.backpack.model.Model;
import com.hikapro.backpack.model.entities.Item;
import com.hikapro.backpack.model.entities.Set;
import com.hikapro.backpack.presenter.Presenter;


public class ItemListFragment extends Fragment implements com.hikapro.backpack.view.View.ItemList,
        SearchView.OnQueryTextListener {

    protected static final String BUNDLE_SET_KEY = "BUNDLE_SET_KEY";
    private com.hikapro.backpack.view.View.ActivityCallback activityCallback;
    private Presenter.ItemList  presenter;
    private boolean hz;

    public ItemListFragment() {
        // Required empty public constructor
    }

    private static ItemListFragment construct() {
        return new ItemListFragment();
    }

    public static ItemListFragment newFromSet(Set set) {
        ItemListFragment ret = ItemListFragment.construct();
        Bundle args = new Bundle();
        args.putSerializable(BUNDLE_SET_KEY, set);
        ret.setArguments(args);
        return ret;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        inflater.inflate(R.menu.menu_main, menu);
        super.onCreateOptionsMenu(menu, inflater);
        final MenuItem item = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) item.getActionView();
        searchView.setOnQueryTextListener(this);
        Log.i(this.getClass().getName(), " onCreateOptionsMenu");
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        if (hz)
            presenter.filter(newText);
        if (!hz)
            hz = true;
        return hz;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        boolean ret;

        switch (item.getItemId()) {

            case R.id.action_share :
                activityCallback.startShareFragment(getSet().getId());
                ret = true;
                break;
            case R.id.action_unpack_my_bag :
                presenter.unpack(getSet().getId());
                ret = true;
                break;
            case R.id.action_restore_to_default :
                presenter.restore(getSet().getId());
                ret = true;
                break;
            case R.id.action_add :
                activityCallback.startAddFragment(getSet());
                ret = true;
                break;
            default:
                ret = super.onOptionsItemSelected(item);
        }
        return ret;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            activityCallback = (com.hikapro.backpack.view.View.ActivityCallback) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + " must implement activityCallback");
        }
    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            activityCallback = (com.hikapro.backpack.view.View.ActivityCallback) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement activityCallback");
        }
        Log.i(this.getClass().getName(), " onAttach");
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        Log.i(this.getClass().getName(), " onCreate");
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        presenter.setView(this);
        View view = presenter.onCreateView(inflater, container, savedInstanceState);
        Log.i(this.getClass().getName(), " onCreateView");
        return view;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i(this.getClass().getName(), " onActivityCreated");
    }
    @Override
    public void onStart() {
        super.onStart();
        Log.i(this.getClass().getName(), " onStart");
    }
    @Override
    public void onResume() {
        super.onResume();
        presenter.onResume();
        Log.i(this.getClass().getName(), " onResume");
    }
    @Override
    public void onStop() {
        super.onStop();
        Log.i(this.getClass().getName(), " onStop");
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        presenter.onDestroy(true); // TODO isChangingConfigurations
        hz = false;
        Log.i(this.getClass().getName(), " onDestroyView");
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onDestroy(false); // TODO isChangingConfigurations
        Log.i(this.getClass().getName(), " onDestroy");
    }
    @Override
    public void onDetach() {
        super.onDetach();
        Log.i(this.getClass().getName(), " onDetach");
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        presenter.onSaveInstanceState(outState);
        Log.i(this.getClass().getName(), " onSaveInstanceState");
    }

    @Override
    public void showItemDetail(int setId, Model.Item baseModel, int position) {
        activityCallback.startItemDetailFragment(setId, baseModel, position);
    }

    @Override
    public void showPackedItems(Set set) {
        activityCallback.startPackedListFragment(set);
    }

    @Override
    public void setPresenter(Presenter.ItemList presenter) {
        this.presenter = presenter;
    }

    @Override
    public Context getAppContext() {
        return this.getActivity().getApplicationContext();
    }

    @Override
    public Context getActivityContext() {
        return this.getActivity();
    }

    public Set getSet() {
        return (Set) getArguments().getSerializable(BUNDLE_SET_KEY);
    }


}
