using NexusShell.Services;
using Xunit;

namespace NexusShell.Tests;

public sealed class NexusCalculatorEngineTests
{
    [Fact]
    public void OnePlusOneEqualsTwo()
    {
        var engine = new NexusCalculatorEngine();
        engine.PressButton("1");
        engine.PressButton("+");
        engine.PressButton("1");
        engine.PressButton("=");

        Assert.Equal("2", engine.Display);
    }

    [Fact]
    public void TenDividedByTwoEqualsFive()
    {
        var engine = new NexusCalculatorEngine();
        engine.PressButton("1");
        engine.PressButton("0");
        engine.PressButton("/");
        engine.PressButton("2");
        engine.PressButton("=");

        Assert.Equal("5", engine.Display);
    }

    [Fact]
    public void FiveTimesFiveEqualsTwentyFive()
    {
        var engine = new NexusCalculatorEngine();
        engine.PressButton("5");
        engine.PressButton("*");
        engine.PressButton("5");
        engine.PressButton("=");

        Assert.Equal("25", engine.Display);
    }

    [Fact]
    public void TenMinusThreeEqualsSeven()
    {
        var engine = new NexusCalculatorEngine();
        engine.PressButton("1");
        engine.PressButton("0");
        engine.PressButton("-");
        engine.PressButton("3");
        engine.PressButton("=");

        Assert.Equal("7", engine.Display);
    }

    [Fact]
    public void PercentageDividesCurrentEntry()
    {
        var engine = new NexusCalculatorEngine();
        engine.PressButton("5");
        engine.PressButton("0");
        engine.PressButton("%");

        Assert.Equal("0.5", engine.Display);
    }

    [Fact]
    public void DecimalAdditionWorks()
    {
        var engine = new NexusCalculatorEngine();
        engine.PressButton("1");
        engine.PressButton(".");
        engine.PressButton("5");
        engine.PressButton("+");
        engine.PressButton("0");
        engine.PressButton(".");
        engine.PressButton("5");
        engine.PressButton("=");

        Assert.Equal("2", engine.Display);
    }

    [Fact]
    public void ClearResetsCalculator()
    {
        var engine = new NexusCalculatorEngine();
        engine.PressButton("9");
        engine.PressButton("+");
        engine.PressButton("1");
        engine.PressButton("C");

        Assert.Equal("0", engine.Display);
        Assert.Equal(string.Empty, engine.ExpressionLine);
    }

    [Fact]
    public void BackspaceRemovesLastDigit()
    {
        var engine = new NexusCalculatorEngine();
        engine.PressButton("1");
        engine.PressButton("2");
        engine.PressButton("3");
        engine.PressButton("Backspace");

        Assert.Equal("12", engine.Display);
    }

    [Fact]
    public void DivisionByZeroIsHandledSafely()
    {
        var engine = new NexusCalculatorEngine();
        engine.PressButton("5");
        engine.PressButton("/");
        engine.PressButton("0");
        engine.PressButton("=");

        Assert.True(engine.HasError);
        Assert.Equal(NexusCalculatorEngine.DivisionByZeroMessage, engine.Display);
    }
}
