using System.Runtime.CompilerServices;
using ZB.MOM.WW.OtOpcUa.Driver.TwinCAT;

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

internal class FakeTwinCATClient : ITwinCATClient
{
    /// <summary>Gets a value indicating whether the client is connected.</summary>
    public bool IsConnected { get; private set; }
    /// <summary>Gets the number of times Connect has been called.</summary>
    public int ConnectCount { get; private set; }
    /// <summary>Gets the number of times Dispose has been called.</summary>
    public int DisposeCount { get; private set; }
    /// <summary>Gets or sets a value indicating whether ConnectAsync should throw.</summary>
    public bool ThrowOnConnect { get; set; }
    /// <summary>Gets or sets a value indicating whether ReadValueAsync should throw.</summary>
    public bool ThrowOnRead { get; set; }
    /// <summary>Gets or sets a value indicating whether WriteValueAsync should throw.</summary>
    public bool ThrowOnWrite { get; set; }
    /// <summary>Gets or sets a value indicating whether ProbeAsync should throw.</summary>
    public bool ThrowOnProbe { get; set; }
    /// <summary>Gets or sets the exception to throw when a throw flag is set.</summary>
    public Exception? Exception { get; set; }
    /// <summary>Gets the simulated values by symbol path.</summary>
    public Dictionary<string, object?> Values { get; } = new(StringComparer.OrdinalIgnoreCase);
    /// <summary>Gets the read statuses by symbol path.</summary>
    public Dictionary<string, uint> ReadStatuses { get; } = new(StringComparer.OrdinalIgnoreCase);
    /// <summary>Gets the write statuses by symbol path.</summary>
    public Dictionary<string, uint> WriteStatuses { get; } = new(StringComparer.OrdinalIgnoreCase);
    /// <summary>Records the <c>arrayCount</c> argument of the most recent <see cref="ReadValueAsync"/>
    ///     call (null = scalar read) so Phase 4c array-read tests can assert the driver requested
    ///     the array with the authored length.</summary>
    public int? LastReadArrayCount { get; private set; }
    /// <summary>Gets the log of all write operations.</summary>
    public List<(string symbol, TwinCATDataType type, int? bit, object? value)> WriteLog { get; } = new();
    /// <summary>Gets or sets the result returned by ProbeAsync.</summary>
    public bool ProbeResult { get; set; } = true;

    /// <summary>Occurs when the symbol version changes.</summary>
    public event EventHandler? OnSymbolVersionChanged;

    /// <summary>Test hook — fire the symbol-version-changed signal as the real client would.</summary>
    public void FireSymbolVersionChanged() => OnSymbolVersionChanged?.Invoke(this, EventArgs.Empty);

    /// <summary>Test hook — simulate a wire-level drop that leaves the AMS port "connected" but dead:
    ///     flips <see cref="IsConnected"/> to false without disposing, so the driver's IsConnected
    ///     fast-path treats the client as stale and rebuilds (exercises the STAB-2 reconnect+replay).</summary>
    public void SimulateWireDrop() => IsConnected = false;

    /// <summary>Simulates connecting to the TwinCAT system.</summary>
    /// <param name="address">The AMS address to connect to.</param>
    /// <param name="timeout">The connection timeout.</param>
    /// <param name="ct">The cancellation token.</param>
    /// <returns>A task that completes when the connection succeeds or fails.</returns>
    public virtual Task ConnectAsync(TwinCATAmsAddress address, TimeSpan timeout, CancellationToken ct)
    {
        ConnectCount++;
        if (ThrowOnConnect) throw Exception ?? new InvalidOperationException();
        IsConnected = true;
        return Task.CompletedTask;
    }

    /// <summary>Simulates reading a value from a symbol.</summary>
    /// <param name="symbolPath">The path to the symbol to read.</param>
    /// <param name="type">The data type of the symbol.</param>
    /// <param name="bitIndex">The optional bit index for bit-level reads.</param>
    /// <param name="arrayCount">The optional 1-D array element count (null = scalar read).</param>
    /// <param name="ct">The cancellation token.</param>
    /// <returns>A task that returns the simulated value and status.</returns>
    public virtual Task<(object? value, uint status)> ReadValueAsync(
        string symbolPath, TwinCATDataType type, int? bitIndex, int? arrayCount, CancellationToken ct)
    {
        if (ThrowOnRead) throw Exception ?? new InvalidOperationException();
        LastReadArrayCount = arrayCount;
        var status = ReadStatuses.TryGetValue(symbolPath, out var s) ? s : TwinCATStatusMapper.Good;
        var value = Values.TryGetValue(symbolPath, out var v) ? v : null;
        return Task.FromResult((value, status));
    }

    /// <summary>Simulates writing a value to a symbol.</summary>
    /// <param name="symbolPath">The path to the symbol to write.</param>
    /// <param name="type">The data type of the symbol.</param>
    /// <param name="bitIndex">The optional bit index for bit-level writes.</param>
    /// <param name="value">The value to write.</param>
    /// <param name="ct">The cancellation token.</param>
    /// <returns>A task that returns the write status.</returns>
    public virtual Task<uint> WriteValueAsync(
        string symbolPath, TwinCATDataType type, int? bitIndex, object? value, CancellationToken ct)
    {
        if (ThrowOnWrite) throw Exception ?? new InvalidOperationException();
        WriteLog.Add((symbolPath, type, bitIndex, value));
        Values[symbolPath] = value;
        var status = WriteStatuses.TryGetValue(symbolPath, out var s) ? s : TwinCATStatusMapper.Good;
        return Task.FromResult(status);
    }

