package kr.co.hanwha.hifive.adapter;

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import kr.co.hanwha.hifive.adapter.contract.MainOKRListContract;
import kr.co.hanwha.hifive.adapter.viewholder.OKRCampaignListViewHolder;
import kr.co.hanwha.hifive.data.model.ProjectCampaignData;

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

    private ArrayList<ProjectCampaignData> mListData;

    public MainOKRListAdapter() {
    }

    /**************************************************
     * 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(ProjectCampaignData data, int index) {
        if (mListData != null && mListData.size() > index) {
            mListData.set(index, data);
        }
    }

    @Override
    public void addItems(ArrayList<ProjectCampaignData> list) {
        if (mListData == null) mListData = new ArrayList<>();
        else mListData.clear();

        if (list == null) return;

        Collections.sort(list, sortProject);

        int lastProjectId = 0;
        for (int i = 0; i < list.size(); i++) {
            ProjectCampaignData data = list.get(i);
            data.setFirstItem(lastProjectId != data.getProject_id());
            mListData.add(data);
            lastProjectId = data.getProject_id();
        }
//        mListData.addAll(list);

    }

    private Comparator<ProjectCampaignData> sortProject = new Comparator<ProjectCampaignData>() {
        @Override
        public int compare(ProjectCampaignData o1, ProjectCampaignData o2) {
            return Long.compare(o1.getProject_id(), o2.getProject_id());
        }
    };

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

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OKRCampaignListViewHolder(parent);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        int index = position;

        OKRCampaignListViewHolder view = (OKRCampaignListViewHolder) holder;
        view.onBind(mListData.get(index), index);

    }

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