package com.daya.live_teaching.ui.base;

import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

public abstract class BaseRecyclerAdapter<V extends BaseViewHolder> extends RecyclerView.Adapter<BaseViewHolder>
        implements View.OnClickListener
        , View.OnLongClickListener {

    public OnItemClickListener onItemClickListener;
    public OnItemLongClickListener onItemLongClickListener;
    public OnSubViewClickListener onSubViewClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        this.onItemLongClickListener = listener;
    }

    public void setOnSubViewClickListener(OnSubViewClickListener listener) {
        this.onSubViewClickListener = listener;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        holder.itemView.setTag(position);
        holder.onBind(position);
        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(this);
        }
        if (onItemLongClickListener != null) {
            holder.itemView.setOnLongClickListener(this);
        }
        if (onSubViewClickListener != null) {
            holder.setSubViewClickListener(onSubViewClickListener, position);
        }
    }

    private long tempTime;

    @Override
    public void onClick(View v) {
        if (v.getTag() != null) {
            int position = (int) v.getTag();
            long newTime = System.currentTimeMillis();
            if ((newTime - tempTime) > 1000) {
                onItemClickListener.onItemClick(position);
                tempTime = newTime;
            }

        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (v.getTag() != null) {
            int position = (int) v.getTag();
            onItemLongClickListener.onItemLongClick(position);
        }
        return true;
    }


    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public interface OnSubViewClickListener {
        void onSubViewClick(View v, int position);
    }

    public interface OnItemLongClickListener {
        void onItemLongClick(int position);
    }

}