using Xunit;
using ytLive.Models;
using ytLive.ViewModels;

namespace ytLive.Tests;

/// <summary>
/// The webcam size safeguard: no scene placement may exceed half the 1920×1080
/// master frame (960×540) — except the Chat scene, where it may reach half the
/// screen AREA (~1358×764 @16:9) — nor drop below 10% of the frame (192×108).
/// The cap is picked by canonical scene name. Enforced at resize (MainWindow)
/// and defensively again on every layout load.
/// </summary>
public class WebcamSafeguardTests
{
    private const string DefaultScene = "Starting";
    private const string ChatScene = "Chat";

    [Fact]
    public void Oversize_Webcam_Is_Capped_To_Half_The_Frame()
    {
        var webcam = new WebcamSceneConfig { Width = 1920, Height = 1080 };
        MainViewModel.ClampWebcamToBounds(webcam, DefaultScene);
        Assert.Equal(MainViewModel.WebcamMaxWidth, webcam.Width);
        Assert.Equal(MainViewModel.WebcamMaxHeight, webcam.Height);
    }

    [Fact]
    public void Wide_Webcam_Scales_To_Fit_Both_Dimensions()
    {
        var webcam = new WebcamSceneConfig { Width = 1000, Height = 500 };
        MainViewModel.ClampWebcamToBounds(webcam, DefaultScene);
        Assert.Equal(960, webcam.Width);
        Assert.Equal(480, webcam.Height);
    }

    [Fact]
    public void Tiny_Webcam_Is_Brought_Up_To_The_Minimum()
    {
        var webcam = new WebcamSceneConfig { Width = 10, Height = 10 };
        MainViewModel.ClampWebcamToBounds(webcam, DefaultScene);
        Assert.Equal(MainViewModel.WebcamMinWidth, webcam.Width);
        Assert.Equal(MainViewModel.WebcamMinWidth, webcam.Height);
    }

    [Fact]
    public void Short_Wide_Webcam_Meets_Both_Minimums()
    {
        var webcam = new WebcamSceneConfig { Width = 100, Height = 20 };
        MainViewModel.ClampWebcamToBounds(webcam, DefaultScene);
        Assert.True(webcam.Width >= MainViewModel.WebcamMinWidth);
        Assert.Equal(MainViewModel.WebcamMinHeight, webcam.Height);
    }

    [Fact]
    public void Round_Border_Size_Tracks_The_Shorter_Clamped_Dimension()
    {
        var webcam = new WebcamSceneConfig { Width = 1920, Height = 500 };
        MainViewModel.ClampWebcamToBounds(webcam, DefaultScene);
        Assert.Equal(960, webcam.Width);
        Assert.Equal(250, webcam.Height);
        Assert.Equal(250, webcam.RoundBorderSize);
    }

    [Fact]
    public void Chat_Webcam_May_Reach_Half_The_Screen_Area()
    {
        var webcam = new WebcamSceneConfig { Width = 1920, Height = 1080 };
        MainViewModel.ClampWebcamToBounds(webcam, ChatScene);
        Assert.Equal(MainViewModel.WebcamChatMaxWidth, webcam.Width);
        Assert.Equal(MainViewModel.WebcamChatMaxHeight, webcam.Height);
    }

    [Fact]
    public void Chat_Webcam_At_The_Default_Size_Is_Not_Grown_Or_Shrunk()
    {
        var webcam = new WebcamSceneConfig { Width = 480, Height = 270 };
        MainViewModel.ClampWebcamToBounds(webcam, ChatScene);
        Assert.Equal(480, webcam.Width);
        Assert.Equal(270, webcam.Height);
    }

    [Fact]
    public void Renamed_Chat_Scene_Loses_The_Larger_Cap()
    {
        var webcam = new WebcamSceneConfig { Width = 1920, Height = 1080 };
        MainViewModel.ClampWebcamToBounds(webcam, "chit-chat");
        Assert.Equal(MainViewModel.WebcamMaxWidth, webcam.Width);
        Assert.Equal(MainViewModel.WebcamMaxHeight, webcam.Height);
    }

    [Fact]
    public void Round_Then_Back_To_Rect_Restores_The_Original_Aspect()
    {
        var webcam = new WebcamSceneConfig { Width = 480, Height = 270 };
        webcam.ToggleClipShape();
        Assert.Equal(ClipShape.Round, webcam.ClipShape);
        Assert.Equal(480, webcam.Width);
        Assert.Equal(270, webcam.Height);
        Assert.Equal(480, webcam.RectWidth);
        Assert.Equal(270, webcam.RectHeight);

        webcam.ToggleClipShape();
        Assert.Equal(ClipShape.Traditional, webcam.ClipShape);
        Assert.Equal(480, webcam.Width);
        Assert.Equal(270, webcam.Height);
        Assert.Null(webcam.RectWidth);
        Assert.Null(webcam.RectHeight);
    }

    // The persisted-reload repro: Round + square dims (from a resize while Round),
    // no in-memory snapshot — exactly what a relaunch produces. The persisted rect
    // dims must restore the 16:9 on toggle-back.
    [Fact]
    public void Reloaded_Round_Config_Restores_Persisted_Rect_Dims()
    {
        var webcam = new WebcamSceneConfig
        {
            Width = 400,
            Height = 400,
            ClipShape = ClipShape.Round,
            RectWidth = 480,
            RectHeight = 270,
        };

        webcam.ToggleClipShape();
        Assert.Equal(ClipShape.Traditional, webcam.ClipShape);
        Assert.Equal(480, webcam.Width);
        Assert.Equal(270, webcam.Height);
        Assert.Null(webcam.RectWidth);
        Assert.Null(webcam.RectHeight);
    }

    [Fact]
    public void Legacy_Square_Rect_Is_Widened_To_16x9()
    {
        var webcam = new WebcamSceneConfig { Width = 414.92, Height = 414.92 };
        MainViewModel.HealLegacySquareRect(webcam);
        Assert.Equal(738, webcam.Width);
        Assert.Equal(414.92, webcam.Height);
    }

    [Fact]
    public void Legacy_Square_Heal_Leaves_Non_Square_Untouched()
    {
        var webcam = new WebcamSceneConfig { Width = 480, Height = 270 };
        MainViewModel.HealLegacySquareRect(webcam);
        Assert.Equal(480, webcam.Width);
        Assert.Equal(270, webcam.Height);
    }

    [Fact]
    public void Legacy_Square_Heal_Leaves_Round_Untouched()
    {
        var webcam = new WebcamSceneConfig { Width = 400, Height = 400, ClipShape = ClipShape.Round };
        MainViewModel.HealLegacySquareRect(webcam);
        Assert.Equal(400, webcam.Width);
        Assert.Equal(400, webcam.Height);
    }

    [Fact]
    public void Legacy_Square_Heal_Respects_Explicit_Rect_Dims()
    {
        var webcam = new WebcamSceneConfig { Width = 400, Height = 400, RectWidth = 480, RectHeight = 270 };
        MainViewModel.HealLegacySquareRect(webcam);
        Assert.Equal(400, webcam.Width);
        Assert.Equal(400, webcam.Height);
    }
}
