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

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

/// <summary>
///     Covers the <c>--value</c> string → CLR type parser inside
///     <see cref="WriteCommand.ParseValue"/>. This is the piece that guards against
///     locale surprises (e.g. comma-as-decimal-separator on PL locales), so all numeric
///     paths assert the invariant-culture path.
/// </summary>
[Trait("Category", "Unit")]
public sealed class WriteCommandParseValueTests
{
    /// <summary>Verifies that boolean parsing accepts multiple aliases for true and false.</summary>
    /// <param name="raw">The raw string value to parse.</param>
    /// <param name="expected">The expected boolean result.</param>
    [Theory]
    [InlineData("true", true)]
    [InlineData("false", false)]
    [InlineData("1", true)]
    [InlineData("0", false)]
    [InlineData("YES", true)]
    [InlineData("No", false)]
    [InlineData("on", true)]
    [InlineData("off", false)]
    public void ParseValue_Bool_accepts_common_aliases(string raw, bool expected)
    {
        WriteCommand.ParseValue(raw, ModbusDataType.Bool).ShouldBe(expected);
    }

    /// <summary>Verifies that boolean parsing rejects unrecognized string values.</summary>
    [Fact]
    public void ParseValue_Bool_rejects_unknown_strings()
    {
        Should.Throw<CliFx.Exceptions.CommandException>(
            () => WriteCommand.ParseValue("maybe", ModbusDataType.Bool));
    }

    /// <summary>Verifies that Int16 parsing handles positive and negative values correctly.</summary>
    [Fact]
    public void ParseValue_Int16_parses_positive_and_negative()
    {
        WriteCommand.ParseValue("-32768", ModbusDataType.Int16).ShouldBe((short)-32768);
        WriteCommand.ParseValue("32767", ModbusDataType.Int16).ShouldBe((short)32767);
    }

    /// <summary>Verifies that both UInt16 and Bcd16 parsing return ushort type values.</summary>
    [Fact]
    public void ParseValue_UInt16_and_Bcd16_both_yield_ushort()
    {
        WriteCommand.ParseValue("65535", ModbusDataType.UInt16).ShouldBeOfType<ushort>();
        WriteCommand.ParseValue("65535", ModbusDataType.Bcd16).ShouldBeOfType<ushort>();
    }

    /// <summary>Verifies that Float32 parsing uses invariant culture with period as decimal separator.</summary>
    [Fact]
    public void ParseValue_Float32_uses_invariant_culture_period_as_decimal_separator()
    {
        WriteCommand.ParseValue("3.14", ModbusDataType.Float32).ShouldBe(3.14f);
    }

    /// <summary>Verifies that Float64 parsing maintains precision for larger decimal values.</summary>
    [Fact]
    public void ParseValue_Float64_handles_larger_precision()
    {
        var result = WriteCommand.ParseValue("2.718281828", ModbusDataType.Float64);
        result.ShouldBeOfType<double>();
        ((double)result).ShouldBe(2.718281828d, 0.0000001d);
    }

    /// <summary>Verifies that String parsing returns the raw input without modification.</summary>
    [Fact]
    public void ParseValue_String_returns_raw_string_unmodified()
    {
        WriteCommand.ParseValue("hello world", ModbusDataType.String).ShouldBe("hello world");
    }

    /// <summary>Verifies that BitInRegister parsing accepts boolean aliases.</summary>
    [Fact]
    public void ParseValue_BitInRegister_accepts_bool_aliases()
    {
        WriteCommand.ParseValue("true", ModbusDataType.BitInRegister).ShouldBe(true);
        WriteCommand.ParseValue("0", ModbusDataType.BitInRegister).ShouldBe(false);
    }

    /// <summary>Verifies that Int32 parsing handles negative maximum values correctly.</summary>
    [Fact]
    public void ParseValue_Int32_parses_negative_max()
    {
        WriteCommand.ParseValue("-2147483648", ModbusDataType.Int32).ShouldBe(int.MinValue);
    }

    /// <summary>Verifies that parsing rejects non-numeric strings for numeric data types.</summary>
    [Fact]
    public void ParseValue_rejects_non_numeric_for_numeric_types()
    {
        Should.Throw<FormatException>(
            () => WriteCommand.ParseValue("not-a-number", ModbusDataType.Int32));
    }
}
