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

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

/// <summary>
///     Covers <see cref="WriteCommand.ParseValue"/>. PCCC types are narrower than AB CIP
///     (no 64-bit, no unsigned variants, no Structure / Dt) so the matrix is smaller.
/// </summary>
[Trait("Category", "Unit")]
public sealed class WriteCommandParseValueTests
{
    /// <summary>Verifies that bit values accept common aliases like "true", "yes", "0", and "OFF".</summary>
    /// <param name="raw">The raw string value to parse.</param>
    /// <param name="expected">The expected boolean result.</param>
    [Theory]
    [InlineData("true", true)]
    [InlineData("0", false)]
    [InlineData("yes", true)]
    [InlineData("OFF", false)]
    public void ParseValue_Bit_accepts_common_aliases(string raw, bool expected)
    {
        WriteCommand.ParseValue(raw, AbLegacyDataType.Bit).ShouldBe(expected);
    }

    /// <summary>Verifies that signed 16-bit integer values are parsed correctly within range.</summary>
    [Fact]
    public void ParseValue_Int_signed_16bit()
    {
        WriteCommand.ParseValue("-32768", AbLegacyDataType.Int).ShouldBe((short)-32768);
        WriteCommand.ParseValue("32767", AbLegacyDataType.Int).ShouldBe((short)32767);
    }

    /// <summary>Verifies that AnalogInt values parse using the same semantics as Int.</summary>
    [Fact]
    public void ParseValue_AnalogInt_parses_same_as_Int()
    {
        // A-file uses N-file semantics — 16-bit signed with the same wire format.
        WriteCommand.ParseValue("100", AbLegacyDataType.AnalogInt).ShouldBeOfType<short>();
    }

    /// <summary>Verifies that 32-bit integer values are parsed correctly within range.</summary>
    [Fact]
    public void ParseValue_Long_32bit()
    {
        WriteCommand.ParseValue("-2147483648", AbLegacyDataType.Long).ShouldBe(int.MinValue);
        WriteCommand.ParseValue("2147483647", AbLegacyDataType.Long).ShouldBe(int.MaxValue);
    }

    /// <summary>Verifies that float values are parsed using invariant culture.</summary>
    [Fact]
    public void ParseValue_Float_invariant_culture()
    {
        WriteCommand.ParseValue("3.14", AbLegacyDataType.Float).ShouldBe(3.14f);
    }

    /// <summary>Verifies that string values are returned unchanged.</summary>
    [Fact]
    public void ParseValue_String_passthrough()
    {
        WriteCommand.ParseValue("hello slc", AbLegacyDataType.String).ShouldBe("hello slc");
    }

    /// <summary>Verifies that Timer, Counter, and Control element types parse as 32-bit integers.</summary>
    /// <param name="type">The AB Legacy data type to test.</param>
    [Theory]
    [InlineData(AbLegacyDataType.TimerElement)]
    [InlineData(AbLegacyDataType.CounterElement)]
    [InlineData(AbLegacyDataType.ControlElement)]
    public void ParseValue_Element_types_land_on_int32(AbLegacyDataType type)
    {
        // T/C/R sub-elements are 32-bit at the wire level regardless of semantic meaning.
        WriteCommand.ParseValue("42", type).ShouldBeOfType<int>();
    }

    /// <summary>Verifies that unknown string values are rejected for bit type.</summary>
    [Fact]
    public void ParseValue_Bit_rejects_unknown_strings()
    {
        Should.Throw<CliFx.Exceptions.CommandException>(
            () => WriteCommand.ParseValue("perhaps", AbLegacyDataType.Bit));
    }

    /// <summary>Verifies that non-numeric values throw CommandException for numeric types.</summary>
    [Fact]
    public void ParseValue_non_numeric_for_numeric_types_throws_CommandException()
    {
        // Bad input must surface as CommandException (clean one-line error), not a raw FormatException.
        Should.Throw<CliFx.Exceptions.CommandException>(
            () => WriteCommand.ParseValue("xyz", AbLegacyDataType.Int));
    }

    /// <summary>Verifies that out-of-range values throw CommandException.</summary>
    /// <param name="raw">The raw string value that is out of range.</param>
    /// <param name="type">The AB Legacy data type to test.</param>
    [Theory]
    [InlineData("99999", AbLegacyDataType.Int)]      // short range is ±32767
    [InlineData("-99999", AbLegacyDataType.Int)]
    [InlineData("-99999", AbLegacyDataType.AnalogInt)]
    public void ParseValue_out_of_range_throws_CommandException(string raw, AbLegacyDataType type)
    {
        // Out-of-range values must surface as CommandException, not OverflowException.
        Should.Throw<CliFx.Exceptions.CommandException>(
            () => WriteCommand.ParseValue(raw, type));
    }

    /// <summary>Verifies that PCCC addresses are preserved verbatim in synthesized tag names.</summary>
    /// <param name="address">The PCCC address string to test.</param>
    /// <param name="type">The AB Legacy data type.</param>
    /// <param name="expected">The expected synthesized tag name.</param>
    [Theory]
    [InlineData("N7:0", AbLegacyDataType.Int, "N7:0:Int")]
    [InlineData("B3:0/3", AbLegacyDataType.Bit, "B3:0/3:Bit")]
    [InlineData("F8:10", AbLegacyDataType.Float, "F8:10:Float")]
    [InlineData("T4:0.ACC", AbLegacyDataType.TimerElement, "T4:0.ACC:TimerElement")]
    public void SynthesiseTagName_preserves_PCCC_address_verbatim(
        string address, AbLegacyDataType type, string expected)
    {
        ReadCommand.SynthesiseTagName(address, type).ShouldBe(expected);
    }
}
