package kr.co.hanwha.hifive.admin.admin.adapter;

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

import java.util.ArrayList;

import kr.co.hanwha.hifive.admin.admin.adapter.viewholder.AdminVersionListViewHolder;
import kr.co.hanwha.hifive.admin.network.model.AdminVersionCvData;

public class AdminVersionListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final int ZERO = 0;

    private final int TYPE_TASK = 1;

    private ArrayList<AdminVersionCvData> mListData;

    private OnAdminVersionListListener mOnAdminVersionListListener;

    public AdminVersionListAdapter(@Nullable OnAdminVersionListListener onAdminVersionListListener) {
        mOnAdminVersionListListener = onAdminVersionListListener;
    }

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

    }

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

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

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

    public void addItems(ArrayList<AdminVersionCvData> 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 AdminVersionListViewHolder(parent, mOnAdminVersionListListener);

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

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


    /**************************************************
     * OnAdminTaskListListener
     **************************************************/
    public interface OnAdminVersionListListener {
        void onUrlLinkClicked(String url);

        void onDeleteClicked(int id);

        void onEditClicked(AdminVersionCvData data);
    }
}
