package com.hikapro.backpack.presenter;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.hikapro.backpack.R;
import com.hikapro.backpack.model.entities.Set;
import com.hikapro.backpack.presenter.adapters.PackedListAdapter;
import com.hikapro.backpack.presenter.adapters.helper.Util;
import com.hikapro.backpack.presenter.adapters.helper.items.DividerDecoration;
import com.hikapro.backpack.presenter.adapters.helper.items.DividerDecoration2;
import com.hikapro.backpack.presenter.adapters.helper.items.ItemSwipeCallback;
import com.hikapro.backpack.presenter.adapters.helper.items.StickyHeaderDecoration;

/**
 * Created by tariel on 13/05/16.
 */
public class PackedListPresenter extends ItemListPresenter {

    private TextView backToList;
    private Button unpackButton;


    public PackedListPresenter() {
        this.adapter = new PackedListAdapter(this);
        adapter.setHasStableIds(true);
        adapter.setCheckAll(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (savedInstanceState != null)
            set = (Set) savedInstanceState.getSerializable(BUNDLE_SET_LIST_KEY);
        else
            set = getView().getSet();
        android.view.View view = inflater.inflate(R.layout.fragment_packed_list, container, false);
        LinearLayoutManager llm = new LinearLayoutManager(getActivityContext());
        recycler = (RecyclerView) view.findViewById(R.id.items_recycler);
        recycler.setLayoutManager(llm);
        recycler.setAdapter(adapter);

        final StickyHeaderDecoration decoration = new StickyHeaderDecoration(adapter);
        recycler.addItemDecoration(decoration);
        //recycler.addItemDecoration(new DividerDecoration(getActivityContext()));
        recycler.addItemDecoration(new DividerDecoration2(getActivityContext(), R.drawable.divider, Util.dp2px(getAppContext(), 16)));
        adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                decoration.invalidateHeaders();
            }
        });
        recycler.setHasFixedSize(true);
        model.executeQuery();
        recycler.scrollToPosition(model.getLastVisiblePosition());
        backToList      = (TextView) view.findViewById(R.id.back_to_list);
        packedCount     = (TextView) view.findViewById(R.id.header_packed_count);
        unpackButton    = (Button) view.findViewById(R.id.unpack_button);

        setVisibility();
        unpackButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                model.unpackSet(set.getId());
            }
        });

        backToList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment = (Fragment) getView();
                if (fragment != null)
                    fragment.getFragmentManager().popBackStack();
            }
        });


        Activity activity = (Activity) getActivityContext();
        if (activity != null) {
            activity.invalidateOptionsMenu();
            ActionBar actionBar = activity.getActionBar();
            if (actionBar != null) {
                actionBar.show();
                actionBar.setTitle(set.getName());
                actionBar.setDisplayShowHomeEnabled(false);
                actionBar.setDisplayShowTitleEnabled(true);
                actionBar.setDisplayShowCustomEnabled(false);
                actionBar.setDisplayHomeAsUpEnabled(true);
            }
        }
        return view;
    }


    @Override
    public void setVisibility() {
        if (model.getPackedQty() == 0) {
            packedCount.setVisibility(View.GONE);
            unpackButton.setVisibility(View.GONE);
            backToList.setVisibility(View.VISIBLE);
        } else if (model.getPackedQty() == model.getActiveItemsCount()) {
            packedCount.setVisibility(View.GONE);
            unpackButton.setVisibility(View.VISIBLE);
            backToList.setVisibility(View.GONE);
        } else {
            packedCount.setVisibility(View.VISIBLE);
            unpackButton.setVisibility(View.GONE);
            backToList.setVisibility(View.VISIBLE);
        }
    }
}
