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.MainEmptyFamousSayingListViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.MainHomeTitleHeaderViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.MainHomeRequestListViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.MarginViewHolder;
import kr.co.hanwha.hifive.data.model.TaskListData;
import kr.co.hanwha.hifive.adapter.model.TaskListViewHolderData;
import kr.co.hanwha.hifive.enums.ViewHolderTaskType;
import kr.co.hanwha.hifive.screen.main.home.MainHomeContract;
import kr.co.hanwha.hifive.util.HFCommonUtil;

/**
 * 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 MainHomeContract.OnMainHomeListListener mOnMainHomeListListener;
    private ArrayList<TaskListViewHolderData> mListData;

    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 (mListData == null) mListData = new ArrayList<>();
        else mListData.clear();


        if ((evaluationListData != null && evaluationListData.size() > 0) ||
                (requestedListData != null && requestedListData.size() > 0)) {
            mListData.add(new TaskListViewHolderData(ViewHolderTaskType.EVALUATION_HEADER, "", -1));

            // 평가함
            if (evaluationListData != null && evaluationListData.size() > 0) {
                for (int i = 0; i < evaluationListData.size(); i++) {
                    mListData.add(new TaskListViewHolderData(ViewHolderTaskType.EVALUATION_DATA, evaluationListData.get(i)));
                }
            }
            // 요청함
            if (requestedListData != null) {
                for (int i = 0; i < requestedListData.size(); i++) {
                    mListData.add(new TaskListViewHolderData(ViewHolderTaskType.REQUEST_DATA, requestedListData.get(i)));
                }
            }

            mListData.add(new TaskListViewHolderData(ViewHolderTaskType.BOTTOM_MARGIN, null, HFCommonUtil.getPixelFromDimension(mContext, R.dimen.dimen_dp27)));
        } else {
            int height = HFCommonUtil.getScreenHeight(mContext)
                    - HFCommonUtil.getStatusBarHeight(mContext)
                    - HFCommonUtil.getPixelFromDimension(mContext, R.dimen.main_top_toolbar_height)
                    - HFCommonUtil.getPixelFromDimension(mContext, R.dimen.main_top_tabbar_height)
                    - HFCommonUtil.getPixelFromDimension(mContext, R.dimen.main_bottom_tabbar_height)
                    - HFCommonUtil.getPixelFromDimension(mContext, R.dimen.main_home_item_title_min_height);
            mListData.add(new TaskListViewHolderData(ViewHolderTaskType.EVALUATION_HEADER, "", -1));
            mListData.add(new TaskListViewHolderData(ViewHolderTaskType.FAMOUS_SAYING_MARGIN, null, height));
        }
    }

    /**************************************************
     * 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 == ViewHolderTaskType.EVALUATION_HEADER.getCode() ||
                viewType == ViewHolderTaskType.REQUEST_HEADER.getCode()) {
            return new MainHomeTitleHeaderViewHolder(parent);
        } else if (viewType == ViewHolderTaskType.EVALUATION_DATA.getCode() ||
                viewType == ViewHolderTaskType.REQUEST_DATA.getCode()) {
            return new MainHomeRequestListViewHolder(parent, mOnMainHomeListListener);
        } else if (viewType == ViewHolderTaskType.BOTTOM_MARGIN.getCode()) {
            return new MarginViewHolder(parent);
        } else if (viewType == ViewHolderTaskType.FAMOUS_SAYING_MARGIN.getCode()) {
            return new MainEmptyFamousSayingListViewHolder(parent);
        }

        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) == ViewHolderTaskType.EVALUATION_HEADER.getCode() ||
                getItemViewType(position) == ViewHolderTaskType.REQUEST_HEADER.getCode()) {
            MainHomeTitleHeaderViewHolder view = (MainHomeTitleHeaderViewHolder) holder;
            view.onBind(data.getTitle(), data.getIntValue());
        } else if (getItemViewType(position) == ViewHolderTaskType.EVALUATION_DATA.getCode() ||
                getItemViewType(position) == ViewHolderTaskType.REQUEST_DATA.getCode()) {
            MainHomeRequestListViewHolder view = (MainHomeRequestListViewHolder) holder;
            view.onBind(data.getData(), position);
        } else if (getItemViewType(position) == ViewHolderTaskType.BOTTOM_MARGIN.getCode()) {
            MarginViewHolder view = (MarginViewHolder) holder;
            view.setColorRes(android.R.color.transparent);
            view.onBind(null, data.getIntValue());
        } else if (getItemViewType(position) == ViewHolderTaskType.FAMOUS_SAYING_MARGIN.getCode()) {
            MainEmptyFamousSayingListViewHolder view = (MainEmptyFamousSayingListViewHolder) holder;
            view.onBind(null, data.getIntValue());
        }
    }

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