package com.example.quiz.data.adapter;

import android.text.TextUtils;
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.example.quiz.R;
import com.example.quiz.data.model.Pari;
import com.example.quiz.data.model.ParisResponse;

import java.util.ArrayList;
import java.util.List;

public class LastBetsAdapter extends RecyclerView.Adapter<LastBetsAdapter.LastBetsViewHolder> {

    private List<ParisResponse> listeParis = new ArrayList<>(); // évite les NPE
    private OnBetClick onBetClick; // peut être null si tu veux

    public interface OnBetClick {
        void onItemClick(ParisResponse pari);
    }

    public LastBetsAdapter(List<ParisResponse> listeParis, OnBetClick onBetClick) {
        if (listeParis != null) this.listeParis = listeParis;
        this.onBetClick = onBetClick;
    }

    // Constructeur vide utile si tu veux créer puis setData ensuite
    public LastBetsAdapter(OnBetClick onBetClick) {
        this.onBetClick = onBetClick;
    }

    public void setData(List<ParisResponse> newList) {
        if (newList == null) {
            this.listeParis = new ArrayList<>();
        } else {
            this.listeParis = newList;
        }
        notifyDataSetChanged();
    }

    public void addItem(ParisResponse pari) {
        if (pari == null) return;
        this.listeParis.add(pari);
        notifyItemInserted(listeParis.size() - 1);
    }

    public void clear() {
        this.listeParis.clear();
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull LastBetsViewHolder holder, int position) {
        ParisResponse pari = listeParis.get(position);
        if (pari == null) return;

        holder.typeOfCourse.setText(pari.getCourseNom());
        holder.refenceTicket.setText(pari.getNumeroTicket());

        holder.betType.setText(pari.getFormules().size()>0 ? pari.getFormules().get(0):"Pas de formule");

        holder.course.setText("Course: " + pari.getCourseId());

        // Chevaux : join en toute sécurité (gère Integer list ou String list)
        holder.horses.setText(pari.getCombinaison());

        // Mise (défensif)
        holder.mise.setText("Mise: " + pari.getMiseTotale()  + " CFA");

        // click listener défensif
        holder.itemView.setOnClickListener(v -> {
            if (onBetClick != null) onBetClick.onItemClick(pari);
        });
    }

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

    public static class LastBetsViewHolder extends RecyclerView.ViewHolder {
        TextView betType, horses, course, mise, refenceTicket, typeOfCourse;

        public LastBetsViewHolder(@NonNull View itemView) {
            super(itemView);
            betType = itemView.findViewById(R.id.last_bet_type);
            course = itemView.findViewById(R.id.last_bet_course);
            horses = itemView.findViewById(R.id.last_bet_horses);
            mise = itemView.findViewById(R.id.last_bet_mise);
            refenceTicket = itemView.findViewById(R.id.reference_ticket);
            typeOfCourse = itemView.findViewById(R.id.type_of_course);
        }
    }
}
