package kr.co.hanwha.hifive.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.HashMap;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.adapter.contract.MainHomeAssignedListContract;
import kr.co.hanwha.hifive.adapter.viewholder.MainHomeAssignedListViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.MainHomeTitleHeaderViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.MainHomeTodayHeaderViewHolder;
import kr.co.hanwha.hifive.data.model.TaskListData;
import kr.co.hanwha.hifive.data.model.TaskListViewHolderData;
import kr.co.hanwha.hifive.enums.TaskViewHolderType;
import kr.co.hanwha.hifive.screen.main.home.MainHomeContract;

/**
 * Created by sanghoonkwon on 2018. 1. 25..
 */

public class MainHomeAssignedListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements MainHomeAssignedListContract.Model, MainHomeAssignedListContract.View {
    private Context mContext;

    private final int ZERO = 0;

    private MainHomeContract.OnMainHomeListListener mOnMainHomeListListener;
    private ArrayList<TaskListViewHolderData> mListData;
    private HashMap<Integer, Boolean> mapAnim = new HashMap<>();

    public MainHomeAssignedListAdapter(@NonNull Context context,
                                       @NonNull MainHomeContract.OnMainHomeListListener onMainHomeListListener) {
        mContext = context;
        mOnMainHomeListListener = onMainHomeListListener;
    }

    /**************************************************
     * View
     **************************************************/
    @Override
    public void notifyDataAll() {
        this.notifyDataSetChanged();
    }

    @Override
    public void notifyDataRangeChanged(int positionStart, int itemCount) {
        this.notifyItemRangeChanged(positionStart, itemCount);
    }

    /**************************************************
     * Model
     **************************************************/
    @Override
    public void addItems(ArrayList<TaskListData> todayListData,
                         ArrayList<TaskListData> taskListData,
                         ArrayList<TaskListData> closedListData) {
        if (mListData == null) mListData = new ArrayList<>();
        else mListData.clear();

        mapAnim.clear();

        //오늘 할일
        mListData.add(new TaskListViewHolderData(TaskViewHolderType.TODAY_HEADER, mContext.getString(R.string.main_home_today), (todayListData != null ? todayListData.size() : 0)));
        if (todayListData != null) {
            for (int i = 0; i < todayListData.size(); i++) {
                mListData.add(new TaskListViewHolderData(TaskViewHolderType.TODAY_DATA, todayListData.get(i)));
            }
        }

        //진행중인 업무
        mListData.add(new TaskListViewHolderData(TaskViewHolderType.TASK_HEADER, mContext.getString(R.string.main_home_assigned_title), (taskListData != null ? taskListData.size() : 0)));
        if (taskListData != null) {
            for (int i = 0; i < taskListData.size(); i++) {
                mListData.add(new TaskListViewHolderData(TaskViewHolderType.TASK_DATA, taskListData.get(i)));
            }
        }

        //완료된 업무
        mListData.add(new TaskListViewHolderData(TaskViewHolderType.TASK_CLOSED_HEADER, mContext.getString(R.string.main_home_closed_title), (closedListData != null ? closedListData.size() : 0)));
        if (closedListData != null) {
            for (int i = 0; i < closedListData.size(); i++) {
                mListData.add(new TaskListViewHolderData(TaskViewHolderType.TASK_CLOSED_DATA, closedListData.get(i)));
            }
        }
    }

    /**************************************************
     * Adapter
     **************************************************/

    @Override
    public int getItemViewType(int position) {
        return mListData != null ? mListData.get(position).getType().getCode() : 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == TaskViewHolderType.TODAY_HEADER.getCode()) {
            return new MainHomeTodayHeaderViewHolder(parent);
        } else if (viewType == TaskViewHolderType.TASK_HEADER.getCode()
                || viewType == TaskViewHolderType.TASK_CLOSED_HEADER.getCode()) {
            return new MainHomeTitleHeaderViewHolder(parent);
        } else if (viewType == TaskViewHolderType.TODAY_DATA.getCode()
                || viewType == TaskViewHolderType.TASK_DATA.getCode()
                || viewType == TaskViewHolderType.TASK_CLOSED_DATA.getCode()) {
            return new MainHomeAssignedListViewHolder(parent, mOnMainHomeListListener);
        }

        throw new RuntimeException("############################################################\n" +
                "There is no type that matches the type " + viewType + " + make sure your using types correctly\n" +
                "############################################################");
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        TaskListViewHolderData data = mListData.get(position);

        if (getItemViewType(position) == TaskViewHolderType.TODAY_HEADER.getCode()) {
            MainHomeTodayHeaderViewHolder view = (MainHomeTodayHeaderViewHolder) holder;
            view.onBind(null, data.getIntValue());
        } else if (getItemViewType(position) == TaskViewHolderType.TASK_HEADER.getCode() ||
                getItemViewType(position) == TaskViewHolderType.TASK_CLOSED_HEADER.getCode()) {
            MainHomeTitleHeaderViewHolder view = (MainHomeTitleHeaderViewHolder) holder;
            view.onBind(data.getTitle(), data.getIntValue());
        } else if (getItemViewType(position) == TaskViewHolderType.TODAY_DATA.getCode() ||
                getItemViewType(position) == TaskViewHolderType.TASK_DATA.getCode() ||
                getItemViewType(position) == TaskViewHolderType.TASK_CLOSED_DATA.getCode()) {
            MainHomeAssignedListViewHolder view = (MainHomeAssignedListViewHolder) holder;
            view.onBind(data.getData(), position);
        }

        //            boolean checkAnim = mapAnim.containsKey(position);
//            if (checkAnim == false) {
//                mapAnim.put(position, true);
//                view.checkAnimation();
//            }

    }

    @Override
    public int getItemCount() {
        return (mListData != null ? mListData.size() : ZERO);
    }
}