using System.Collections.Concurrent;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Driver.AbLegacy;

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

[Trait("Category", "Unit")]
public sealed class AbLegacyCapabilityTests
{
    // ---- ITagDiscovery ----

    /// <summary>Verifies that DiscoverAsync emits pre-declared tags under the device folder.</summary>
    [Fact]
    public async Task DiscoverAsync_emits_pre_declared_tags_under_device_folder()
    {
        var builder = new RecordingBuilder();
        var drv = new AbLegacyDriver(new AbLegacyDriverOptions
        {
            Devices = [new AbLegacyDeviceOptions("ab://10.0.0.5/1,0", DeviceName: "Press-SLC-1")],
            RawTags = AbLegacyRawTags.Entries(
            [
                new AbLegacyTagDefinition("Speed", "ab://10.0.0.5/1,0", "N7:0", AbLegacyDataType.Int),
                new AbLegacyTagDefinition("Temperature", "ab://10.0.0.5/1,0", "F8:0", AbLegacyDataType.Float, Writable: false),
            ]),
            Probe = new AbLegacyProbeOptions { Enabled = false },
        }, "drv-1");
        await drv.InitializeAsync("{}", CancellationToken.None);

        await drv.DiscoverAsync(builder, CancellationToken.None);

        builder.Folders.ShouldContain(f => f.BrowseName == "AbLegacy");
        builder.Folders.ShouldContain(f => f.BrowseName == "ab://10.0.0.5/1,0" && f.DisplayName == "Press-SLC-1");
        builder.Variables.Count.ShouldBe(2);
        builder.Variables.Single(v => v.BrowseName == "Speed").Info.SecurityClass.ShouldBe(SecurityClassification.Operate);
        builder.Variables.Single(v => v.BrowseName == "Temperature").Info.SecurityClass.ShouldBe(SecurityClassification.ViewOnly);
    }

    // ---- ISubscribable ----

    /// <summary>Verifies that Subscribe initial poll raises OnDataChange.</summary>
    [Fact]
    public async Task Subscribe_initial_poll_raises_OnDataChange()
    {
        var factory = new FakeAbLegacyTagFactory
        {
            Customise = p => new FakeAbLegacyTag(p) { Value = 42 },
        };
        var drv = new AbLegacyDriver(new AbLegacyDriverOptions
        {
            Devices = [new AbLegacyDeviceOptions("ab://10.0.0.5/1,0")],
            RawTags = AbLegacyRawTags.Entries([new AbLegacyTagDefinition("X", "ab://10.0.0.5/1,0", "N7:0", AbLegacyDataType.Int)]),
            Probe = new AbLegacyProbeOptions { Enabled = false },
        }, "drv-1", factory);
        await drv.InitializeAsync("{}", CancellationToken.None);

        var events = new ConcurrentQueue<DataChangeEventArgs>();
        drv.OnDataChange += (_, e) => events.Enqueue(e);

        var handle = await drv.SubscribeAsync(["X"], TimeSpan.FromMilliseconds(200), CancellationToken.None);
        await WaitForAsync(() => events.Count >= 1, TimeSpan.FromSeconds(2));

        events.First().Snapshot.Value.ShouldBe(42);
        await drv.UnsubscribeAsync(handle, CancellationToken.None);
    }

    /// <summary>Verifies that Unsubscribe halts polling.</summary>
    [Fact]
    public async Task Unsubscribe_halts_polling()
    {
        var tagRef = new FakeAbLegacyTag(
            new AbLegacyTagCreateParams("10.0.0.5", 44818, "1,0", "slc500", "N7:0", TimeSpan.FromSeconds(2))) { Value = 1 };
        var factory = new FakeAbLegacyTagFactory { Customise = _ => tagRef };
        var drv = new AbLegacyDriver(new AbLegacyDriverOptions
        {
            Devices = [new AbLegacyDeviceOptions("ab://10.0.0.5/1,0")],
            RawTags = AbLegacyRawTags.Entries([new AbLegacyTagDefinition("X", "ab://10.0.0.5/1,0", "N7:0", AbLegacyDataType.Int)]),
            Probe = new AbLegacyProbeOptions { Enabled = false },
        }, "drv-1", factory);
        await drv.InitializeAsync("{}", CancellationToken.None);

        var events = new ConcurrentQueue<DataChangeEventArgs>();
        drv.OnDataChange += (_, e) => events.Enqueue(e);

        var handle = await drv.SubscribeAsync(["X"], TimeSpan.FromMilliseconds(100), CancellationToken.None);
        await WaitForAsync(() => events.Count >= 1, TimeSpan.FromSeconds(1));
        await drv.UnsubscribeAsync(handle, CancellationToken.None);

        var afterUnsub = events.Count;
        tagRef.Value = 999;
        await Task.Delay(300, TestContext.Current.CancellationToken);
        events.Count.ShouldBe(afterUnsub);
    }

