package com.hx.utils.application.base;

import android.annotation.SuppressLint;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;

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

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public abstract class BaseAdapter<D, T extends ViewBinding> extends RecyclerView.Adapter<BaseAdapter.BindViewHolder<T>> {
    OnItemClickListener onItemClickListener;
    OnItemChildClickListener onItemChildClickListener;

    T binding;
    //数据源
    public List<D> mList;
    public Set<Integer> idList = new HashSet<>();

    public BaseAdapter(List<D> list) {
        if (list == null) {
            mList = new ArrayList<>();
        } else {
            mList = list;
        }
    }

    public BaseAdapter() {
        mList = new ArrayList<>();
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @NonNull
    @Override
    public BindViewHolder<T> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        binding = onCreateView(parent);
        BindViewHolder<T> bindViewHolder = new BindViewHolder<>(binding);
        binding.getRoot().setOnClickListener(view -> {
            if (onItemClickListener == null) return;
            onItemClickListener.onItemClick(this, view, bindViewHolder.getLayoutPosition());
        });
        for (Integer item : idList) {
            View view = binding.getRoot().findViewById(item);
            if (view != null) {
                view.setOnClickListener(it -> {
                    if (onItemChildClickListener == null) return;
                    onItemChildClickListener.onItemChildClick(this, it, bindViewHolder.getLayoutPosition());
                });
            }
        }

        return bindViewHolder;
    }

    public void setList(List<D> list) {
        if (list == null) return;
        mList.clear();
        mList.addAll(list);
        notifyItemChanged(0, mList.size());
    }

    public void addDate(List<D> list) {
        if (list == null) return;
        mList.addAll(list);
        notifyItemRangeInserted(mList.size() - list.size(), list.size());
    }

    public void addDate(D d) {
        if (mList == null) return;
        mList.add(d);
        notifyItemChanged(mList.size());
    }

    public void removeItem(int position) {
        if (position >= 0 && position < mList.size()) {
            mList.remove(position);
            notifyItemRemoved(position);
        }
    }

    public void updateItem(int position, D newItem) {
        if (position >= 0 && position < mList.size()) {
            mList.set(position, newItem);
            notifyItemChanged(position);
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    public void clearData() {
        if (mList == null) return;
        mList.clear();
        notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")
    public List<D> getData() {
        if (mList == null) return null;
        return mList;
    }

    public abstract T onCreateView(ViewGroup parent);

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

    public void setOnItemChildClickListener(OnItemChildClickListener onItemChildClickListener) {
        this.onItemChildClickListener = onItemChildClickListener;
    }

    public void addClickChildIds(@NonNull Set<Integer> list) {
        idList.addAll(list);
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public void onBindViewHolder(@NonNull BindViewHolder<T> holder, int position) {
        onBindView(holder.bind, mList.get(position), position);
    }

    protected abstract void onBindView(@NonNull T bind, @NonNull D data, int position);


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

    public int getPositionByItem(D item) {
        if (item == null) return -1;
        for (int i = 0; i < mList.size(); i++) {
            if (item.equals(mList.get(i))) {
                return i;
            }
        }
        return -1; // 如果未找到匹配的项，则返回 -1
    }

    public D getItemByPosition(int position) {
        if (position >= 0 && position < mList.size()) {
            return mList.get(position);
        } else {
            return null; // 如果位置无效，则返回 null
        }
    }


    public static class BindViewHolder<T extends ViewBinding> extends RecyclerView.ViewHolder {
        T bind;

        public BindViewHolder(@NonNull T itemView) {
            super(itemView.getRoot());
            bind = itemView;
        }
    }

    public interface OnItemClickListener {
        void onItemClick(BaseAdapter<?, ?> adapter, @NonNull View view, int position);
    }

    public interface OnItemChildClickListener {
        void onItemChildClick(BaseAdapter<?, ?> adapter, @NonNull View view, int position);
    }
}
