﻿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>
public class HoldemRowPrefab : UdonSharpBehaviour
{
    [Header("Text References")]
    public TextMeshProUGUI txtRank;
    public TextMeshProUGUI txtPlayerName;
    public TextMeshProUGUI txtProfit;
    public TextMeshProUGUI txtBalance;

    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>
    public void UpdateDisplay(int rank, string playerName, int profit, int balance)
    {
        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

        // Color code profit (green for positive, red for negative)
        if (profit > 0)
            txtProfit.color = Color.green;
        else if (profit < 0)
            txtProfit.color = Color.red;
        else
            txtProfit.color = Color.white;

        gameObject.SetActive(true);
    }

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