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.SettingMyHomeTodoListContract;
import kr.co.hanwha.hifive.adapter.model.TaskListViewHolderData;
import kr.co.hanwha.hifive.adapter.viewholder.MainEmptyFamousSayingListViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.MarginViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.SettingMyHomeTodoHeaderViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.SettingMyHomeTodoListViewHolder;
import kr.co.hanwha.hifive.data.model.TaskListData;
import kr.co.hanwha.hifive.enums.ViewHolderTaskType;
import kr.co.hanwha.hifive.screen.main.setting.myhome.contract.MyHomeContract;
import kr.co.hanwha.hifive.util.HFCommonUtil;

/**
 * Created by aaron on 2018. 8. 30.
 */

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

    private final int ZERO = 0;

    private MyHomeContract.OnSettingMyHomeTodoListListener mMyHomeTodoListListener;
    private ArrayList<TaskListViewHolderData> mListData;

    public SettingMyHomeTodoListAdapter(@NonNull Context context,
                                        MyHomeContract.OnSettingMyHomeTodoListListener onMyHomeTodoListListener) {
        mContext = context;
        mMyHomeTodoListListener = onMyHomeTodoListListener;
    }

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

        //오늘 할일
        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)));
            }
        }

        //진행중인 업무
        mListData.add(new TaskListViewHolderData(ViewHolderTaskType.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(ViewHolderTaskType.TASK_DATA, taskListData.get(i)));
            }
        }

        //완료된 업무
        mListData.add(new TaskListViewHolderData(ViewHolderTaskType.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(ViewHolderTaskType.TASK_CLOSED_DATA, closedListData.get(i)));
            }
        }

        mListData.add(new TaskListViewHolderData(ViewHolderTaskType.BOTTOM_MARGIN, null, HFCommonUtil.getPixelFromDimension(mContext, R.dimen.dimen_dp27)));


//        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()
                || viewType == ViewHolderTaskType.TASK_HEADER.getCode()
                || viewType == ViewHolderTaskType.TASK_CLOSED_HEADER.getCode()) {
            return new SettingMyHomeTodoHeaderViewHolder(parent);
        } else if (viewType == ViewHolderTaskType.TODAY_DATA.getCode()
                || viewType == ViewHolderTaskType.TASK_DATA.getCode()
                || viewType == ViewHolderTaskType.TASK_CLOSED_DATA.getCode()) {
            return new SettingMyHomeTodoListViewHolder(parent, mMyHomeTodoListListener);
        } else if (viewType == ViewHolderTaskType.BOTTOM_MARGIN.getCode()) {
            return new MarginViewHolder(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() ||
                getItemViewType(position) == ViewHolderTaskType.TASK_HEADER.getCode() ||
                getItemViewType(position) == ViewHolderTaskType.TASK_CLOSED_HEADER.getCode()) {
            SettingMyHomeTodoHeaderViewHolder view = (SettingMyHomeTodoHeaderViewHolder) holder;
            view.onBind(data, data.getIntValue());
        } else if (getItemViewType(position) == ViewHolderTaskType.TODAY_DATA.getCode() ||
                getItemViewType(position) == ViewHolderTaskType.TASK_DATA.getCode() ||
                getItemViewType(position) == ViewHolderTaskType.TASK_CLOSED_DATA.getCode()) {
            SettingMyHomeTodoListViewHolder view = (SettingMyHomeTodoListViewHolder) 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);
    }
}