using System;
using System.Collections.Generic;
using ZB.MOM.WW.MxGateway.Worker.MxAccess;

namespace ZB.MOM.WW.MxGateway.Worker.Tests.MxAccess;

/// <summary>
/// Regression tests for <see cref="MxAccessComServer"/>. The
/// adapter no longer falls back to late-bound <c>Type.InvokeMember</c>
/// reflection: a COM object must implement either the typed
/// <c>ILMXProxyServer</c> COM interface family (production) or
/// <see cref="IMxAccessServer"/> directly (test fakes).
/// </summary>
public sealed class MxAccessComServerTests
{
    /// <summary>
    /// A COM object implementing <see cref="IMxAccessServer"/> is routed
    /// through the typed interface — no reflection — preserving arguments
    /// and return values.
    /// </summary>
    [Fact]
    public void Methods_WithTypedServer_RouteThroughTypedInterface()
    {
        RecordingMxAccessServer typed = new(registerHandle: 77);
        MxAccessComServer adapter = new(typed);

        int serverHandle = adapter.Register("client-a");
        adapter.Advise(serverHandle, itemHandle: 9);
        adapter.Unregister(serverHandle);

        Assert.Equal(77, serverHandle);
        Assert.Equal("client-a", typed.RegisteredClientName);
        Assert.Equal(new[] { "Register:client-a", "Advise:77:9", "Unregister:77" }, typed.Calls);
    }

    /// <summary>
    /// The MXAccess command methods added in the worker COM commands bundle
    /// (Suspend/Activate/AuthenticateUser/ArchestrAUserToId/AddBufferedItem/
    /// SetBufferedUpdateInterval) route through the typed interface with their
    /// arguments preserved, and the credential is never echoed back.
    /// </summary>
    [Fact]
    public void CommandMethods_WithTypedServer_RouteThroughTypedInterface()
    {
        RecordingMxAccessServer typed = new(registerHandle: 5);
        MxAccessComServer adapter = new(typed);

        adapter.Suspend(serverHandle: 5, itemHandle: 11);
        adapter.Activate(serverHandle: 5, itemHandle: 12);
        adapter.AuthenticateUser(serverHandle: 5, verifyUser: "Administrator", verifyUserPassword: "s3cret");
        adapter.ArchestrAUserToId(serverHandle: 5, userIdGuid: "guid-1");
        adapter.AddBufferedItem(serverHandle: 5, itemDefinition: "TestInt", itemContext: "TestChildObject");
        adapter.SetBufferedUpdateInterval(serverHandle: 5, updateIntervalMilliseconds: 250);

        Assert.Equal(
            new[]
            {
                "Suspend:5:11",
                "Activate:5:12",
                "AuthenticateUser:5:Administrator",
                "ArchestrAUserToId:5:guid-1",
                "AddBufferedItem:5:TestInt:TestChildObject",
                "SetBufferedUpdateInterval:5:250",
            },
            typed.Calls);
        Assert.DoesNotContain(typed.Calls, call => call.Contains("s3cret", StringComparison.Ordinal));
    }

    /// <summary>
    /// A COM object that implements neither the typed COM interface family
    /// nor <see cref="IMxAccessServer"/> fails fast with a clear
    /// <see cref="InvalidOperationException"/> instead of a late-bound
    /// reflection call.
    /// </summary>
    [Fact]
    public void Methods_WithUntypedObject_ThrowInvalidOperation()
    {
        MxAccessComServer adapter = new(new object());

        InvalidOperationException exception =
            Assert.Throws<InvalidOperationException>(() => adapter.Register("client"));

        Assert.Contains("does not implement", exception.Message, StringComparison.Ordinal);
        Assert.Contains(nameof(IMxAccessServer), exception.Message, StringComparison.Ordinal);
    }

    /// <summary>
    /// Exceptions thrown by the typed server propagate unchanged — no
    /// <c>TargetInvocationException</c> wrapping (reflection is gone).
    /// </summary>
    [Fact]
    public void Methods_WhenTypedServerThrows_PropagateOriginalException()
    {
        RecordingMxAccessServer typed = new(registerHandle: 1)
        {
            ThrowOnRegister = new InvalidOperationException("register failed"),
        };
        MxAccessComServer adapter = new(typed);

        InvalidOperationException exception =
            Assert.Throws<InvalidOperationException>(() => adapter.Register("client"));

        Assert.Equal("register failed", exception.Message);
    }

    private sealed class RecordingMxAccessServer : IMxAccessServer
    {
        private readonly int registerHandle;
        private readonly List<string> calls = new();

