package com.example.boidelov3.games.papelito;

import android.annotation.SuppressLint;
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.boidelov3.R;

import java.util.ArrayList;

/**
 * Adaptateur pour afficher les résultats finaux de tous les joueurs
 */
public class PapelitoResultAdapter extends RecyclerView.Adapter<PapelitoResultAdapter.ResultViewHolder> {

    private final ArrayList<PapelitoPlayer> players;
    private final String civilWord;
    private final String undercoverWord;

    public PapelitoResultAdapter(ArrayList<PapelitoPlayer> players, String civilWord, String undercoverWord) {
        this.players = players;
        this.civilWord = civilWord;
        this.undercoverWord = undercoverWord;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ResultViewHolder holder, int position) {
        PapelitoPlayer player = players.get(position);

        // Nom du joueur
        holder.textViewPlayerName.setText(player.getName());

        // Rôle avec vérification null
        if (player.getRole() != null) {
            holder.textViewRole.setText(player.getRole().getDisplayName());
        } else {
            holder.textViewRole.setText("?");
        }

        // Statut (éliminé ou survivant)
        if (player.isAlive()) {
            holder.textViewStatus.setText("✅ Vivant");
            holder.textViewStatus.setTextColor(
                holder.itemView.getContext().getColor(R.color.success_green)
            );
        } else {
            holder.textViewStatus.setText("❌ Éliminé");
            holder.textViewStatus.setTextColor(
                holder.itemView.getContext().getColor(R.color.error_red)
            );
        }

        // Avatar avec première lettre (vérification null et bounds)
        String name = player.getName();
        if (name != null && !name.isEmpty()) {
            holder.playerAvatar.setText(name.substring(0, 1).toUpperCase());
        } else {
            holder.playerAvatar.setText("?");
        }

        // Couleur de fond selon le rôle
        int backgroundColor;
        int roleColor;
        if (player.isCivil()) {
            backgroundColor = holder.itemView.getContext().getColor(R.color.civil_bg_light);
            roleColor = holder.itemView.getContext().getColor(R.color.civil_bg);
        } else if (player.isUndercover()) {
            backgroundColor = holder.itemView.getContext().getColor(R.color.undercover_bg_light);
            roleColor = holder.itemView.getContext().getColor(R.color.undercover_bg);
        } else {
            backgroundColor = holder.itemView.getContext().getColor(R.color.mr_white_bg_light);
            roleColor = holder.itemView.getContext().getColor(R.color.mr_white_bg);
        }
        
        // Définir la couleur de fond de la carte
        if (holder.cardView != null) {
            holder.cardView.setCardBackgroundColor(backgroundColor);
        }
        
        // Définir la couleur du texte du rôle
        holder.textViewRole.setTextColor(roleColor);
    }

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

    static class ResultViewHolder extends RecyclerView.ViewHolder {
        TextView playerAvatar;
        TextView textViewPlayerName;
        TextView textViewRole;
        TextView textViewStatus;
        com.google.android.material.card.MaterialCardView cardView;

        public ResultViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView = (com.google.android.material.card.MaterialCardView) itemView;
            playerAvatar = itemView.findViewById(R.id.playerAvatarTextView);
            textViewPlayerName = itemView.findViewById(R.id.playerNameTextView);
            textViewRole = itemView.findViewById(R.id.playerRoleTextView);
            textViewStatus = itemView.findViewById(R.id.playerStatusBadge);
        }
    }
}
