using Bunit;
using ZB.MOM.WW.ScadaBridge.CentralUI.Components.Shared;

namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Shared;

/// <summary>
/// Tests for T34b's <see cref="DarkModeToggle"/>. The component is pure
/// JS-interop over <c>window.sbTheme</c> (theme.js): <c>sbTheme.get</c> reflects
/// the persisted theme on first render and <c>sbTheme.toggle</c> flips it on
/// click. The toggle carries the required a11y attributes
/// (<c>aria-label="Toggle dark mode"</c> + <c>aria-pressed</c>).
/// </summary>
public class DarkModeToggleTests : BunitContext
{
    public DarkModeToggleTests()
    {
        // Loose mode: unconfigured interop returns default. We configure the two
        // calls the component actually makes (get on first render, toggle on click).
        JSInterop.Mode = JSRuntimeMode.Loose;
    }

    [Fact]
    public void Renders_Button_WithToggleDarkModeAriaLabel()
    {
        JSInterop.Setup<string>("sbTheme.get").SetResult("light");

        var cut = Render<DarkModeToggle>();

        var button = cut.Find("button[aria-label='Toggle dark mode']");
        Assert.NotNull(button);
    }

    [Fact]
    public void OnFirstRender_PersistedLight_AriaPressedFalse()
    {
        // sbTheme.get returns light → button reflects light (aria-pressed=false,
        // moon glyph offering "switch to dark").
        JSInterop.Setup<string>("sbTheme.get").SetResult("light");

        var cut = Render<DarkModeToggle>();

        var button = cut.Find("button.sb-theme-toggle");
        Assert.Equal("false", button.GetAttribute("aria-pressed"));
        Assert.NotNull(cut.Find("i.bi-moon-stars"));
    }

    [Fact]
    public void OnFirstRender_PersistedDark_AriaPressedTrue()
    {
        // sbTheme.get returns dark → button reflects dark (aria-pressed=true,
        // sun glyph offering "switch to light").
        JSInterop.Setup<string>("sbTheme.get").SetResult("dark");

        var cut = Render<DarkModeToggle>();

        cut.WaitForAssertion(() =>
        {
            var button = cut.Find("button.sb-theme-toggle");
            Assert.Equal("true", button.GetAttribute("aria-pressed"));
            Assert.NotNull(cut.Find("i.bi-sun"));
        });
    }

    [Fact]
    public void Click_InvokesToggle_AndReflectsNewState()
    {
        // Start persisted-light; toggling returns "dark". The click handler must
        // invoke sbTheme.toggle and flip aria-pressed/glyph to the dark state.
        JSInterop.Setup<string>("sbTheme.get").SetResult("light");
        var toggle = JSInterop.Setup<string>("sbTheme.toggle").SetResult("dark");

        var cut = Render<DarkModeToggle>();

        cut.Find("button.sb-theme-toggle").Click();

        cut.WaitForAssertion(() =>
        {
            // The handler invoked sbTheme.toggle.
            Assert.Single(toggle.Invocations);
            var button = cut.Find("button.sb-theme-toggle");
            Assert.Equal("true", button.GetAttribute("aria-pressed"));
            Assert.NotNull(cut.Find("i.bi-sun"));
        });
    }
}
