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
{
    [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);
    }

    [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();
    }

    [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");
    }

    [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);
    }

    [Theory]
    [InlineData(0, AbLegacyStatusMapper.Good)]
    [InlineData(1, AbLegacyStatusMapper.GoodMoreData)]
    [InlineData(-5, AbLegacyStatusMapper.BadTimeout)]
    [InlineData(-7, AbLegacyStatusMapper.BadCommunicationError)]
    [InlineData(-14, AbLegacyStatusMapper.BadNodeIdUnknown)]
    [InlineData(-16, AbLegacyStatusMapper.BadNotWritable)]
    [InlineData(-17, AbLegacyStatusMapper.BadOutOfRange)]
    public void LibplctagStatus_maps_known_codes(int status, uint expected)
    {
        AbLegacyStatusMapper.MapLibplctagStatus(status).ShouldBe(expected);
    }
}