    // ---- IHostConnectivityProbe ----

    /// <summary>Verifies that GetHostStatuses returns one status per device.</summary>
    [Fact]
    public async Task GetHostStatuses_returns_one_per_device()
    {
        var drv = new AbLegacyDriver(new AbLegacyDriverOptions
        {
            Devices =
            [
                new AbLegacyDeviceOptions("ab://10.0.0.5/1,0"),
                new AbLegacyDeviceOptions("ab://10.0.0.6/1,0"),
            ],
            Probe = new AbLegacyProbeOptions { Enabled = false },
        }, "drv-1");
        await drv.InitializeAsync("{}", CancellationToken.None);

        drv.GetHostStatuses().Count.ShouldBe(2);
    }

    /// <summary>Verifies that Probe transitions to Running on successful read.</summary>
    [Fact]
    public async Task Probe_transitions_to_Running_on_successful_read()
    {
        var factory = new FakeAbLegacyTagFactory { Customise = p => new FakeAbLegacyTag(p) };
        var transitions = new ConcurrentQueue<HostStatusChangedEventArgs>();
        var drv = new AbLegacyDriver(new AbLegacyDriverOptions
        {
            Devices = [new AbLegacyDeviceOptions("ab://10.0.0.5/1,0")],
            Probe = new AbLegacyProbeOptions
            {
                Enabled = true, Interval = TimeSpan.FromMilliseconds(100),
                Timeout = TimeSpan.FromMilliseconds(50), ProbeAddress = "S:0",
            },
        }, "drv-1", factory);
        drv.OnHostStatusChanged += (_, e) => transitions.Enqueue(e);

        await drv.InitializeAsync("{}", CancellationToken.None);
        await WaitForAsync(() => transitions.Any(t => t.NewState == HostState.Running), TimeSpan.FromSeconds(2));

        drv.GetHostStatuses().Single().State.ShouldBe(HostState.Running);
        await drv.ShutdownAsync(CancellationToken.None);
    }

    /// <summary>Verifies that Probe transitions to Stopped on read failure.</summary>
    [Fact]
    public async Task Probe_transitions_to_Stopped_on_read_failure()
    {
        var factory = new FakeAbLegacyTagFactory { Customise = p => new FakeAbLegacyTag(p) { ThrowOnRead = true } };
        var transitions = new ConcurrentQueue<HostStatusChangedEventArgs>();
        var drv = new AbLegacyDriver(new AbLegacyDriverOptions
        {
            Devices = [new AbLegacyDeviceOptions("ab://10.0.0.5/1,0")],
            Probe = new AbLegacyProbeOptions
            {
                Enabled = true, Interval = TimeSpan.FromMilliseconds(100),
                Timeout = TimeSpan.FromMilliseconds(50), ProbeAddress = "S:0",
            },
        }, "drv-1", factory);
        drv.OnHostStatusChanged += (_, e) => transitions.Enqueue(e);

        await drv.InitializeAsync("{}", CancellationToken.None);
        await WaitForAsync(() => transitions.Any(t => t.NewState == HostState.Stopped), TimeSpan.FromSeconds(2));

        drv.GetHostStatuses().Single().State.ShouldBe(HostState.Stopped);
        await drv.ShutdownAsync(CancellationToken.None);
    }

    /// <summary>Verifies that Probe is disabled when ProbeAddress is null.</summary>
    [Fact]
    public async Task Probe_disabled_when_ProbeAddress_is_null()
    {
        var drv = new AbLegacyDriver(new AbLegacyDriverOptions
        {
            Devices = [new AbLegacyDeviceOptions("ab://10.0.0.5/1,0")],
            Probe = new AbLegacyProbeOptions { Enabled = true, ProbeAddress = null },
        }, "drv-1");
        await drv.InitializeAsync("{}", CancellationToken.None);
        await Task.Delay(200, TestContext.Current.CancellationToken);

        drv.GetHostStatuses().Single().State.ShouldBe(HostState.Unknown);
        await drv.ShutdownAsync(CancellationToken.None);
    }

    // ---- IPerCallHostResolver ----

