using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Driver.AbLegacy;
using ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.PlcFamilies;

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

[Trait("Category", "Unit")]
public sealed class AbLegacyDriverTests
{
    [Fact]
    public void DriverType_is_AbLegacy()
    {
        var drv = new AbLegacyDriver(new AbLegacyDriverOptions(), "drv-1");
        drv.DriverType.ShouldBe("AbLegacy");
        drv.DriverInstanceId.ShouldBe("drv-1");
    }

    [Fact]
    public async Task InitializeAsync_with_devices_assigns_family_profiles()
    {
        var drv = new AbLegacyDriver(new AbLegacyDriverOptions
        {
            Devices =
            [
                new AbLegacyDeviceOptions("ab://10.0.0.5/1,0", AbLegacyPlcFamily.Slc500),
                new AbLegacyDeviceOptions("ab://10.0.0.6/", AbLegacyPlcFamily.MicroLogix),
                new AbLegacyDeviceOptions("ab://10.0.0.7/1,0", AbLegacyPlcFamily.Plc5),
            ],
        }, "drv-1");

        await drv.InitializeAsync("{}", CancellationToken.None);

        drv.DeviceCount.ShouldBe(3);
        drv.GetDeviceState("ab://10.0.0.5/1,0")!.Profile.ShouldBe(AbLegacyPlcFamilyProfile.Slc500);
        drv.GetDeviceState("ab://10.0.0.6/")!.Profile.ShouldBe(AbLegacyPlcFamilyProfile.MicroLogix);
        drv.GetDeviceState("ab://10.0.0.7/1,0")!.Profile.ShouldBe(AbLegacyPlcFamilyProfile.Plc5);
    }

    [Fact]
    public async Task InitializeAsync_with_malformed_host_address_faults()
    {
        var drv = new AbLegacyDriver(new AbLegacyDriverOptions
        {
            Devices = [new AbLegacyDeviceOptions("not-a-valid-address")],
        }, "drv-1");

        await Should.ThrowAsync<InvalidOperationException>(
            () => drv.InitializeAsync("{}", CancellationToken.None));
        drv.GetHealth().State.ShouldBe(DriverState.Faulted);
    }

    [Fact]
    public async Task ShutdownAsync_clears_devices()
    {
        var drv = new AbLegacyDriver(new AbLegacyDriverOptions
        {
            Devices = [new AbLegacyDeviceOptions("ab://10.0.0.5/1,0")],
        }, "drv-1");
        await drv.InitializeAsync("{}", CancellationToken.None);

        await drv.ShutdownAsync(CancellationToken.None);
        drv.DeviceCount.ShouldBe(0);
        drv.GetHealth().State.ShouldBe(DriverState.Unknown);
    }

    [Fact]
    public void Family_profiles_expose_expected_defaults()
    {
        AbLegacyPlcFamilyProfile.Slc500.LibplctagPlcAttribute.ShouldBe("slc500");
        AbLegacyPlcFamilyProfile.Slc500.SupportsLongFile.ShouldBeTrue();
        AbLegacyPlcFamilyProfile.Slc500.DefaultCipPath.ShouldBe("1,0");

        AbLegacyPlcFamilyProfile.MicroLogix.DefaultCipPath.ShouldBe("");
        AbLegacyPlcFamilyProfile.MicroLogix.SupportsLongFile.ShouldBeFalse();

        AbLegacyPlcFamilyProfile.Plc5.LibplctagPlcAttribute.ShouldBe("plc5");
        AbLegacyPlcFamilyProfile.Plc5.SupportsLongFile.ShouldBeFalse();

        AbLegacyPlcFamilyProfile.LogixPccc.LibplctagPlcAttribute.ShouldBe("logixpccc");
        AbLegacyPlcFamilyProfile.LogixPccc.SupportsLongFile.ShouldBeTrue();
    }

    [Theory]
    [InlineData(AbLegacyPlcFamily.Slc500, "slc500")]
    [InlineData(AbLegacyPlcFamily.MicroLogix, "micrologix")]
    [InlineData(AbLegacyPlcFamily.Plc5, "plc5")]
    [InlineData(AbLegacyPlcFamily.LogixPccc, "logixpccc")]
    public void ForFamily_dispatches_correctly(AbLegacyPlcFamily family, string expectedAttribute)
    {
        AbLegacyPlcFamilyProfile.ForFamily(family).LibplctagPlcAttribute.ShouldBe(expectedAttribute);
    }

    [Fact]
    public void DataType_mapping_covers_atomic_pccc_types()
    {
        AbLegacyDataType.Bit.ToDriverDataType().ShouldBe(DriverDataType.Boolean);
        AbLegacyDataType.Int.ToDriverDataType().ShouldBe(DriverDataType.Int32);
        AbLegacyDataType.Long.ToDriverDataType().ShouldBe(DriverDataType.Int32);
        AbLegacyDataType.Float.ToDriverDataType().ShouldBe(DriverDataType.Float32);
        AbLegacyDataType.String.ToDriverDataType().ShouldBe(DriverDataType.String);
        AbLegacyDataType.TimerElement.ToDriverDataType().ShouldBe(DriverDataType.Int32);
    }
}
