package com.secspace.lib.common.adapter;

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

import java.util.ArrayList;
import java.util.List;

public abstract class Adapter<T, R extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<R> {

    private OnItemClickListener<T> mOnItemClickListener;

    private List<T> mData = new ArrayList<>();

    public void update(List<T> data) {
        this.mData = data;
        notifyDataSetChanged();
    }

    protected T getItem(int position) {
        return mData.get(position);
    }

    public void setOnItemClickListener(OnItemClickListener<T> onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener<T> {
        void onItemClick(int position, T item);
    }

    protected class OnItemViewClick implements View.OnClickListener {

        private int position;

        public OnItemViewClick(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(position, getItem(position));
            }
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }
}
