package com.auto.clicker.autoclicker.ui.floating;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.auto.clicker.autoclicker.room.entity.Solution;

import java.util.List;

public class SolutionAdapter extends RecyclerView.Adapter<SolutionAdapter.SolutionViewHolder> {

    private List<Solution> solutionList;
    private OnSolutionClickListener listener;

    // 定义接口用于处理点击事件
    public interface OnSolutionClickListener {
        void onSolutionClick(Solution solution);
    }

    public SolutionAdapter(List<Solution> solutionList, OnSolutionClickListener listener) {
        this.solutionList = solutionList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public SolutionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(android.R.layout.simple_list_item_1, parent, false); // 可以自定义更复杂的item布局
        return new SolutionViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SolutionViewHolder holder, int position) {
        Solution solution = solutionList.get(position);
        holder.solutionNameTextView.setText(solution.getSolutionName());

        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onSolutionClick(solution);
            }
        });
    }

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

    public void setSolutions(List<Solution> newSolutions) {
        this.solutionList.clear();
        if (newSolutions != null) {
            this.solutionList.addAll(newSolutions);
        }
        notifyDataSetChanged();
    }

    static class SolutionViewHolder extends RecyclerView.ViewHolder {
        TextView solutionNameTextView;

        SolutionViewHolder(View itemView) {
            super(itemView);
            // simple_list_item_1 只有一个 TextView
            solutionNameTextView = itemView.findViewById(android.R.id.text1);
        }
    }
}