    /// <summary>Simulates probing the connection status.</summary>
    /// <param name="ct">The cancellation token.</param>
    /// <returns>A task that returns the probe result.</returns>
    public virtual Task<bool> ProbeAsync(CancellationToken ct)
    {
        if (ThrowOnProbe) return Task.FromResult(false);
        return Task.FromResult(ProbeResult);
    }

    /// <summary>Releases unmanaged resources.</summary>
    public virtual void Dispose()
    {
        DisposeCount++;
        IsConnected = false;
    }

    // ---- notification fake ----

    /// <summary>Gets the list of registered notifications.</summary>
    public List<FakeNotification> Notifications { get; } = new();
    /// <summary>Gets or sets a value indicating whether AddNotificationAsync should throw.</summary>
    public bool ThrowOnAddNotification { get; set; }
    /// <summary>Records the most recently-supplied <c>maxDelayMs</c> for Driver.TwinCAT-014 tests.</summary>
    public int LastMaxDelayMs { get; private set; }
    /// <summary>When set, <see cref="AddNotificationAsync"/> awaits this gate before creating the handle —
    ///     lets a test wedge a replay/register mid-flight (STAB-17 orphan race).</summary>
    public TaskCompletionSource? AddNotificationGate { get; set; }

    /// <summary>Simulates adding a notification for value changes.</summary>
    /// <param name="symbolPath">The path to the symbol to watch.</param>
    /// <param name="type">The data type of the symbol.</param>
    /// <param name="bitIndex">The optional bit index for bit-level notifications.</param>
    /// <param name="cycleTime">The sampling cycle time.</param>
    /// <param name="maxDelayMs">The maximum delay in milliseconds.</param>
    /// <param name="onChange">The callback to invoke on value change.</param>
    /// <param name="cancellationToken">The cancellation token.</param>
    /// <returns>A task that returns a notification handle.</returns>
    public virtual async Task<ITwinCATNotificationHandle> AddNotificationAsync(
        string symbolPath, TwinCATDataType type, int? bitIndex, TimeSpan cycleTime,
        int maxDelayMs, Action<string, object?> onChange, CancellationToken cancellationToken)
    {
        if (ThrowOnAddNotification)
            throw Exception ?? new InvalidOperationException("fake AddNotification failure");

        if (AddNotificationGate is { } gate)
            await gate.Task.ConfigureAwait(false);

        LastMaxDelayMs = maxDelayMs;
        var reg = new FakeNotification(symbolPath, type, bitIndex, onChange, this);
        Notifications.Add(reg);
        return reg;
    }

    /// <summary>Fire a change event through the registered callback for <paramref name="symbolPath"/>.</summary>
    /// <param name="symbolPath">The symbol path for which to fire the change.</param>
    /// <param name="value">The new value to pass to the callback.</param>
    public void FireNotification(string symbolPath, object? value)
    {
        foreach (var n in Notifications)
            if (!n.Disposed && string.Equals(n.SymbolPath, symbolPath, StringComparison.OrdinalIgnoreCase))
                n.OnChange(symbolPath, value);
    }

    // ---- symbol browser fake ----

    /// <summary>Gets the simulated browse results.</summary>
    public List<TwinCATDiscoveredSymbol> BrowseResults { get; } = new();
    /// <summary>Gets or sets a value indicating whether BrowseSymbolsAsync should throw.</summary>
    public bool ThrowOnBrowse { get; set; }

    /// <summary>Simulates browsing the symbol tree.</summary>
    /// <param name="cancellationToken">The cancellation token.</param>
    /// <returns>An async enumerable of discovered symbols.</returns>
    public virtual async IAsyncEnumerable<TwinCATDiscoveredSymbol> BrowseSymbolsAsync(
        [EnumeratorCancellation] CancellationToken cancellationToken)
    {
        if (ThrowOnBrowse) throw Exception ?? new InvalidOperationException("fake browse failure");
        await Task.CompletedTask;
        foreach (var sym in BrowseResults)
        {
            if (cancellationToken.IsCancellationRequested) yield break;
            yield return sym;
        }
    }

    /// <summary>Represents a registered notification in the fake client.</summary>
    public sealed class FakeNotification(
        string symbolPath, TwinCATDataType type, int? bitIndex,
        Action<string, object?> onChange, FakeTwinCATClient owner) : ITwinCATNotificationHandle
    {
        /// <summary>Gets the symbol path being watched.</summary>
        public string SymbolPath { get; } = symbolPath;
        /// <summary>Gets the data type of the symbol.</summary>
        public TwinCATDataType Type { get; } = type;
        /// <summary>Gets the optional bit index.</summary>
        public int? BitIndex { get; } = bitIndex;
        /// <summary>Gets the callback to invoke on value change.</summary>
        public Action<string, object?> OnChange { get; } = onChange;
        /// <summary>Gets a value indicating whether this notification has been disposed.</summary>
        public bool Disposed { get; private set; }

        /// <summary>Disposes this notification handle.</summary>
        public void Dispose()
        {
            Disposed = true;
            owner.Notifications.Remove(this);
        }
    }
}

/// <summary>Represents a factory for creating fake TwinCAT clients.</summary>
internal sealed class FakeTwinCATClientFactory : ITwinCATClientFactory
{
    /// <summary>Gets the list of clients created by this factory.</summary>
    public List<FakeTwinCATClient> Clients { get; } = new();
    /// <summary>Gets or sets an optional customization function for creating clients.</summary>
    public Func<FakeTwinCATClient>? Customise { get; set; }

    /// <summary>Creates a new fake TwinCAT client.</summary>
    /// <returns>A newly created client instance.</returns>
    public ITwinCATClient Create()
    {
        var client = Customise?.Invoke() ?? new FakeTwinCATClient();
        Clients.Add(client);
        return client;
    }
}
