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

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

[Trait("Category", "Unit")]
public sealed class AbCipHostAddressTests
{
    /// <summary>Verifies that TryParse accepts valid address forms.</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="cipPath">The expected CIP path.</param>
    [Theory]
    [InlineData("ab://10.0.0.5/1,0", "10.0.0.5", 44818, "1,0")]
    [InlineData("ab://10.0.0.5/1,4", "10.0.0.5", 44818, "1,4")]
    [InlineData("ab://10.0.0.5/1,2,2,192.168.50.20,1,0", "10.0.0.5", 44818, "1,2,2,192.168.50.20,1,0")]
    [InlineData("ab://10.0.0.5/", "10.0.0.5", 44818, "")]
    [InlineData("ab://plc-01.factory.internal/1,0", "plc-01.factory.internal", 44818, "1,0")]
    [InlineData("ab://10.0.0.5:44818/1,0", "10.0.0.5", 44818, "1,0")]
    [InlineData("ab://10.0.0.5:2222/1,0", "10.0.0.5", 2222, "1,0")]
    [InlineData("AB://10.0.0.5/1,0", "10.0.0.5", 44818, "1,0")] // case-insensitive scheme
    public void TryParse_accepts_valid_forms(string input, string gateway, int port, string cipPath)
    {
        var parsed = AbCipHostAddress.TryParse(input);
        parsed.ShouldNotBeNull();
        parsed.Gateway.ShouldBe(gateway);
        parsed.Port.ShouldBe(port);
        parsed.CipPath.ShouldBe(cipPath);
    }

    /// <summary>Verifies that TryParse rejects invalid address forms.</summary>
    /// <param name="input">The invalid or null URI string to test.</param>
    [Theory]
    [InlineData(null)]
    [InlineData("")]
    [InlineData("   ")]
    [InlineData("http://10.0.0.5/1,0")]         // wrong scheme
    [InlineData("ab:10.0.0.5/1,0")]             // missing //
    [InlineData("ab://10.0.0.5")]               // no path slash
    [InlineData("ab:///1,0")]                   // no gateway
    [InlineData("ab://10.0.0.5:0/1,0")]         // invalid port
    [InlineData("ab://10.0.0.5:65536/1,0")]     // port out of range
    [InlineData("ab://10.0.0.5:abc/1,0")]       // non-numeric port
    public void TryParse_rejects_invalid_forms(string? input)
    {
        AbCipHostAddress.TryParse(input).ShouldBeNull();
    }

    /// <summary>Verifies that ToString canonicalises the address format.</summary>
    /// <param name="gateway">The gateway host component.</param>
    /// <param name="port">The port number.</param>
    /// <param name="path">The CIP path component.</param>
    /// <param name="expected">The expected canonical string representation.</param>
    [Theory]
    [InlineData("10.0.0.5", 44818, "1,0", "ab://10.0.0.5/1,0")]
    [InlineData("10.0.0.5", 2222, "1,0", "ab://10.0.0.5:2222/1,0")]
    [InlineData("10.0.0.5", 44818, "", "ab://10.0.0.5/")]
    public void ToString_canonicalises(string gateway, int port, string path, string expected)
    {
        var addr = new AbCipHostAddress(gateway, port, path);
        addr.ToString().ShouldBe(expected);
    }

    /// <summary>Verifies that round-trip parsing and formatting is stable.</summary>
    [Fact]
    public void RoundTrip_is_stable()
    {
        const string input = "ab://plc-01:44818/1,2,2,10.0.0.10,1,0";
        var parsed = AbCipHostAddress.TryParse(input)!;
        // Default port is stripped in canonical form; explicit 44818 → becomes default form.
        parsed.ToString().ShouldBe("ab://plc-01/1,2,2,10.0.0.10,1,0");

        var parsedAgain = AbCipHostAddress.TryParse(parsed.ToString())!;
        parsedAgain.ShouldBe(parsed);
    }
}
