using TeleWave.Domain.Broadcast;
using Xunit;

namespace TeleWave.Domain.Tests.Broadcast;

public class BumperTextVariantTests
{
    private static BumperTextVariant NewVariant() =>
        BumperTemplate.Create("b", "Текст 1").AddVariant("Text");

    [Fact]
    public void Create_HasDefaults()
    {
        var v = NewVariant();

        Assert.Equal(BumperTextVariant.DefaultWeight, v.Weight);
        Assert.Equal(BumperTrigger.OnShowChange, v.Trigger);
        Assert.Equal(BumperBackground.NextPoster, v.Background);
        Assert.Empty(v.Lines);
    }

    [Fact]
    public void Update_SetsFields_AndClampsNegativeWeightToZero()
    {
        var v = NewVariant();

        v.Update("Name", BumperTrigger.Both, BumperBackground.Template, -5);

        Assert.Equal("Name", v.Name);
        Assert.Equal(BumperTrigger.Both, v.Trigger);
        Assert.Equal(BumperBackground.Template, v.Background);
        Assert.Equal(0, v.Weight);
    }

    [Fact]
    public void SetLines_ReplacesAndRenumbers()
    {
        var v = NewVariant();

        v.SetLines([
            BumperLine.Create(7, BumperLineStyle.Label, BumperLineColor.Accent, " ДАЛЕЕ "),
            BumperLine.Create(9, BumperLineStyle.Title, BumperLineColor.Text, "{next.title}"),
        ]);

        Assert.Equal([0, 1], v.Lines.Select(l => l.Position));
        Assert.Equal("ДАЛЕЕ", v.Lines[0].Text);

        v.SetLines([BumperLine.Create(0, BumperLineStyle.Title, BumperLineColor.Text, "один")]);
        Assert.Single(v.Lines);
    }

    [Theory]
    [InlineData(BumperTrigger.OnShowChange, true, true)]
    [InlineData(BumperTrigger.OnShowChange, false, false)]
    [InlineData(BumperTrigger.BetweenEpisodes, true, false)]
    [InlineData(BumperTrigger.BetweenEpisodes, false, true)]
    [InlineData(BumperTrigger.Both, true, true)]
    [InlineData(BumperTrigger.Both, false, true)]
    public void Matches_FollowsTrigger(BumperTrigger trigger, bool isShowChange, bool expected)
    {
        var v = NewVariant();
        v.Update("n", trigger, BumperBackground.Template, 1);

        Assert.Equal(expected, v.Matches(isShowChange));
    }
}
