package com.hikapro.backpack.presenter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
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.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;

import java.lang.ref.WeakReference;
import java.util.Collections;

import com.hikapro.backpack.R;
import com.hikapro.backpack.model.Model;
import com.hikapro.backpack.model.entities.Set;
import com.hikapro.backpack.presenter.adapters.SetListAdapter;
import com.hikapro.backpack.presenter.adapters.helper.sets.OnStartDragListener;
import com.hikapro.backpack.presenter.adapters.helper.sets.SimpleItemTouchHelperCallback;
import com.hikapro.backpack.view.View;
import com.hikapro.backpack.view.recycler.SetViewHolder;

/**
 * Created by tariel on 20/04/16.
 */
public class SetListPresenter implements Presenter.SetList {

    private WeakReference<View.SetList> view;
    private Model.Set                   model;
    private SetListAdapter              adapter;
    private ViewGroup                   progressBarContainer;
    private ProgressBar                 progressBar;
    private RecyclerView                setRecycler;


    //GLM_add_resources_SetList
    private OnStartDragListener mDragStartListener;
    private ItemTouchHelper mItemTouchHelper;

    public SetListPresenter() {
        this.adapter    = new SetListAdapter(this);
    }

    //GLM_add_resources_SetList
    @Override
    public void onItemDismiss(int position) {
        model.getSets().remove(position);
        adapter.notifyItemRemoved(position);
    }

    //GLM_add_resources_SetList
    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        Collections.swap(model.getSets(), fromPosition, toPosition);
        adapter.notifyItemMoved(fromPosition, toPosition);
        model.setsReorderNotify();
        return true;
    }

    @Override
    public void onDestroy(boolean isChangingConfiguration) {
        view = null;
        model.onDestroy(isChangingConfiguration);
        if ( !isChangingConfiguration ) {
            model = null;
        }
    }
    @Override
    public android.view.View onCreateView(LayoutInflater inflater, ViewGroup container,
                                          Bundle savedInstanceState) {
        android.view.View view = inflater.inflate(R.layout.fragment_set_list, container, false);

        progressBarContainer = (ViewGroup) view.findViewById(R.id.set_progress_container);
        progressBar = (ProgressBar) progressBarContainer.findViewById(R.id.set_progress);

        LinearLayoutManager llm = new LinearLayoutManager(getActivityContext());
        setRecycler = (RecyclerView) view.findViewById(R.id.set_recycler);
        setRecycler.setLayoutManager(llm);
        setRecycler.setAdapter(adapter);
        setRecycler.setItemAnimator(new DefaultItemAnimator());
        setRecycler.setHasFixedSize(true);
        model.executeQuery();
        setRecycler.scrollToPosition(model.getLastVisiblePosition());

        //GLM_add_resources_SetList
        ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(adapter);
        mItemTouchHelper = new ItemTouchHelper(callback);
        mItemTouchHelper.attachToRecyclerView(setRecycler);
        Activity activity = (Activity) getActivityContext();
        if (activity != null)
            activity.getActionBar().hide();

        return view;
    }

    @Override
    public void notifyDataSetChanged() {
        adapter.notifyDataSetChanged();
    }

    @Override
    public Context getAppContext() {
        try {
            return getView().getAppContext();
        } catch (NullPointerException e) {
            return null;
        }
    }
    @Override
    public Context getActivityContext() {
        try {
            return getView().getActivityContext();
        } catch (NullPointerException e) {
            return null;
        }
    }
    @Override
    public void setView(View.SetList view) {
        this.view = new WeakReference<>(view);
        this.mDragStartListener = getView().getOnStartDragListener();
    }
    @Override
    public void setModel(Model.Set model) {
        this.model = model;
    }

    @Override
    public Model.Set getModel() {
        return model;
    }

    private View.SetList getView() throws NullPointerException {
        if ( view != null )
            return view.get();
        else
            throw new NullPointerException("View is unavailable");
    }

    @Override
    public void startProgress() {
        setRecycler.setVisibility(android.view.View.GONE);
        progressBarContainer.setVisibility(android.view.View.VISIBLE);
    }

    @Override
    public void stopProgress() {
        setRecycler.setVisibility(android.view.View.VISIBLE);
        progressBarContainer.setVisibility(android.view.View.GONE);
    }

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

    public void showItemList(Set set) {
        if (getView() != null) {
            getView().showItemList(set);
            LinearLayoutManager llm = (LinearLayoutManager)setRecycler.getLayoutManager();
            model.setLastVisiblePosition(llm.findFirstVisibleItemPosition());
        }
        else
            Toast.makeText(getActivityContext(), "There is no view in presenter", Toast.LENGTH_SHORT).show();
    }

    public void onLongClick(SetViewHolder holder) {
        mDragStartListener.onStartDrag(holder);
    }
}
