using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Driver.AbCip.Cli.Commands;

namespace ZB.MOM.WW.OtOpcUa.Driver.AbCip.Cli.Tests;

/// <summary>
///     Covers <see cref="WriteCommand.ParseValue"/>. Every Logix atomic type has at least
///     one happy-path case plus a failure case for unparseable input.
/// </summary>
[Trait("Category", "Unit")]
public sealed class WriteCommandParseValueTests
{
    /// <summary>Verifies ParseValue accepts common boolean aliases.</summary>
    /// <param name="raw">Input string to parse.</param>
    /// <param name="expected">Expected boolean value.</param>
    [Theory]
    [InlineData("true", true)]
    [InlineData("0", false)]
    [InlineData("on", true)]
    [InlineData("NO", false)]
    public void ParseValue_Bool_accepts_common_aliases(string raw, bool expected)
    {
        WriteCommand.ParseValue(raw, AbCipDataType.Bool).ShouldBe(expected);
    }

    /// <summary>Verifies ParseValue rejects invalid boolean input.</summary>
    [Fact]
    public void ParseValue_Bool_rejects_garbage()
    {
        Should.Throw<CliFx.Exceptions.CommandException>(
            () => WriteCommand.ParseValue("maybe", AbCipDataType.Bool));
    }

    /// <summary>Verifies ParseValue correctly widens SInt to signed byte.</summary>
    [Fact]
    public void ParseValue_SInt_widens_to_sbyte()
    {
        WriteCommand.ParseValue("-128", AbCipDataType.SInt).ShouldBe((sbyte)-128);
        WriteCommand.ParseValue("127", AbCipDataType.SInt).ShouldBe((sbyte)127);
    }

    /// <summary>Verifies ParseValue correctly parses signed 16-bit integers.</summary>
    [Fact]
    public void ParseValue_Int_signed_16bit()
    {
        WriteCommand.ParseValue("-32768", AbCipDataType.Int).ShouldBe((short)-32768);
    }

    /// <summary>Verifies ParseValue handles DInt and Dt data types as 32-bit integers.</summary>
    [Fact]
    public void ParseValue_DInt_and_Dt_both_land_on_int()
    {
        WriteCommand.ParseValue("42", AbCipDataType.DInt).ShouldBeOfType<int>();
        WriteCommand.ParseValue("1234567", AbCipDataType.Dt).ShouldBeOfType<int>();
    }

    /// <summary>Verifies ParseValue correctly parses 64-bit long integers.</summary>
    [Fact]
    public void ParseValue_LInt_64bit()
    {
        WriteCommand.ParseValue("9223372036854775807", AbCipDataType.LInt).ShouldBe(long.MaxValue);
    }

    /// <summary>Verifies ParseValue respects unsigned type bounds.</summary>
    [Fact]
    public void ParseValue_unsigned_range_respects_bounds()
    {
        WriteCommand.ParseValue("255", AbCipDataType.USInt).ShouldBeOfType<byte>();
        WriteCommand.ParseValue("65535", AbCipDataType.UInt).ShouldBeOfType<ushort>();
        WriteCommand.ParseValue("4294967295", AbCipDataType.UDInt).ShouldBeOfType<uint>();
    }

    /// <summary>Verifies ParseValue correctly parses floating-point with invariant culture.</summary>
    [Fact]
    public void ParseValue_Real_invariant_culture_decimal()
    {
        WriteCommand.ParseValue("3.14", AbCipDataType.Real).ShouldBe(3.14f);
    }

    /// <summary>Verifies ParseValue correctly handles double-precision floating-point.</summary>
    [Fact]
    public void ParseValue_LReal_handles_double_precision()
    {
        WriteCommand.ParseValue("2.718281828", AbCipDataType.LReal).ShouldBeOfType<double>();
    }

    /// <summary>Verifies ParseValue passes through string values unchanged.</summary>
    [Fact]
    public void ParseValue_String_passthrough()
    {
        WriteCommand.ParseValue("hello logix", AbCipDataType.String).ShouldBe("hello logix");
    }

    /// <summary>Verifies ParseValue throws for non-numeric input on numeric types.</summary>
    [Fact]
    public void ParseValue_non_numeric_for_numeric_types_throws_CommandException()
    {
        Should.Throw<CliFx.Exceptions.CommandException>(
            () => WriteCommand.ParseValue("xyz", AbCipDataType.DInt));
    }

    /// <summary>Verifies ParseValue throws for out-of-range numeric values.</summary>
    [Fact]
    public void ParseValue_out_of_range_throws_CommandException()
    {
        // sbyte max is 127; 999 overflows it.
        Should.Throw<CliFx.Exceptions.CommandException>(
            () => WriteCommand.ParseValue("999", AbCipDataType.SInt));
    }

    /// <summary>Verifies ParseValue exception messages include the input and data type for context.</summary>
    /// <param name="raw">Input string to parse.</param>
    /// <param name="type">Data type to parse into.</param>
    [Theory]
    [InlineData("12x", AbCipDataType.Int)]
    [InlineData("3.14", AbCipDataType.DInt)]
    [InlineData("99999999999", AbCipDataType.Int)]
    public void ParseValue_bad_input_CommandException_message_is_actionable(
        string raw, AbCipDataType type)
    {
        var ex = Should.Throw<CliFx.Exceptions.CommandException>(
            () => WriteCommand.ParseValue(raw, type));
        ex.Message.ShouldContain(raw);
        ex.Message.ShouldContain(type.ToString());
    }

    /// <summary>Verifies SynthesiseTagName preserves the tag path verbatim in the output.</summary>
    /// <param name="path">Tag path.</param>
    /// <param name="type">Data type.</param>
    /// <param name="expected">Expected synthesized tag name.</param>
    [Theory]
    [InlineData("Motor01_Speed", AbCipDataType.Real, "Motor01_Speed:Real")]
    [InlineData("Program:Main.Counter", AbCipDataType.DInt, "Program:Main.Counter:DInt")]
    [InlineData("Recipe[3]", AbCipDataType.Int, "Recipe[3]:Int")]
    public void SynthesiseTagName_preserves_path_verbatim(
        string path, AbCipDataType type, string expected)
    {
        ReadCommand.SynthesiseTagName(path, type).ShouldBe(expected);
    }
}
