using NUnit.Framework;
using UnityEngine;
using UnityEngine.UI;

public class GlobalToggleFlagsTests
{
    private GameObject root;
    private GlobalToggleFlags flags;
    private Toggle primary;
    private Toggle mirror;
    private HoldemPlayer player;
    private VrcRuntimeFake runtime;

    [SetUp]
    public void SetUp()
    {
        runtime = new VrcRuntimeFake();
        runtime.Install();
        root = new GameObject("PhysicalToggleTest");
        flags = root.AddComponent<GlobalToggleFlags>();
        primary = new GameObject("Primary").AddComponent<Toggle>();
        primary.transform.SetParent(root.transform);
        mirror = new GameObject("Mirror").AddComponent<Toggle>();
        mirror.transform.SetParent(root.transform);
        player = new GameObject("Player").AddComponent<HoldemPlayer>();
        player.transform.SetParent(root.transform);
        flags.physicalBettingToggle = primary;
        flags.allPhysicalBettingToggles = new[] { primary, mirror, null };
        flags.holdemPlayers = new[] { player, null };
    }

    [TearDown]
    public void TearDown()
    {
        runtime.Uninstall();
        Object.DestroyImmediate(root);
    }

    [Test]
    public void MissingPersistenceDefaultsOffAndUpdatesPlayers()
    {
        primary.SetIsOnWithoutNotify(true);
        flags.OnPlayerRestored(runtime.LocalPlayer);

        Assert.IsFalse(primary.isOn);
        Assert.IsFalse(mirror.isOn);
        Assert.IsFalse((bool)VrcRuntimeFake.GetPrivate(player, "physicalBettingEnabled"));
    }

    [Test]
    public void StoredValueRestoresAndRuntimeChangesPersist()
    {
        runtime.SeedInt("WorldSettings/PhysicalChipBetting", 1);
        flags.OnPlayerRestored(runtime.LocalPlayer);

        Assert.IsTrue(primary.isOn);
        Assert.IsTrue(mirror.isOn);
        Assert.IsTrue((bool)VrcRuntimeFake.GetPrivate(player, "physicalBettingEnabled"));

        primary.SetIsOnWithoutNotify(false);
        flags.UpdatePhysicalBetting();
        Assert.AreEqual(0, runtime.GetInt("WorldSettings/PhysicalChipBetting"));
        Assert.IsFalse((bool)VrcRuntimeFake.GetPrivate(player, "physicalBettingEnabled"));
    }

    [Test]
    public void BeforeRestoreDoesNotWritePersistence()
    {
        primary.SetIsOnWithoutNotify(true);
        flags.UpdatePhysicalBetting();
        CollectionAssert.IsEmpty(runtime.Writes);
    }

    [Test]
    public void AnimatedContainers_AreLocalPersistedAndPropagateThroughSeatZero()
    {
        HoldemGame game = new GameObject("Game").AddComponent<HoldemGame>();
        game.transform.SetParent(root.transform);
        player.game = game;
        player.seatIdx = 0;
        Toggle animated = new GameObject("Animated").AddComponent<Toggle>();
        animated.transform.SetParent(root.transform);
        flags.animatedChipContainersToggle = animated;
        flags.allAnimatedChipContainersToggles = new[] { animated };

        animated.SetIsOnWithoutNotify(true);
        flags.UpdateAnimatedChipContainers();

        Assert.IsTrue((bool)VrcRuntimeFake.GetPrivate(game, "animatedChipContainersEnabled"));
        Assert.IsTrue(animated.isOn);
    }

    [Test]
    public void AnimatedContainers_DefaultToStaticBeforePersistenceRestores()
    {
        HoldemGame game = new GameObject("Game").AddComponent<HoldemGame>();
        game.transform.SetParent(root.transform);
        player.game = game;
        player.seatIdx = 0;
        Toggle animated = new GameObject("Animated").AddComponent<Toggle>();
        animated.transform.SetParent(root.transform);
        animated.SetIsOnWithoutNotify(true);
        flags.animatedChipContainersToggle = animated;
        flags.holdemPlayers = new[] { player };

        VrcRuntimeFake.InvokeNoArg(flags, "Start");

        Assert.IsTrue(game._IsChipPresentationModeReady());
        Assert.IsFalse(game.animatedChipContainersEnabled);
        Assert.IsFalse(animated.isOn);

        runtime.SeedInt("WorldSettings/AnimatedChipContainers", 1);
        flags.OnPlayerRestored(runtime.LocalPlayer);

        Assert.IsTrue(game._IsChipPresentationModeReady());
        Assert.IsTrue(game.animatedChipContainersEnabled);
    }
}
