package com.example.quiz.data.adapter;

import static java.security.AccessController.getContext;

import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
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 onPariClickListener listener;

    public interface onPariClickListener {
        void onItemClick(ParisResponse pari);

        void onItemCancel(ParisResponse pari);
    }

    public void setPariClickListener(onPariClickListener listener) {
        this.listener = listener;
    }

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


    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");
        holder.tvStatut.setText(pari.getStatutPari() != null ? String.valueOf(pari.getStatutPari()) : "Pas de statut");
        switch (pari.getStatutPari()){
            case ANNULE:{
                holder.tvStatut.setTextColor(Color.RED);
                holder.btnAnnuler.setVisibility(View.GONE);
                holder.btnImprimer.setVisibility(View.GONE);
                break;
            }
            case PAYE:{
                holder.tvStatut.setTextColor(Color.GREEN);
                holder.btnAnnuler.setVisibility(View.GONE);
                holder.btnImprimer.setVisibility(View.GONE);
                break;
            }
            case ENREGISTRE:{
                holder.tvStatut.setTextColor(Color.YELLOW);
                holder.btnAnnuler.setVisibility(View.VISIBLE);
                holder.btnImprimer.setVisibility(View.VISIBLE);
                break;
            }
            default:{
                holder.tvStatut.setTextColor(Color.BLACK);
                holder.btnAnnuler.setVisibility(View.GONE);
                holder.btnImprimer.setVisibility(View.GONE);
                break;
            }
        }

        holder.btnAnnuler.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemCancel(pari);
            }
        });

        holder.btnImprimer.setOnClickListener(v -> {
            if (listener != null) {
                listener.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, tvStatut;
        Button btnAnnuler, btnImprimer;;

        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);
            btnAnnuler = itemView.findViewById(R.id.btn_annuler);
            btnImprimer = itemView.findViewById(R.id.btn_imprimer);
            tvStatut = itemView.findViewById(R.id.tv_statut);
        }
    }
}
