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

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

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

    /// <summary>Verifies that DiscoverAsync emits pre-declared tags.</summary>
    [Fact]
    public async Task DiscoverAsync_emits_pre_declared_tags()
    {
        var builder = new RecordingBuilder();
        var drv = new FocasDriver(new FocasDriverOptions
        {
            Devices = [new FocasDeviceOptions("focas://10.0.0.5:8193", DeviceName: "Lathe-1")],
            RawTags = FocasRawTags.Entries(
            [
                new FocasTagDefinition("Run", "focas://10.0.0.5:8193", "R100", FocasDataType.Byte),
                new FocasTagDefinition("Alarm", "focas://10.0.0.5:8193", "R200", FocasDataType.Byte, Writable: false),
            ]),
            Probe = new FocasProbeOptions { Enabled = false },
        }, "drv-1", new FakeFocasClientFactory());
        await drv.InitializeAsync("{}", CancellationToken.None);

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

        builder.Folders.ShouldContain(f => f.BrowseName == "FOCAS");
        builder.Folders.ShouldContain(f => f.BrowseName == "focas://10.0.0.5:8193" && f.DisplayName == "Lathe-1");
        // FOCAS is read-only by design — all user tags are ViewOnly regardless of the
        // Writable field, because WireFocasClient.WriteAsync always returns BadNotWritable.
        builder.Variables.Single(v => v.BrowseName == "Run").Info.SecurityClass.ShouldBe(SecurityClassification.ViewOnly);
        builder.Variables.Single(v => v.BrowseName == "Alarm").Info.SecurityClass.ShouldBe(SecurityClassification.ViewOnly);
    }

    // ---- ISubscribable ----

    /// <summary>Verifies that the initial subscription poll raises an OnDataChange event.</summary>
    [Fact]
    public async Task Subscribe_initial_poll_raises_OnDataChange()
    {
        var factory = new FakeFocasClientFactory
        {
            Customise = () => new FakeFocasClient { Values = { ["R100"] = (sbyte)42 } },
        };
        var drv = new FocasDriver(new FocasDriverOptions
        {
            Devices = [new FocasDeviceOptions("focas://10.0.0.5:8193")],
            RawTags = [FocasRawTags.Entry(new FocasTagDefinition("X", "focas://10.0.0.5:8193", "R100", FocasDataType.Byte))],
            Probe = new FocasProbeOptions { 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((sbyte)42);
        await drv.UnsubscribeAsync(handle, CancellationToken.None);
    }

    /// <summary>Verifies that ShutdownAsync cancels active subscriptions.</summary>
    [Fact]
    public async Task ShutdownAsync_cancels_active_subscriptions()
    {
        var factory = new FakeFocasClientFactory
        {
            Customise = () => new FakeFocasClient { Values = { ["R100"] = (sbyte)1 } },
        };
        var drv = new FocasDriver(new FocasDriverOptions
        {
            Devices = [new FocasDeviceOptions("focas://10.0.0.5:8193")],
            RawTags = [FocasRawTags.Entry(new FocasTagDefinition("X", "focas://10.0.0.5:8193", "R100", FocasDataType.Byte))],
            Probe = new FocasProbeOptions { Enabled = false },
        }, "drv-1", factory);
        await drv.InitializeAsync("{}", CancellationToken.None);

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

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

        var afterShutdown = events.Count;
        await Task.Delay(200);
        events.Count.ShouldBe(afterShutdown);
    }

    // ---- IHostConnectivityProbe ----

    /// <summary>Verifies that GetHostStatuses returns one entry per device.</summary>
    [Fact]
    public async Task GetHostStatuses_returns_entry_per_device()
    {
        var drv = new FocasDriver(new FocasDriverOptions
        {
            Devices =
            [
                new FocasDeviceOptions("focas://10.0.0.5:8193"),
                new FocasDeviceOptions("focas://10.0.0.6:8193"),
            ],
            Probe = new FocasProbeOptions { Enabled = false },
        }, "drv-1", new FakeFocasClientFactory());
        await drv.InitializeAsync("{}", CancellationToken.None);

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

    /// <summary>Verifies that the probe transitions to Running on successful connection.</summary>
    [Fact]
    public async Task Probe_transitions_to_Running_on_success()
    {
        var factory = new FakeFocasClientFactory
        {
            Customise = () => new FakeFocasClient { ProbeResult = true },
        };
        var transitions = new ConcurrentQueue<HostStatusChangedEventArgs>();
        var drv = new FocasDriver(new FocasDriverOptions
        {
            Devices = [new FocasDeviceOptions("focas://10.0.0.5:8193")],
            Probe = new FocasProbeOptions
            {
                Enabled = true, Interval = TimeSpan.FromMilliseconds(100),
                Timeout = TimeSpan.FromMilliseconds(50),
            },
        }, "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 the probe transitions to Stopped on connection failure.</summary>
    [Fact]
    public async Task Probe_transitions_to_Stopped_on_failure()
    {
        var factory = new FakeFocasClientFactory
        {
            Customise = () => new FakeFocasClient { ProbeResult = false },
        };
        var transitions = new ConcurrentQueue<HostStatusChangedEventArgs>();
        var drv = new FocasDriver(new FocasDriverOptions
        {
            Devices = [new FocasDeviceOptions("focas://10.0.0.5:8193")],
            Probe = new FocasProbeOptions
            {
                Enabled = true, Interval = TimeSpan.FromMilliseconds(100),
                Timeout = TimeSpan.FromMilliseconds(50),
            },
        }, "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);
    }

    // ---- IPerCallHostResolver ----

    /// <summary>Verifies that ResolveHost returns the declared device for a known tag.</summary>
    [Fact]
    public async Task ResolveHost_returns_declared_device_for_known_tag()
    {
        var drv = new FocasDriver(new FocasDriverOptions
        {
            Devices =
            [
                new FocasDeviceOptions("focas://10.0.0.5:8193"),
                new FocasDeviceOptions("focas://10.0.0.6:8193"),
            ],
            RawTags = FocasRawTags.Entries(
            [
                new FocasTagDefinition("A", "focas://10.0.0.5:8193", "R100", FocasDataType.Byte),
                new FocasTagDefinition("B", "focas://10.0.0.6:8193", "R100", FocasDataType.Byte),
            ]),
            Probe = new FocasProbeOptions { Enabled = false },
        }, "drv-1", new FakeFocasClientFactory());
        await drv.InitializeAsync("{}", CancellationToken.None);

        drv.ResolveHost("A").ShouldBe("focas://10.0.0.5:8193");
        drv.ResolveHost("B").ShouldBe("focas://10.0.0.6:8193");
    }

    /// <summary>Verifies that ResolveHost falls back to the first device for unknown tags.</summary>
    [Fact]
    public async Task ResolveHost_falls_back_to_first_device_for_unknown()
    {
        var drv = new FocasDriver(new FocasDriverOptions
        {
            Devices = [new FocasDeviceOptions("focas://10.0.0.5:8193")],
            Probe = new FocasProbeOptions { Enabled = false },
        }, "drv-1", new FakeFocasClientFactory());
        await drv.InitializeAsync("{}", CancellationToken.None);

        drv.ResolveHost("missing").ShouldBe("focas://10.0.0.5:8193");
    }

    /// <summary>Verifies that ResolveHost falls back to the driver instance ID when no devices are configured.</summary>
    [Fact]
    public async Task ResolveHost_falls_back_to_DriverInstanceId_when_no_devices()
    {
        var drv = new FocasDriver(new FocasDriverOptions(), "drv-1", new FakeFocasClientFactory());
        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);
    }

    /// <summary>Test double that records IAddressSpaceBuilder calls.</summary>
    private sealed class RecordingBuilder : IAddressSpaceBuilder
    {
        /// <summary>Gets the list of recorded folder calls.</summary>
        public List<(string BrowseName, string DisplayName)> Folders { get; } = new();
        /// <summary>Gets the list of recorded variable calls.</summary>
        public List<(string BrowseName, DriverAttributeInfo Info)> Variables { get; } = new();

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

        /// <summary>Records a variable call.</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>
        /// <returns>A variable handle for the recorded variable.</returns>
        public IVariableHandle Variable(string browseName, string displayName, DriverAttributeInfo info)
        { Variables.Add((browseName, info)); return new Handle(info.FullName); }

        /// <summary>Records a property call (no-op).</summary>
        /// <param name="_">The property name (unused).</param>
        /// <param name="__">The property 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.</summary>
            public string FullReference => fullRef;
            /// <summary>Marks as alarm condition.</summary>
            /// <param name="info">The alarm condition information.</param>
            /// <returns>An alarm condition sink.</returns>
            public IAlarmConditionSink MarkAsAlarmCondition(AlarmConditionInfo info) => new NullSink();
        }
        /// <summary>Null alarm condition sink.</summary>
        private sealed class NullSink : IAlarmConditionSink {
            /// <summary>Handles transition (no-op).</summary>
            /// <param name="args">The alarm event arguments (unused).</param>
            public void OnTransition(AlarmEventArgs args) { }
        }
    }
}