        /// <summary>Initializes a new instance with the specified register handle.</summary>
        /// <param name="registerHandle">The initial server handle value to return from Register.</param>
        public RecordingMxAccessServer(int registerHandle)
        {
            this.registerHandle = registerHandle;
        }

        /// <summary>Gets the client name passed to the most recent Register call.</summary>
        public string? RegisteredClientName { get; private set; }

        /// <summary>Gets or sets an exception to throw from the Register method.</summary>
        public Exception? ThrowOnRegister { get; set; }

        /// <summary>Gets the recorded method calls as strings.</summary>
        public IReadOnlyList<string> Calls => calls.ToArray();

        /// <inheritdoc />
        public int Register(string clientName)
        {
            calls.Add($"Register:{clientName}");
            RegisteredClientName = clientName;
            if (ThrowOnRegister is not null)
            {
                throw ThrowOnRegister;
            }

            return registerHandle;
        }

        /// <inheritdoc />
        public void Unregister(int serverHandle)
        {
            calls.Add($"Unregister:{serverHandle}");
        }

        /// <inheritdoc />
        public int AddItem(int serverHandle, string itemDefinition)
        {
            calls.Add($"AddItem:{serverHandle}:{itemDefinition}");
            return 0;
        }

        /// <inheritdoc />
        public int AddItem2(int serverHandle, string itemDefinition, string itemContext)
        {
            calls.Add($"AddItem2:{serverHandle}:{itemDefinition}:{itemContext}");
            return 0;
        }

        /// <inheritdoc />
        public void RemoveItem(int serverHandle, int itemHandle)
        {
            calls.Add($"RemoveItem:{serverHandle}:{itemHandle}");
        }

        /// <inheritdoc />
        public void Advise(int serverHandle, int itemHandle)
        {
            calls.Add($"Advise:{serverHandle}:{itemHandle}");
        }

        /// <inheritdoc />
        public void UnAdvise(int serverHandle, int itemHandle)
        {
            calls.Add($"UnAdvise:{serverHandle}:{itemHandle}");
        }

        /// <inheritdoc />
        public void AdviseSupervisory(int serverHandle, int itemHandle)
        {
            calls.Add($"AdviseSupervisory:{serverHandle}:{itemHandle}");
        }

        /// <inheritdoc />
        public void Write(int serverHandle, int itemHandle, object? value, int userId)
        {
            calls.Add($"Write:{serverHandle}:{itemHandle}:{value}:{userId}");
        }

        /// <inheritdoc />
        public void Write2(int serverHandle, int itemHandle, object? value, object? timestamp, int userId)
        {
            calls.Add($"Write2:{serverHandle}:{itemHandle}:{value}:{timestamp}:{userId}");
        }

        /// <inheritdoc />
        public void WriteSecured(int serverHandle, int itemHandle, int currentUserId, int verifierUserId, object? value)
        {
            calls.Add($"WriteSecured:{serverHandle}:{itemHandle}:{currentUserId}:{verifierUserId}:{value}");
        }

        /// <inheritdoc />
        public void WriteSecured2(
            int serverHandle, int itemHandle, int currentUserId, int verifierUserId, object? value, object? timestamp)
        {
            calls.Add($"WriteSecured2:{serverHandle}:{itemHandle}:{currentUserId}:{verifierUserId}:{value}:{timestamp}");
        }

        /// <inheritdoc />
        public object Suspend(int serverHandle, int itemHandle)
        {
            calls.Add($"Suspend:{serverHandle}:{itemHandle}");
            return new object();
        }

        /// <inheritdoc />
        public object Activate(int serverHandle, int itemHandle)
        {
            calls.Add($"Activate:{serverHandle}:{itemHandle}");
            return new object();
        }

        /// <inheritdoc />
        public int AuthenticateUser(int serverHandle, string verifyUser, string verifyUserPassword)
        {
            calls.Add($"AuthenticateUser:{serverHandle}:{verifyUser}");
            return 0;
        }

        /// <inheritdoc />
        public int ArchestrAUserToId(int serverHandle, string userIdGuid)
        {
            calls.Add($"ArchestrAUserToId:{serverHandle}:{userIdGuid}");
            return 0;
        }

        /// <inheritdoc />
        public int AddBufferedItem(int serverHandle, string itemDefinition, string itemContext)
        {
            calls.Add($"AddBufferedItem:{serverHandle}:{itemDefinition}:{itemContext}");
            return 0;
        }

        /// <inheritdoc />
        public void SetBufferedUpdateInterval(int serverHandle, int updateIntervalMilliseconds)
        {
            calls.Add($"SetBufferedUpdateInterval:{serverHandle}:{updateIntervalMilliseconds}");
        }
    }
}
