using libplctag;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Driver.AbLegacy;

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

[Trait("Category", "Unit")]
public sealed class AbLegacyHostAndStatusTests
{
    /// <summary>Verifies that HostAddress parses valid URI strings correctly.</summary>
    /// <param name="input">The raw URI string to parse.</param>
    /// <param name="gateway">The expected gateway host.</param>
    /// <param name="port">The expected port number.</param>
    /// <param name="path">The expected CIP path component.</param>
    [Theory]
    [InlineData("ab://10.0.0.5/1,0", "10.0.0.5", 44818, "1,0")]
    [InlineData("ab://10.0.0.5/", "10.0.0.5", 44818, "")]
    [InlineData("ab://10.0.0.5:2222/1,0", "10.0.0.5", 2222, "1,0")]
    [InlineData("ab://plc-slc.factory/1,2", "plc-slc.factory", 44818, "1,2")]
    public void HostAddress_parses_valid(string input, string gateway, int port, string path)
    {
        var parsed = AbLegacyHostAddress.TryParse(input);
        parsed.ShouldNotBeNull();
        parsed.Gateway.ShouldBe(gateway);
        parsed.Port.ShouldBe(port);
        parsed.CipPath.ShouldBe(path);
    }

    /// <summary>Verifies that HostAddress rejects invalid URI strings.</summary>
    /// <param name="input">The invalid or null URI string to test.</param>
    [Theory]
    [InlineData(null)]
    [InlineData("http://10.0.0.5/1,0")]
    [InlineData("ab://10.0.0.5")]
    [InlineData("ab:///1,0")]
    [InlineData("ab://10.0.0.5:0/1,0")]
    public void HostAddress_rejects_invalid(string? input)
    {
        AbLegacyHostAddress.TryParse(input).ShouldBeNull();
    }

    /// <summary>Verifies that HostAddress.ToString produces canonical URI format.</summary>
    [Fact]
    public void HostAddress_ToString_canonicalises()
    {
        new AbLegacyHostAddress("10.0.0.5", 44818, "1,0").ToString().ShouldBe("ab://10.0.0.5/1,0");
        new AbLegacyHostAddress("10.0.0.5", 2222, "1,0").ToString().ShouldBe("ab://10.0.0.5:2222/1,0");
    }

    /// <summary>Verifies that PCCC status codes are mapped to OPC UA status codes correctly.</summary>
    /// <param name="sts">The PCCC status byte to map.</param>
    /// <param name="expected">The expected OPC UA status code.</param>
    [Theory]
    [InlineData((byte)0x00, AbLegacyStatusMapper.Good)]
    [InlineData((byte)0x10, AbLegacyStatusMapper.BadNotSupported)]
    [InlineData((byte)0x20, AbLegacyStatusMapper.BadNodeIdUnknown)]
    [InlineData((byte)0x30, AbLegacyStatusMapper.BadNotWritable)]
    [InlineData((byte)0x40, AbLegacyStatusMapper.BadDeviceFailure)]
    [InlineData((byte)0x50, AbLegacyStatusMapper.BadDeviceFailure)]
    [InlineData((byte)0xF0, AbLegacyStatusMapper.BadInternalError)]
    [InlineData((byte)0xFF, AbLegacyStatusMapper.BadCommunicationError)]
    public void PcccStatus_maps_known_codes(byte sts, uint expected)
    {
        AbLegacyStatusMapper.MapPcccStatus(sts).ShouldBe(expected);
    }

    /// <summary>Verifies that libplctag Status enum members are mapped to OPC UA status codes correctly.</summary>
    /// <param name="status">The libplctag Status enum value to map.</param>
    /// <param name="expected">The expected OPC UA status code.</param>
    // Driver.AbLegacy-010 — tests use the libplctag.NET Status enum members (what
    // (int)Tag.GetStatus() actually returns) rather than the unverified magic integers
    // that predated this fix (-5/-7/-14/-16/-17 matched neither native PLCTAG_ERR_*
    // constants nor the .NET wrapper enum ordinals reliably).
    [Theory]
    [InlineData(Status.Ok, AbLegacyStatusMapper.Good)]
    [InlineData(Status.Pending, AbLegacyStatusMapper.GoodMoreData)]
    [InlineData(Status.ErrorTimeout, AbLegacyStatusMapper.BadTimeout)]
    [InlineData(Status.ErrorNotFound, AbLegacyStatusMapper.BadNodeIdUnknown)]
    [InlineData(Status.ErrorNoMatch, AbLegacyStatusMapper.BadNodeIdUnknown)]
    [InlineData(Status.ErrorNotAllowed, AbLegacyStatusMapper.BadNotWritable)]
    [InlineData(Status.ErrorOutOfBounds, AbLegacyStatusMapper.BadOutOfRange)]
    [InlineData(Status.ErrorTooLarge, AbLegacyStatusMapper.BadOutOfRange)]
    [InlineData(Status.ErrorBadConnection, AbLegacyStatusMapper.BadCommunicationError)]
    [InlineData(Status.ErrorBadGateway, AbLegacyStatusMapper.BadCommunicationError)]
    [InlineData(Status.ErrorUnsupported, AbLegacyStatusMapper.BadNotSupported)]
    [InlineData(Status.ErrorNoMem, AbLegacyStatusMapper.BadCommunicationError)] // unmapped → generic comms
    public void LibplctagStatus_maps_real_enum_members(Status status, uint expected)
    {
        AbLegacyStatusMapper.MapLibplctagStatus(status).ShouldBe(expected);
        // The int overload must agree — it is the seam IAbLegacyTagRuntime.GetStatus() drives.
        AbLegacyStatusMapper.MapLibplctagStatus((int)status).ShouldBe(expected);
    }

    /// <summary>Verifies that timeout errors are distinguished from generic communication errors.</summary>
    [Fact]
    public void MapLibplctagStatus_distinguishes_timeout_from_generic_comms_error()
    {
        // Regression for Driver.AbLegacy-010: timeout must not fall through to
        // BadCommunicationError the way the old magic-integer switch did.
        AbLegacyStatusMapper.MapLibplctagStatus((int)Status.ErrorTimeout)
            .ShouldBe(AbLegacyStatusMapper.BadTimeout);
        AbLegacyStatusMapper.MapLibplctagStatus((int)Status.ErrorNotFound)
            .ShouldBe(AbLegacyStatusMapper.BadNodeIdUnknown);
    }
}
