package com.bbitcn.silk.base;

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

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.xuexiang.rxutil2.rxjava.RxJavaUtils;
import com.xuexiang.rxutil2.rxjava.task.RxUITask;

import java.lang.reflect.Method;
import java.util.List;

/**
 * RecyclerAdapter基类
 *
 * @param <T> 数据-类型
 * @param <B> 布局-类型
 */
public abstract class BaseRecyclerAdapter<T, B extends ViewBinding> extends RecyclerView.Adapter<BaseRecyclerAdapter<T, B>.BaseViewHolder> {

    View tvTipNone;

    protected List<T> dataList;
    private Class<B> bindingClass;

    /**
     * 构造方法，必须重写此方法
     *
     * @param dataList     数据list
     * @param bindingClass 布局类，例如ActivityMainBinding.class
     */
    public BaseRecyclerAdapter(List<T> dataList, Class<B> bindingClass) {
        this.dataList = dataList;
        this.bindingClass = bindingClass;
    }

    public BaseRecyclerAdapter(List<T> dataList, Class<B> bindingClass, View tvTipNone) {
        this.dataList = dataList;
        this.bindingClass = bindingClass;
        this.tvTipNone = tvTipNone;
        if (dataList != null && dataList.size() > 0) {
            tvTipNone.setVisibility(View.GONE);
        } else {
            tvTipNone.setVisibility(View.VISIBLE);
        }
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        try {
            Method inflateMethod = bindingClass.getDeclaredMethod("inflate", LayoutInflater.class, ViewGroup.class, boolean.class);
            B binding = (B) inflateMethod.invoke(null, LayoutInflater.from(parent.getContext()), parent, false);
            return new BaseViewHolder(binding);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Failed to inflate binding class: " + bindingClass.getSimpleName());
        }
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        if (holder.binding != null) {
            T data = dataList.get(position);
            bindData(holder.binding, data, position);
        }
    }

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

    /**
     * 快捷更新RecycleView数据
     *
     * @param dataList 数据
     */
    public void setDataList(List<T> dataList) {
        RxJavaUtils.doInUIThread(new RxUITask<Object>(null) {
            @Override
            public void doInUIThread(Object o) {
                if (tvTipNone != null) {
                    if (dataList != null && dataList.size() > 0) {
                        tvTipNone.setVisibility(View.GONE);
                    } else {
                        tvTipNone.setVisibility(View.VISIBLE);
                    }
                }
                BaseRecyclerAdapter.this.dataList = dataList;
                notifyDataSetChanged();
            }
        });
    }

    public void addDataList(List<T> dataList) {
        this.dataList.addAll(dataList);
        //只刷新增加新数据
        notifyItemRangeInserted(this.dataList.size() - dataList.size(), dataList.size());
    }

    /**
     * 手动实现具体数据与布局的绑定
     *
     * @param binding 布局
     * @param data    数据
     */
    protected abstract void bindData(B binding, T data);

    /**
     * 有需要位置信息的，可以重写该方法
     *
     * @param binding  布局
     * @param data     数据
     * @param position 位置
     */
    protected void bindData(B binding, T data, int position) {
        bindData(binding, data);
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder {
        public B binding;

        public BaseViewHolder(B binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}