package com.hikapro.backpack.view.fragments;


import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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
    }

    @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");
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
    }
    @Override
    public void onResume() {
        super.onResume();
    }
    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        presenter.onDestroy(true);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onDestroy(false);
    }

    @Override
    public void onDetach() {
        super.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);
    }

}
