package kr.co.hanwha.hifive.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import java.util.ArrayList;

import kr.co.hanwha.hifive.adapter.contract.SearchTaskListContract;
import kr.co.hanwha.hifive.adapter.viewholder.SearchTaskListViewHolder;
import kr.co.hanwha.hifive.data.model.TaskListData;

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

public class SearchTaskListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements SearchTaskListContract.Model, SearchTaskListContract.View {
    private final int ZERO = 0;

    private final int TYPE_TASK = 1;

    private ArrayList<TaskListData> mListData;

    private SearchTaskListContract.OnSearchListListener onSearchListListener;

    public void setOnSearchListListener(SearchTaskListContract.OnSearchListListener onSearchListListener) {
        this.onSearchListListener = onSearchListListener;
    }

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

    }

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

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

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

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

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

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

    @Override
    public int getItemViewType(int position) {
        return TYPE_TASK;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_TASK)
            return new SearchTaskListViewHolder(parent, onSearchListListener);

        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) {
            int index = position;
            SearchTaskListViewHolder view = (SearchTaskListViewHolder) holder;
            view.onBind(mListData.get(index), index);
        }
    }

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