package com.sjkj.base_lib.adapter;

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

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.sjkj.base_lib.utils.ToastUtils;

import java.util.List;

public abstract class BaseRecAdapter<T, B extends ViewDataBinding> extends RecyclerView.Adapter {
    protected Context context;
    protected List<T> list;
    private OnItemClick<T> listener;
    protected boolean isEdit = false;

    public BaseRecAdapter(Context context) {
        this.context = context;
    }

    public BaseRecAdapter(Context context, List<T> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        B mBinding = DataBindingUtil.inflate(LayoutInflater.from(context), getLayoutResId(viewType), parent, false);
        return new RecyclerHolder(mBinding.getRoot());
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        B mBinding = DataBindingUtil.getBinding(holder.itemView);
        if (this.list == null) return;
        T bean = this.list.get(position);
        onBindItem(mBinding, bean, position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.onItemClick(bean, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return this.list == null ? 0 : this.list.size();
    }

    public void refreshList(List<T> list) {
        this.list = list;
        notifyDataSetChanged();
        if (this.list == null || this.list.size() <= 0) {
            ToastUtils.show("暂无更多数据");
        }
    }

    public void refreshList(List<T> list, boolean isEdit) {
        this.list = list;
        this.isEdit = isEdit;
        notifyDataSetChanged();
        if (this.list == null || this.list.size() <= 0) {
            ToastUtils.show("暂无更多数据");
        }
    }

    public List<T> getList() {
        return this.list;
    }

    public void clear() {
        if (this.list != null)
            this.list.clear();
        notifyDataSetChanged();
    }

    protected abstract @LayoutRes
    int getLayoutResId(int viewType);

    protected abstract void onBindItem(B binding, T t, int position);

    static class RecyclerHolder extends RecyclerView.ViewHolder {

        public RecyclerHolder(View itemView) {
            super(itemView);
        }
    }

    public void setItemClick(OnItemClick listener) {
        this.listener = listener;
    }

    //回调接口
    public interface OnItemClick<T> {
        void onItemClick(T bean, int position);
    }

}
