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 kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.adapter.contract.MainHomeRequestListContract;
import kr.co.hanwha.hifive.adapter.viewholder.MainHomeTitleHeaderViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.MainHomeRequestListViewHolder;
import kr.co.hanwha.hifive.data.model.TaskListData;
import kr.co.hanwha.hifive.screen.main.home.MainHomeContract;

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

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

    private final int ZERO = 0;

    private final int COUNT_EVALUATION_HEADER = 1;
    private final int COUNT_REQUSTED_HEADER = 1;

    private final int TYPE_EVALUATION_HEADER = 11;
    private final int TYPE_EVALUATION = 12;
    private final int TYPE_REQUSTED_HEADER = 21;
    private final int TYPE_REQUSTED = 22;

    private MainHomeContract.OnMainHomeListListener mOnMainHomeListListener;
    private ArrayList<TaskListData> mListDataEvaluation;
    private ArrayList<TaskListData> mListDataRequested;

    public MainHomeRequestListAdapter(@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> evaluationListData, ArrayList<TaskListData> requestedListData) {
        if (mListDataEvaluation == null) mListDataEvaluation = new ArrayList<>();
        else mListDataEvaluation.clear();
        if (mListDataRequested == null) mListDataRequested = new ArrayList<>();
        else mListDataRequested.clear();

        if (mListDataEvaluation != null) mListDataEvaluation.addAll(evaluationListData);
        if (mListDataRequested != null) mListDataRequested.addAll(requestedListData);
    }

    /**************************************************
     * Adapter
     **************************************************/
    @Override
    public int getItemViewType(int position) {
        if (getItemCountEvaluation() > 0) {
            if (position == 0) return TYPE_EVALUATION_HEADER;
            else if (position <= getItemCountEvaluation())
                return TYPE_EVALUATION;
            else if (position == getItemCountEvaluation() + COUNT_EVALUATION_HEADER)
                return TYPE_REQUSTED_HEADER;
            else return TYPE_REQUSTED;
        } else {
            if (position == 0)
                return TYPE_REQUSTED_HEADER;
            else return TYPE_REQUSTED;
        }
    }

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

        if (viewType == TYPE_EVALUATION_HEADER || viewType == TYPE_REQUSTED_HEADER) {
            return new MainHomeTitleHeaderViewHolder(parent);
        } else if (viewType == TYPE_EVALUATION || viewType == TYPE_REQUSTED) {
            return new MainHomeRequestListViewHolder(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) {
        if (getItemViewType(position) == TYPE_EVALUATION_HEADER) {
            MainHomeTitleHeaderViewHolder view = (MainHomeTitleHeaderViewHolder) holder;
            view.onBind(mContext.getString(R.string.main_home_evaluation), getItemCountEvaluation());
        } else if (getItemViewType(position) == TYPE_EVALUATION) {
            int index = getIndexEvaluation(position);
            TaskListData data = mListDataEvaluation.get(index);
            MainHomeRequestListViewHolder view = (MainHomeRequestListViewHolder) holder;
            view.onBind(data, index);
        } else if (getItemViewType(position) == TYPE_REQUSTED_HEADER) {
            MainHomeTitleHeaderViewHolder view = (MainHomeTitleHeaderViewHolder) holder;
            view.onBind(mContext.getString(R.string.main_home_requested), getItemCountRequest());
        } else if (getItemViewType(position) == TYPE_REQUSTED) {
            int index = getIndexRequest(position);
            TaskListData data = mListDataRequested.get(index);
            MainHomeRequestListViewHolder view = (MainHomeRequestListViewHolder) holder;
            view.onBind(data, index);
        }
    }

    @Override
    public int getItemCount() {
        return (getItemCountEvaluation() > 0 ? getItemCountEvaluation() + COUNT_EVALUATION_HEADER : ZERO)
                + getItemCountRequest() + COUNT_REQUSTED_HEADER;
    }

    private int getItemCountEvaluation() {
        return (mListDataEvaluation != null ? mListDataEvaluation.size() : ZERO);
    }

    private int getItemCountRequest() {
        return (mListDataRequested != null ? mListDataRequested.size() : ZERO);
    }

    private int getIndexEvaluation(int position) {
        return position - COUNT_EVALUATION_HEADER;
    }

    private int getIndexRequest(int position) {
        return position - (getItemCountEvaluation() > 0 ? getItemCountEvaluation() + COUNT_EVALUATION_HEADER : ZERO) - COUNT_REQUSTED_HEADER;
    }
}
