using RioJoy.Core.Overlay;
using Xunit;

namespace RioJoy.Core.Tests.Overlay;

public class OverlayRegionEditTests
{
    private static OverlayRegion Box() => new() { Name = "r", X = 100, Y = 50, Width = 200, Height = 80 };

    [Theory]
    [InlineData(100, 50, OverlayBoxHandle.NorthWest)]
    [InlineData(300, 50, OverlayBoxHandle.NorthEast)]
    [InlineData(100, 130, OverlayBoxHandle.SouthWest)]
    [InlineData(300, 130, OverlayBoxHandle.SouthEast)]
    [InlineData(200, 50, OverlayBoxHandle.North)]
    [InlineData(200, 130, OverlayBoxHandle.South)]
    [InlineData(100, 90, OverlayBoxHandle.West)]
    [InlineData(300, 90, OverlayBoxHandle.East)]
    [InlineData(200, 90, OverlayBoxHandle.Move)]
    [InlineData(0, 0, OverlayBoxHandle.None)]
    public void HitHandle_ResolvesEachZone(double x, double y, OverlayBoxHandle expected)
    {
        Assert.Equal(expected, OverlayRegionEdit.HitHandle(Box(), x, y, tolerance: 4));
    }

    [Fact]
    public void HitHandle_ToleranceExtendsOutsideTheBox()
    {
        Assert.Equal(OverlayBoxHandle.NorthWest, OverlayRegionEdit.HitHandle(Box(), 97, 47, 4));
        Assert.Equal(OverlayBoxHandle.None, OverlayRegionEdit.HitHandle(Box(), 94, 44, 4));
    }

    [Fact]
    public void Apply_MoveShiftsWithoutResizing()
    {
        var r = OverlayRegionEdit.Apply(100, 50, 200, 80, OverlayBoxHandle.Move, 15, -10);

        Assert.Equal((115d, 40d, 200d, 80d), r);
    }

    [Fact]
    public void Apply_EastGrowsWidthOnly()
    {
        var r = OverlayRegionEdit.Apply(100, 50, 200, 80, OverlayBoxHandle.East, 30, 99);

        Assert.Equal((100d, 50d, 230d, 80d), r);
    }

    [Fact]
    public void Apply_NorthWestMovesOriginAndResizes()
    {
        var r = OverlayRegionEdit.Apply(100, 50, 200, 80, OverlayBoxHandle.NorthWest, 10, 20);

        Assert.Equal((110d, 70d, 190d, 60d), r);
    }

    [Fact]
    public void Apply_ClampsToMinSize_OppositeEdgeStaysPut()
    {
        var r = OverlayRegionEdit.Apply(100, 50, 200, 80, OverlayBoxHandle.West, 500, 0, minSize: 4);

        Assert.Equal(296, r.X, 6);        // right edge (300) minus min width
        Assert.Equal(4, r.Width, 6);

        var s = OverlayRegionEdit.Apply(100, 50, 200, 80, OverlayBoxHandle.South, 0, -500, minSize: 4);
        Assert.Equal(50, s.Y, 6);
        Assert.Equal(4, s.Height, 6);
    }
}
