using Inspectron.Epson.Templates.Configuration;
using Inspectron.Epson.Templates.Interpreter;
using Inspectron.Epson.Templates.Language;
using Inspectron.Epson.Templates.Language.Nodes;

namespace Inspectron.Epson.Templates.Tests.Interpreter;

public class InterpreterTextTests
{
    private readonly PrinterProfile _profile = new("test", "Test Printer", 48, 24, false);

    private TemplateNode Parse(string source)
    {
        var lexer = new Lexer(source);
        var parser = new Parser(lexer.Tokenize().Tokens);
        return parser.Parse().Template;
    }

    [Fact]
    public void Interpret_PlainText_OutputsText()
    {
        var template = Parse("Hello World");
        var interpreter = new TemplateInterpreter(_profile);
        var commands = interpreter.Interpret(template, "{}");

        Assert.Contains(commands, c => c.Text == "Hello World");
    }

    [Fact]
    public void Interpret_BoldStyle_SetsBoldFlag()
    {
        var template = Parse("#bold# text #");
        var interpreter = new TemplateInterpreter(_profile);
        var commands = interpreter.Interpret(template, "{}");

        var cmd = commands.FirstOrDefault(c => c.Text.Contains("text"));
        Assert.NotNull(cmd);
        Assert.True(cmd.IsBold);
    }

    [Fact]
    public void Interpret_BigStyle_SetsBigFlag()
    {
        var template = Parse("#big# text #");
        var interpreter = new TemplateInterpreter(_profile);
        var commands = interpreter.Interpret(template, "{}");

        var cmd = commands.FirstOrDefault(c => c.Text.Contains("text"));
        Assert.NotNull(cmd);
        Assert.True(cmd.IsBig);
    }

    [Fact]
    public void Interpret_TallStyle_SetsTallFlag()
    {
        var template = Parse("#tall# text #");
        var interpreter = new TemplateInterpreter(_profile);
        var commands = interpreter.Interpret(template, "{}");

        var cmd = commands.FirstOrDefault(c => c.Text.Contains("text"));
        Assert.NotNull(cmd);
        Assert.True(cmd.IsTall);
    }

    [Fact]
    public void Interpret_RedStyle_SetsRedFlag_WhenSupported()
    {
        var profileWithRed = new PrinterProfile("test", "Test", 48, 24, true);
        var template = Parse("#red# text #");
        var interpreter = new TemplateInterpreter(profileWithRed);
        var commands = interpreter.Interpret(template, "{}");

        var cmd = commands.FirstOrDefault(c => c.Text.Contains("text"));
        Assert.NotNull(cmd);
        Assert.True(cmd.IsRed);
    }

    [Fact]
    public void Interpret_RedStyle_IgnoresRedFlag_WhenNotSupported()
    {
        var profileNoRed = new PrinterProfile("test", "Test", 48, 24, false);
        var template = Parse("#red# text #");
        var interpreter = new TemplateInterpreter(profileNoRed);
        var commands = interpreter.Interpret(template, "{}");

        var cmd = commands.FirstOrDefault(c => c.Text.Contains("text"));
        Assert.NotNull(cmd);
        Assert.False(cmd.IsRed);
    }

    [Fact]
    public void Interpret_CenterStyle_CentersText()
    {
        var template = Parse("#center# Hi #");
        var interpreter = new TemplateInterpreter(_profile);
        var commands = interpreter.Interpret(template, "{}");

        var cmd = commands.FirstOrDefault(c => c.Text.Contains("Hi"));
        Assert.NotNull(cmd);
        Assert.StartsWith(" ", cmd.Text);
    }

    [Fact]
    public void Interpret_MultipleStyles_CombinesFlags()
    {
        var template = Parse("#bold,big,tall# text #");
        var interpreter = new TemplateInterpreter(_profile);
        var commands = interpreter.Interpret(template, "{}");

        var cmd = commands.FirstOrDefault(c => c.Text.Contains("text"));
        Assert.NotNull(cmd);
        Assert.True(cmd.IsBold);
        Assert.True(cmd.IsBig);
        Assert.True(cmd.IsTall);
    }

    [Fact]
    public void Interpret_DashSeparator_OutputsFullWidthLine()
    {
        var template = Parse("---");
        var interpreter = new TemplateInterpreter(_profile);
        var commands = interpreter.Interpret(template, "{}");

        var cmd = commands.FirstOrDefault(c => c.Text.Contains('-'));
        Assert.NotNull(cmd);
        Assert.Equal(48, cmd.Text.Length);
        Assert.True(cmd.Text.All(c => c == '-'));
    }

    [Fact]
    public void Interpret_EqualsSeparator_OutputsFullWidthLine()
    {
        var template = Parse("===");
        var interpreter = new TemplateInterpreter(_profile);
        var commands = interpreter.Interpret(template, "{}");

        var cmd = commands.FirstOrDefault(c => c.Text.Contains('='));
        Assert.NotNull(cmd);
        Assert.Equal(48, cmd.Text.Length);
    }

    [Fact]
    public void Interpret_SpacingStyle_SetsLineSpacing()
    {
        var template = Parse("#spacing:50# text #");
        var interpreter = new TemplateInterpreter(_profile);
        var commands = interpreter.Interpret(template, "{}");

        var cmd = commands.FirstOrDefault(c => c.Text.Contains("text"));
        Assert.NotNull(cmd);
        Assert.Equal(50, cmd.SetLineSpacing);
    }
}
