package com.hikapro.backpack.view.fragments;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.app.Fragment;
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 com.hikapro.backpack.R;
import com.hikapro.backpack.model.entities.Item;
import com.hikapro.backpack.model.entities.Set;
import com.hikapro.backpack.presenter.Presenter;

/**
 * A simple {@link Fragment} subclass.
 */
public class AddFragment extends Fragment implements com.hikapro.backpack.view.View.Add {

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

    public static AddFragment construct() {
        return new AddFragment();
    }

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


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

    // life cycle -->
    @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.toString(), " onAttach");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setHasOptionsMenu(true);
        Log.i(this.toString(), " onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        presenter.setView(this);
        View view = presenter.onCreateView(inflater, container, savedInstanceState);
        Log.i(this.toString(), " onCreateView");
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i(this.toString(), " onActivityCreated");
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.i(this.toString(), " onStart");
    }
    @Override
    public void onResume() {
        super.onResume();
        Log.i(this.toString(), " onResume");
    }
    @Override
    public void onStop() {
        super.onStop();
        Log.i(this.toString(), " onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        presenter.onDestroy(true); // TODO isChangingConfigurations
        Log.i(this.toString(), " onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onDestroy(false); // TODO isChangingConfigurations
        Log.i(this.toString(), " onDestroy");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.i(this.toString(), " onDetach");
    }

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

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

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

    @Override
    public void setNewItem(Item item) {

    }

    public Set getSet() {
        return (Set) getArguments().getSerializable(BUNDLE_SET_KEY);
    }
/*
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_add, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

*/
}
