using TeleWave.Domain.Broadcast;
using Xunit;

namespace TeleWave.Domain.Tests.Broadcast;

public class BumperTemplateTests
{
    private static BumperTemplate NewTemplate() => BumperTemplate.Create("Block", "Текст 1");

    [Fact]
    public void Create_HasDefaultsAndOneVariant()
    {
        var t = NewTemplate();

        Assert.Equal(BumperTemplate.DefaultBackgroundColor, t.BackgroundColor);
        Assert.Null(t.AudioExtension);
        Assert.Null(t.BackgroundImageId);
        Assert.Equal(0, t.Revision);
        var v = Assert.Single(t.Variants);
        Assert.Equal(BumperTrigger.OnShowChange, v.Trigger);
    }

    [Fact]
    public void AddVariant_AppendsWithNextPosition()
    {
        var t = NewTemplate();

        var v1 = t.AddVariant("Text 2");

        Assert.Equal(1, v1.Position);
        Assert.Equal(2, t.Variants.Count);
        Assert.Equal(v1, t.FindVariant(v1.Id));
        Assert.Null(t.FindVariant(Guid.NewGuid()));
    }

    [Fact]
    public void RemoveVariant_CannotRemoveLast()
    {
        var t = NewTemplate();
        var first = t.Variants[0];

        Assert.False(t.RemoveVariant(first.Id)); // единственный — нельзя

        var second = t.AddVariant("Text 2");
        Assert.True(t.RemoveVariant(second.Id));
        Assert.False(t.RemoveVariant(Guid.NewGuid()));
        Assert.Single(t.Variants);
    }

    [Fact]
    public void UpdateStyle_ChangesColorsAndName()
    {
        var t = NewTemplate();

        t.UpdateStyle(
            new BumperStyle("New", BumperFont.Serif, "0x111111", "0x222222", "0x333333", "black")
        );

        Assert.Equal("New", t.Name);
        Assert.Equal(BumperFont.Serif, t.Font);
        Assert.Equal("0x111111", t.BackgroundColor);
        Assert.Equal("0x333333", t.AccentColor);
        Assert.Equal("black", t.TextColor);
        // Оформление входит в сигнатуру рендера — правка обязана пересобрать заставки.
        Assert.Equal(1, t.Revision);
    }

    [Fact]
    public void SetAudio_BumpsRevision_AndStoresDuration()
    {
        var t = NewTemplate();

        t.SetAudio(".mp3", 12.5);

        Assert.Equal(".mp3", t.AudioExtension);
        Assert.Equal(12.5, t.AudioDurationSeconds);
        Assert.Equal(1, t.Revision);
    }

    [Fact]
    public void SetAudio_NonPositiveDuration_StoresNull()
    {
        var t = NewTemplate();
        t.SetAudio(".wav", 0);
        Assert.Null(t.AudioDurationSeconds);
    }

    [Fact]
    public void ClearAudio_ResetsAndBumpsRevisionOnceWhenPresent()
    {
        var t = NewTemplate();
        t.SetAudio(".mp3", 5); // rev 1

        t.ClearAudio(); // rev 2
        Assert.Null(t.AudioExtension);
        Assert.Equal(2, t.Revision);

        t.ClearAudio(); // уже пусто — ревизия не двигается
        Assert.Equal(2, t.Revision);
    }

    [Fact]
    public void BackgroundImage_SetAndClear_BumpRevision()
    {
        var t = NewTemplate();
        var img = Guid.NewGuid();

        t.SetBackgroundImage(img); // rev 1
        Assert.Equal(img, t.BackgroundImageId);
        Assert.Equal(1, t.Revision);

        t.ClearBackgroundImage(); // rev 2
        Assert.Null(t.BackgroundImageId);
        Assert.Equal(2, t.Revision);

        t.ClearBackgroundImage(); // уже пусто
        Assert.Equal(2, t.Revision);
    }
}
