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
{
    public bool ThrowOnCreate { get; set; }
    public int CreateCallCount { get; private set; }
    public ConnectionSettings? LastSettings { get; private set; }

    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);
    }
}