package com.example.quiz.data.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

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

import com.example.quiz.R;
import com.example.quiz.data.model.Course;
import com.example.quiz.data.model.TypeOfBet;

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

public class MultiTypeOfBetsAdapter extends RecyclerView.Adapter<MultiTypeOfBetsAdapter.TypeOfBetViewHolder> {
    private List<TypeOfBet> types;
    private Set<Integer> selectedPositions = new HashSet<>();
    private onItemClickListener listener;

    public interface onItemClickListener {
        void onItemClick(TypeOfBet type, boolean isChecked);
        void onItemsSelected(List<TypeOfBet> selectedItems);
    }

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

    public MultiTypeOfBetsAdapter(List<TypeOfBet> types) {
        this.types = types;
    }

    // Méthode pour pré-sélectionner les paris (rendue publique)
    public void preSelectAvailableBets(List<Course.TypeParis> preselectedTypes) {
        selectedPositions.clear();
        for (int i = 0; i < types.size(); i++) {
            if (preselectedTypes.contains(types.get(i).getName())) {
                selectedPositions.add(i);
            }
        }
        notifyDataSetChanged();
    }

    public void setTypes(List<TypeOfBet> types) {
        this.types = types;
        selectedPositions.clear();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public TypeOfBetViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.multi_type_of_bet_item, parent, false);
        return new TypeOfBetViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TypeOfBetViewHolder holder, int position) {
        TypeOfBet type = types.get(position);
        holder.type_of_bet_name.setText(type.getLabel());

        boolean isSelected = selectedPositions.contains(position);
        holder.checkBox.setChecked(isSelected);

        if (isSelected) {
            holder.itemView.setBackgroundResource(R.drawable.item_gradient_bg_selected);
        } else {
            holder.itemView.setBackgroundResource(R.drawable.item_gradient_bg);
        }

        holder.itemView.setOnClickListener(v -> {
            toggleSelection(position, type);
        });

        holder.checkBox.setOnClickListener(v -> {
            toggleSelection(position, type);
        });
    }

    private void toggleSelection(int position, TypeOfBet type) {
        boolean isCurrentlySelected = selectedPositions.contains(position);

        if (isCurrentlySelected) {
            selectedPositions.remove(position);
        } else {
            selectedPositions.add(position);
        }

        if (listener != null) {
            listener.onItemClick(type, !isCurrentlySelected);
        }


        notifyItemChanged(position);
    }

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

    public List<TypeOfBet> getSelectedItems() {
        List<TypeOfBet> selectedItems = new ArrayList<>();
        for (Integer position : selectedPositions) {
            if (position >= 0 && position < types.size()) {
                selectedItems.add(types.get(position));
            }
        }
        return selectedItems;
    }

    public boolean isSelected(int position) {
        return selectedPositions.contains(position);
    }

    public void selectAll() {
        selectedPositions.clear();
        for (int i = 0; i < types.size(); i++) {
            selectedPositions.add(i);
        }
        notifyDataSetChanged();
    }

    public void deselectAll() {
        selectedPositions.clear();
        notifyDataSetChanged();
    }

    public int getSelectedCount() {
        return selectedPositions.size();
    }

    public class TypeOfBetViewHolder extends RecyclerView.ViewHolder {
        TextView type_of_bet_name;
        CheckBox checkBox;

        public TypeOfBetViewHolder(@NonNull View itemView) {
            super(itemView);
            type_of_bet_name = itemView.findViewById(R.id.tv_bet_name);
            checkBox = itemView.findViewById(R.id.checkbox_bet);
        }
    }
}