    /// <summary>Verifies that ResolveHost returns declared device for known tag.</summary>
    [Fact]
    public async Task ResolveHost_returns_declared_device_for_known_tag()
    {
        var drv = new AbLegacyDriver(new AbLegacyDriverOptions
        {
            Devices =
            [
                new AbLegacyDeviceOptions("ab://10.0.0.5/1,0"),
                new AbLegacyDeviceOptions("ab://10.0.0.6/1,0"),
            ],
            RawTags = AbLegacyRawTags.Entries(
            [
                new AbLegacyTagDefinition("A", "ab://10.0.0.5/1,0", "N7:0", AbLegacyDataType.Int),
                new AbLegacyTagDefinition("B", "ab://10.0.0.6/1,0", "N7:0", AbLegacyDataType.Int),
            ]),
            Probe = new AbLegacyProbeOptions { Enabled = false },
        }, "drv-1");
        await drv.InitializeAsync("{}", CancellationToken.None);

        drv.ResolveHost("A").ShouldBe("ab://10.0.0.5/1,0");
        drv.ResolveHost("B").ShouldBe("ab://10.0.0.6/1,0");
    }

    /// <summary>Verifies that ResolveHost falls back to first device for unknown tags.</summary>
    [Fact]
    public async Task ResolveHost_falls_back_to_first_device_for_unknown()
    {
        var drv = new AbLegacyDriver(new AbLegacyDriverOptions
        {
            Devices = [new AbLegacyDeviceOptions("ab://10.0.0.5/1,0")],
            Probe = new AbLegacyProbeOptions { Enabled = false },
        }, "drv-1");
        await drv.InitializeAsync("{}", CancellationToken.None);

        drv.ResolveHost("missing").ShouldBe("ab://10.0.0.5/1,0");
    }

    /// <summary>Verifies that ResolveHost falls back to DriverInstanceId when no devices exist.</summary>
    [Fact]
    public async Task ResolveHost_falls_back_to_DriverInstanceId_when_no_devices()
    {
        var drv = new AbLegacyDriver(new AbLegacyDriverOptions(), "drv-1");
        await drv.InitializeAsync("{}", CancellationToken.None);

        drv.ResolveHost("anything").ShouldBe("drv-1");
    }

    // ---- helpers ----

    private static async Task WaitForAsync(Func<bool> condition, TimeSpan timeout)
    {
        var deadline = DateTime.UtcNow + timeout;
        while (!condition() && DateTime.UtcNow < deadline)
            await Task.Delay(20);
    }

    private sealed class RecordingBuilder : IAddressSpaceBuilder
    {
        /// <summary>Gets list of folders created during discovery.</summary>
        public List<(string BrowseName, string DisplayName)> Folders { get; } = new();
        /// <summary>Gets list of variables created during discovery.</summary>
        public List<(string BrowseName, DriverAttributeInfo Info)> Variables { get; } = new();

        /// <summary>Records folder creation.</summary>
        /// <param name="browseName">The browse name of the folder.</param>
        /// <param name="displayName">The display name of the folder.</param>
        public IAddressSpaceBuilder Folder(string browseName, string displayName)
        { Folders.Add((browseName, displayName)); return this; }

        /// <summary>Records variable creation.</summary>
        /// <param name="browseName">The browse name of the variable.</param>
        /// <param name="displayName">The display name of the variable.</param>
        /// <param name="info">The driver attribute information.</param>
        public IVariableHandle Variable(string browseName, string displayName, DriverAttributeInfo info)
        { Variables.Add((browseName, info)); return new Handle(info.FullName); }

        /// <summary>Records property addition (stub implementation).</summary>
        /// <param name="_">The property name (unused).</param>
        /// <param name="__">The data type (unused).</param>
        /// <param name="___">The property value (unused).</param>
        public void AddProperty(string _, DriverDataType __, object? ___) { }

        private sealed class Handle(string fullRef) : IVariableHandle
        {
            /// <summary>Gets the full reference of the variable.</summary>
            public string FullReference => fullRef;
            /// <summary>Marks the variable as an alarm condition.</summary>
            /// <param name="info">The alarm condition information.</param>
            public IAlarmConditionSink MarkAsAlarmCondition(AlarmConditionInfo info) => new NullSink();
        }
        /// <summary>Null sink for alarm condition transitions.</summary>
        private sealed class NullSink : IAlarmConditionSink
        {
            /// <inheritdoc />
            public void OnTransition(AlarmEventArgs args) { }
        }
    }
}
