using NUnit.Framework;
using TMPro;
using UnityEngine;

public class ScoreboardRowTests
{
    private GameObject go;
    private HoldemRowPrefab row;

    [SetUp]
    public void SetUp()
    {
        go = new GameObject("Row");
        row = go.AddComponent<HoldemRowPrefab>();
        row.txtRank = Text("Rank");
        row.txtPlayerName = Text("Player");
        row.txtProfit = Text("Profit");
        row.txtBalance = Text("Balance");
        row.txtPerHand = Text("PerHand");
        row.txtHats = Text("Hats");
        row.txtDonations = Text("Donations");
    }

    [TearDown]
    public void TearDown()
    {
        Object.DestroyImmediate(go);
    }

    private TextMeshProUGUI Text(string name)
    {
        var child = new GameObject(name);
        child.transform.SetParent(go.transform);
        return child.AddComponent<TextMeshProUGUI>();
    }

    [Test]
    public void NoHandsPlayedShowsDash()
    {
        row.UpdateDisplay(1, "Alice", 0, 1000, 0, 0, 0, 0, 0);

        Assert.AreEqual("-", row.txtPerHand.text);
    }

    [Test]
    public void LossPerHandIsRoundedAndRed()
    {
        row.UpdateDisplay(1, "Alice", -1000, 1000, 3, 0, 0, 0, 0);

        Assert.AreEqual("-333", row.txtPerHand.text);
        Assert.AreEqual(Color.red, row.txtPerHand.color);
    }

    [Test]
    public void WinPerHandHasPlusSign()
    {
        row.UpdateDisplay(1, "Alice", 2500, 1000, 2, 0, 0, 0, 0);

        Assert.AreEqual("+1,250", row.txtPerHand.text);
        Assert.AreEqual(Color.green, row.txtPerHand.color);
    }

    [Test]
    public void HatsShowGivenThenReceived()
    {
        row.UpdateDisplay(1, "Alice", 0, 1000, 0, 3, 1, 0, 0);

        Assert.AreEqual("3/1", row.txtHats.text);
    }

    [Test]
    public void DonationsShowGivenThenReceived()
    {
        row.UpdateDisplay(1, "Alice", 0, 1000, 0, 0, 0, 2, 5);

        Assert.AreEqual("2/5", row.txtDonations.text);
    }

    [Test]
    public void RowWithoutOptionalColumnsStillRenders()
    {
        row.txtPerHand = null;
        row.txtHats = null;
        row.txtDonations = null;

        row.UpdateDisplay(2, "Bob", 50, 1000, 1, 1, 1, 0, 0);

        Assert.AreEqual("Bob", row.txtPlayerName.text);
    }
}
