namespace splat;

using Chickensoft.GoDotTest;
using Godot;
using Shouldly;

public class GameManagerTest : TestClass
{
    public GameManagerTest(Node testScene) : base(testScene) { }

    [Test]
    public void TimeAdvancesAtSelectedSpeedAndEmitsNewDays()
    {
        var manager = new GameManager();
        var emittedDay = -1;
        manager.NewDay += day => emittedDay = day;

        manager._Process(60d);
        manager.TimeOfDay.ShouldBe(0.5f, tolerance: 0.001f);
        manager.Day.ShouldBe(0);

        manager.CycleSpeedForward();
        manager._Process(30d);
        manager.Day.ShouldBe(1);
        manager.TimeOfDay.ShouldBe(0f, tolerance: 0.001f);
        emittedDay.ShouldBe(1);
        manager.Free();
    }

    [Test]
    public void PauseStopsTimeAndResumeReturnsToNormalSpeed()
    {
        var manager = new GameManager();
        manager.SetPaused(true);
        manager.IsPaused.ShouldBeTrue();
        manager._Process(120d);
        manager.SimTime.ShouldBe(0d);

        manager.SetPaused(false);
        manager.IsPaused.ShouldBeFalse();
        manager.TimeScale.ShouldBe(1f);
        manager._Process(1d);
        manager.SimTime.ShouldBe(1d);
        manager.Free();
    }

    [Test]
    public void SpeedCycleWrapsAndEmitsEveryChange()
    {
        var manager = new GameManager();
        var emissions = 0;
        manager.SpeedChanged += _ => emissions++;

        for (var i = 0; i < GameManager.SpeedSteps.Length; i++)
            manager.CycleSpeedForward();

        manager.CurrentSpeedIndex.ShouldBe(1);
        emissions.ShouldBe(GameManager.SpeedSteps.Length);
        manager.Free();
    }
}
