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.MainHomeAssignedListContract;
import kr.co.hanwha.hifive.adapter.viewholder.MainEmptyFamousSayingListViewHolder;
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.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 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;

    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();

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

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

            //완료된 업무
            if (closedListData != null) {
                if (closedListData.size() > 0)
                    mListData.add(new TaskListViewHolderData(ViewHolderTaskType.TASK_CLOSED_HEADER, mContext.getString(R.string.main_home_closed_title), (closedListData != null ? closedListData.size() : 0)));
                for (int i = 0; i < closedListData.size(); i++) {
                    mListData.add(new TaskListViewHolderData(ViewHolderTaskType.TASK_CLOSED_DATA, closedListData.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.TASK_HEADER, mContext.getString(R.string.main_home_today_title), (taskListData != null ? taskListData.size() : 0)));
            mListData.add(new TaskListViewHolderData(ViewHolderTaskType.FAMOUS_SAYING_MARGIN, null, height));
        }

//        int height = HFCommonUtil.getPixelFromDimension(mContext, R.dimen.dimen_dp16);
//        mListData.add(new TaskListViewHolderData(ViewHolderTaskType.BOTTOM_MARGIN, "", 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.TODAY_HEADER.getCode()) {
            return new MainHomeTodayHeaderViewHolder(parent);
        } else if (viewType == ViewHolderTaskType.TASK_HEADER.getCode()
                || viewType == ViewHolderTaskType.TASK_CLOSED_HEADER.getCode()) {
            return new MainHomeTitleHeaderViewHolder(parent);
        } else if (viewType == ViewHolderTaskType.TODAY_DATA.getCode()
                || viewType == ViewHolderTaskType.TASK_DATA.getCode()
                || viewType == ViewHolderTaskType.TASK_CLOSED_DATA.getCode()) {
            return new MainHomeAssignedListViewHolder(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.TODAY_HEADER.getCode()) {
            MainHomeTodayHeaderViewHolder view = (MainHomeTodayHeaderViewHolder) holder;
            view.onBind(null, data.getIntValue());
        } else if (getItemViewType(position) == ViewHolderTaskType.TASK_HEADER.getCode() ||
                getItemViewType(position) == ViewHolderTaskType.TASK_CLOSED_HEADER.getCode()) {
            MainHomeTitleHeaderViewHolder view = (MainHomeTitleHeaderViewHolder) holder;
            view.onBind(data.getTitle(), data.getIntValue());
        } else if (getItemViewType(position) == ViewHolderTaskType.TODAY_DATA.getCode() ||
                getItemViewType(position) == ViewHolderTaskType.TASK_DATA.getCode() ||
                getItemViewType(position) == ViewHolderTaskType.TASK_CLOSED_DATA.getCode()) {
            MainHomeAssignedListViewHolder view = (MainHomeAssignedListViewHolder) 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);
    }
}