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.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hikapro.backpack.model.entities.Set;
import com.hikapro.backpack.presenter.Presenter;
import com.hikapro.backpack.presenter.adapters.helper.sets.OnStartDragListener;

public class SetListFragment extends Fragment implements com.hikapro.backpack.view.View.SetList,
        OnStartDragListener {

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


    public SetListFragment() {
        // Required empty public constructor
    }
    public static SetListFragment construct() {
        SetListFragment ret = new SetListFragment();
        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.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) {
        Log.i(this.toString(), "onCreateView");
        presenter.setView(this);
        View view = presenter.onCreateView(inflater, container, savedInstanceState);


        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 onPause() {
        super.onPause();
        Log.i(this.toString(), "onPause");
    }
    @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 void showItemList(Set set) {
        if (set.getActiveQty() == set.getPackedQty())
            activityCallback.startPackedListFragment(set);
        else
            activityCallback.startItemListFragment(set);
    }

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

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


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


    //GLM_add_resources_SetList
    @Override
    public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
        presenter.onStartDrag(viewHolder);
    }

    //GLM_add_resources_SetList
    @Override
    public OnStartDragListener getOnStartDragListener() {
        return this;
    }
}
