﻿using UdonSharp;
using UnityEngine;
using TMPro;

/// <summary>
/// Lightweight display component for a single scoreboard row.
/// Attach to the row prefab and assign TextMeshPro references.
/// </summary>
[UdonBehaviourSyncMode(BehaviourSyncMode.None)]
public class HoldemRowPrefab : UdonSharpBehaviour
{
    [Header("Text References")]
    public TextMeshProUGUI txtRank;
    public TextMeshProUGUI txtPlayerName;
    public TextMeshProUGUI txtProfit;
    public TextMeshProUGUI txtBalance;
    public TextMeshProUGUI txtPerHand;
    public TextMeshProUGUI txtHats;
    public TextMeshProUGUI txtDonations;

    private bool isInitialized = false;

    void Start()
    {
        isInitialized = txtRank != null && txtPlayerName != null && 
                       txtProfit != null && txtBalance != null;
    }

    /// <summary>
    /// Updates the row display with player data.
    /// </summary>
    /// <param name="rank">Rank number (1, 2, 3, etc.)</param>
    /// <param name="playerName">Player display name</param>
    /// <param name="profit">Player's net profit</param>
    /// <param name="balance">Player's current balance</param>
    /// <param name="handsPlayed">Wins + losses + folds</param>
    /// <param name="hatsGiven">Hats placed on others</param>
    /// <param name="hatsReceived">Hats placed on this player</param>
    /// <param name="donationsGiven">Charity sent to others</param>
    /// <param name="donationsReceived">Charity sent to this player</param>
    public void UpdateDisplay(int rank, string playerName, int profit, int balance,
        int handsPlayed, int hatsGiven, int hatsReceived, int donationsGiven, int donationsReceived)
    {
        if (!isInitialized)
        {
            isInitialized = txtRank != null && txtPlayerName != null && 
                           txtProfit != null && txtBalance != null;
        }

        if (!isInitialized) return;

        txtRank.text = rank.ToString();
        txtPlayerName.text = playerName;
        txtProfit.text = profit.ToString("N0"); // Format with commas
        txtBalance.text = balance.ToString("N0"); // Format with commas
        txtProfit.color = ProfitColor(profit);

        if (txtPerHand != null)
        {
            if (handsPlayed > 0)
            {
                int perHand = Mathf.RoundToInt((float)profit / handsPlayed);
                txtPerHand.text = perHand.ToString("+#,0;-#,0;0");
                txtPerHand.color = ProfitColor(perHand);
            }
            else
            {
                txtPerHand.text = "-";
                txtPerHand.color = Color.white;
            }
        }

        if (txtHats != null)
        {
            txtHats.text = hatsGiven + "/" + hatsReceived;
        }

        if (txtDonations != null)
        {
            txtDonations.text = donationsGiven + "/" + donationsReceived;
        }

        gameObject.SetActive(true);
    }

    // Green for positive, red for negative
    private Color ProfitColor(int value)
    {
        if (value > 0) return Color.green;
        if (value < 0) return Color.red;
        return Color.white;
    }

    /// <summary>
    /// Hides the row when no data is available.
    /// </summary>
    public void Clear()
    {
        gameObject.SetActive(false);
    }
}