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 com.hikapro.backpack.R;
import com.hikapro.backpack.model.entities.Item;
import com.hikapro.backpack.presenter.Presenter;

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

    private static final String BUNDLE_SET_KEY = "BUNDLE_SET_KEY";

    private Presenter.ItemDetail presenter;
    private com.hikapro.backpack.view.View.ActivityCallback activityCallback;


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

    protected static ItemDetailFragment construct() {
        return new ItemDetailFragment();
    }

    public static ItemDetailFragment newInstance(int setId) {
        ItemDetailFragment ret = ItemDetailFragment.construct();
        Bundle args = new Bundle();
        args.putInt(BUNDLE_SET_KEY, setId);
        ret.setArguments(args);
        return ret;
    }

    // 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);
        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);
        Log.i(this.toString(), " onDestroyView");
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onDestroy(false);
        Log.i(this.toString(), " onDestroy");
    }
    @Override
    public void onDetach() {
        super.onDetach();
        Log.i(this.toString(), " onDetach");
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i(this.toString(), " onSaveInstanceState");
    }

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

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

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

    @Override
    public int getSetId() {
        Bundle args = getArguments();
        int setId = args.getInt(BUNDLE_SET_KEY);
        return setId;
    }
}
