package kr.co.hanwha.hifive.adapter;

import android.app.Activity;
import android.content.Context;
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.TaskDetailListContract;
import kr.co.hanwha.hifive.adapter.viewholder.CommentHeaderViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.CommentListViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.MarginViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.TaskDetailHeaderViewHolder;
import kr.co.hanwha.hifive.data.model.MessageListData;
import kr.co.hanwha.hifive.data.model.TaskDetailRoot;
import kr.co.hanwha.hifive.util.HFCommonUtil;

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

public class TaskDetailListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements TaskDetailListContract.Model, TaskDetailListContract.View {
    private final int ZERO = 0;
    private final int COUNT_HEADER = 1;
    private final int COUNT_COMMENT_HEADER = 1;
    private final int COUNT_COMMENT_EMPTY = 1;

    private final int TYPE_TASK_HEADER = 1;
    private final int TYPE_COMMENT_HEADER = 4;
    private final int TYPE_COMMENT_CONTENT = 5;
    private final int TYPE_COMMENT_EMPTY = 6;

    private Context mContext;
    private TaskDetailRoot mTaskData;
    private ArrayList<MessageListData> mListData;

    private int mEmptyHeight = 300;
    private int mTotalCount;
    private int mFlag;
    private Activity mActivity;

    private TaskDetailListContract.OnTaskDetailListener onTaskDetailListener;

    public TaskDetailListAdapter(Context context, TaskDetailListContract.OnTaskDetailListener onTaskDetailListener) {
        this.mContext = context;
        this.onTaskDetailListener = onTaskDetailListener;
    }

    public void setFlag(int flag) {
        mFlag = flag;
    }

    public void setActivity(Activity activity) {
        mActivity = activity;
    }

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

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

    /**************************************************
     * Model
     **************************************************/
    @Override
    public void setTotalCount(int totalCount) {
        mTotalCount = totalCount;
    }

    @Override
    public void clearItems() {
        if (mListData != null) mListData.clear();
    }

    @Override
    public void setTaskItem(TaskDetailRoot taskDetailRoot) {
        mTaskData = taskDetailRoot;
    }

    @Override
    public void addItem(MessageListData data) {
        if (mListData == null) mListData = new ArrayList<>();
        mListData.add(data);
    }

    @Override
    public void addItem(MessageListData data, int index) {
        if (mListData != null && mListData.size() > index) {
            mListData.set(index, data);
        }
    }

    @Override
    public void addItems(ArrayList<MessageListData> list) {
        if (mListData == null) mListData = new ArrayList<>();
        mListData.addAll(0, list);
    }

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

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return TYPE_TASK_HEADER;
        else if (position == 1) return TYPE_COMMENT_HEADER;
        else if (getCommentCount() == 0 && position == 2) return TYPE_COMMENT_EMPTY;
        else return TYPE_COMMENT_CONTENT;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_TASK_HEADER)
            return new TaskDetailHeaderViewHolder(parent, onTaskDetailListener);
        else if (viewType == TYPE_COMMENT_HEADER)
            return new CommentHeaderViewHolder(parent, onTaskDetailListener);
        else if (viewType == TYPE_COMMENT_CONTENT)
            return new CommentListViewHolder(parent, onTaskDetailListener);
        else if (viewType == TYPE_COMMENT_EMPTY)
            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) {
        if (getItemViewType(position) == TYPE_TASK_HEADER) {
            TaskDetailHeaderViewHolder view = (TaskDetailHeaderViewHolder) holder;
            view.setFlag(mFlag);
            view.setActivity(mActivity);
            view.onBind(mTaskData, 0);
            mEmptyHeight = HFCommonUtil.getScreenHeight(mContext)
                    - HFCommonUtil.getStatusBarHeight(mContext)
                    - view.getHeight();
        } else if (getItemViewType(position) == TYPE_COMMENT_HEADER) {
            CommentHeaderViewHolder view = (CommentHeaderViewHolder) holder;
            boolean isMore = mTotalCount > getCommentCount();
            view.onBind(isMore, mTotalCount);
            mEmptyHeight -= view.getHeight()
                    + HFCommonUtil.getPixelFromDimension(mContext, R.dimen.toolbar_height)
                    + HFCommonUtil.getPixelFromDimension(mContext, R.dimen.comment_min_height);
        } else if (getItemViewType(position) == TYPE_COMMENT_CONTENT) {
            CommentListViewHolder view = (CommentListViewHolder) holder;
            int index = getCommentIndex(position);
            view.onBind(mListData.get(index), index);
            view.setLastItem(getCommentCount() - 1 == index);
        } else if (getItemViewType(position) == TYPE_COMMENT_EMPTY) {
            MarginViewHolder view = (MarginViewHolder) holder;
            view.onBind(mContext.getString(R.string.task_detail_comment_empty), mEmptyHeight);
        }
    }

    @Override
    public int getItemCount() {
        return COUNT_HEADER + COUNT_COMMENT_HEADER + (getCommentCount() == 0 && mListData != null ? COUNT_COMMENT_EMPTY : getCommentCount());
    }

    private int getCommentIndex(int position) {
        return position - COUNT_HEADER - COUNT_COMMENT_HEADER;
    }

    private int getCommentCount() {
        return (mListData != null ? mListData.size() : 0);
    }
}