package com.dskj.daikuan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

public abstract class BaseAdapter extends RecyclerView.Adapter implements View.OnClickListener, View.OnLongClickListener {

    private Context mContext;
    private OnItemClickListener mItemClickListener;

    public BaseAdapter(Context context) {
        mContext = context;
    }

    public abstract RecyclerView.ViewHolder getViewHolder(View itemView);

    public abstract int getItemViewResource();

    public abstract void bindItemData(RecyclerView.ViewHolder holder, int position);

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        int resource = getItemViewResource();
        if (resource <= 0) return null;

        View view = LayoutInflater.from(mContext).inflate(resource, parent, false);
        view.setOnClickListener(this);
        view.setOnLongClickListener(this);
        return getViewHolder(view);
    }

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

    @Override
    public final void onClick(View v) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick(v);
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemLongClick(v);
        }
        return false;
    }

    public Context getContext() {
        return mContext;
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View v);
        void onItemLongClick(View v);
    }
}
