using Opc.Ua;
using ZB.MOM.WW.OtOpcUa.Client.Shared.Adapters;
using ZB.MOM.WW.OtOpcUa.Client.Shared.Models;

namespace ZB.MOM.WW.OtOpcUa.Client.Shared.Tests.Fakes;

internal sealed class FakeApplicationConfigurationFactory : IApplicationConfigurationFactory
{
    /// <summary>Gets or sets a value indicating whether to throw when Create is called.</summary>
    public bool ThrowOnCreate { get; set; }

    /// <summary>Gets the number of times CreateAsync has been called.</summary>
    public int CreateCallCount { get; private set; }

    /// <summary>Gets the last connection settings passed to CreateAsync.</summary>
    public ConnectionSettings? LastSettings { get; private set; }

    /// <inheritdoc />
    public Task<ApplicationConfiguration> CreateAsync(ConnectionSettings settings, CancellationToken ct)
    {
        CreateCallCount++;
        LastSettings = settings;

        if (ThrowOnCreate)
            throw new InvalidOperationException("FakeApplicationConfigurationFactory configured to fail.");

        var config = new ApplicationConfiguration
        {
            ApplicationName = "FakeClient",
            ApplicationUri = "urn:localhost:FakeClient",
            ApplicationType = ApplicationType.Client,
            SecurityConfiguration = new SecurityConfiguration
            {
                AutoAcceptUntrustedCertificates = true
            },
            ClientConfiguration = new ClientConfiguration
            {
                DefaultSessionTimeout = settings.SessionTimeoutSeconds * 1000
            }
        };

        return Task.FromResult(config);
    }
}