@page "/clusters/{ClusterId}/drivers/new/opcuaclient"
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
@rendermode RenderMode.InteractiveServer
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.EntityFrameworkCore
@using ZB.MOM.WW.OtOpcUa.AdminUI.Clients
@using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers
@using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers.Pickers
@using ZB.MOM.WW.OtOpcUa.Configuration
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
@using ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient
@inject IDbContextFactory<OtOpcUaConfigDbContext> DbFactory
@inject NavigationManager Nav

<div class="d-flex justify-content-between align-items-center mb-3">
    <h4 class="mb-0">@(IsNew ? "New OPC UA Client driver" : "Edit OPC UA Client driver") &middot; <span class="mono">@ClusterId</span></h4>
    <a href="/clusters/@ClusterId/drivers" class="btn btn-outline-secondary btn-sm">Cancel</a>
</div>
<ClusterNav ClusterId="@ClusterId" ActiveTab="drivers" />

@if (!_loaded)
{
    <p>Loading&hellip;</p>
}
else if (!IsNew && _existing is null)
{
    <section class="panel notice rise" style="animation-delay:.02s">
        Driver instance <span class="mono">@DriverInstanceId</span> was not found in cluster <span class="mono">@ClusterId</span>.
    </section>
}
else
{
    <EditForm Model="_form" OnValidSubmit="SubmitAsync" FormName="opcuaclientDriverEdit">
        <DataAnnotationsValidator />
        <DriverFormShell IsNew="IsNew" Busy="_busy" Error="@_error"
                         CancelHref="@($"/clusters/{ClusterId}/drivers")"
                         OnDelete="@(IsNew ? null : (EventCallback?)EventCallback.Factory.Create(this, DeleteAsync))">

            <DriverIdentitySection Model="_identityModel" Namespaces="_namespaces" IsNew="IsNew" ShowDriverType="false" />

            @if (!IsNew && !string.IsNullOrEmpty(DriverInstanceId))
            {
                <DriverStatusPanel DriverInstanceId="@DriverInstanceId" Enabled="@_identityModel.Enabled" />
            }

            <div class="mt-2 mb-3">
                <DriverTestConnectButton DriverType="@DriverTypeKey"
                                         GetConfigJson="@SerializeCurrentConfig"
                                         TimeoutSeconds="@_form.OpcUa.ProbeTimeoutSeconds" />
                <button type="button" class="btn btn-sm btn-outline-secondary mt-2"
                        @onclick="@(() => _showPicker = true)">
                    Pick address
                </button>
            </div>

            <DriverTagPicker @bind-Visible="_showPicker"
                             Title="OPC UA address"
                             CurrentAddress="@_pickedAddress"
                             OnPickAddress="@OnAddressPicked">
                <OpcUaClientAddressPickerBody CurrentAddress="@_pickedAddress"
                                              CurrentAddressChanged="@((s) => _pickedAddress = s)"
                                              GetConfigJson="@SerializeCurrentConfig" />
            </DriverTagPicker>

            @* Endpoint *@
            <section class="panel rise mt-3" style="animation-delay:.06s">
                <div class="panel-head">Endpoint</div>
                <div style="padding:1rem">
                    <div class="row g-3">
                        <div class="col-md-8">
                            <label class="form-label">Endpoint URL</label>
                            <InputText @bind-Value="_form.OpcUa.EndpointUrl" class="form-control form-control-sm mono"
                                       placeholder="opc.tcp://plc.internal:4840" />
                            <div class="form-text">Single-endpoint shortcut. When EndpointUrls list is non-empty, this field is ignored.</div>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Browse root NodeId (blank = ObjectsFolder)</label>
                            <InputText @bind-Value="_form.OpcUa.BrowseRoot" class="form-control form-control-sm mono"
                                       placeholder="i=85" />
                            <div class="form-text">Restrict mirroring to a sub-tree.</div>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Application URI</label>
                            <InputText @bind-Value="_form.OpcUa.ApplicationUri" class="form-control form-control-sm mono" />
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Session name</label>
                            <InputText @bind-Value="_form.OpcUa.SessionName" class="form-control form-control-sm" />
                        </div>
                        <div class="col-md-4">
                            <div class="form-check form-switch mt-4">
                                <InputCheckbox @bind-Value="_form.OpcUa.AutoAcceptCertificates" class="form-check-input" id="autoAcceptCerts" />
                                <label class="form-check-label" for="autoAcceptCerts">Auto-accept certificates <span class="badge bg-warning text-dark">Dev only</span></label>
                            </div>
                        </div>
                    </div>
                    <div class="row g-3 mt-1">
                        <div class="col-md-3">
                            <label class="form-label">Per-endpoint connect timeout (s)</label>
                            <InputNumber @bind-Value="_form.OpcUa.PerEndpointConnectTimeoutSeconds" class="form-control form-control-sm" />
                            <div class="form-text">Default 3 s — failover sweep budget.</div>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Operation timeout (s)</label>
                            <InputNumber @bind-Value="_form.OpcUa.TimeoutSeconds" class="form-control form-control-sm" />
                            <div class="form-text">Default 10 s — steady-state reads/writes.</div>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Session timeout (s)</label>
                            <InputNumber @bind-Value="_form.OpcUa.SessionTimeoutSeconds" class="form-control form-control-sm" />
                            <div class="form-text">Default 120 s.</div>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Keep-alive interval (s)</label>
                            <InputNumber @bind-Value="_form.OpcUa.KeepAliveIntervalSeconds" class="form-control form-control-sm" />
                            <div class="form-text">Default 5 s.</div>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Reconnect period (s)</label>
                            <InputNumber @bind-Value="_form.OpcUa.ReconnectPeriodSeconds" class="form-control form-control-sm" />
                            <div class="form-text">Initial delay after session drop. Default 5 s.</div>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Max discovered nodes</label>
                            <InputNumber @bind-Value="_form.OpcUa.MaxDiscoveredNodes" class="form-control form-control-sm" />
                            <div class="form-text">Default 10000.</div>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Max browse depth</label>
                            <InputNumber @bind-Value="_form.OpcUa.MaxBrowseDepth" class="form-control form-control-sm" />
                            <div class="form-text">Default 10.</div>
                        </div>
                    </div>
                    <div class="row g-3 mt-1">
                        <div class="col-12">
                            <CollectionEditor TRow="EndpointUrlRow" Items="_endpoints"
                                              Title="Endpoint URLs" ItemNoun="endpoint" AnimationDelay=".07s"
                                              NewRow="@(() => new EndpointUrlRow())" Clone="@(r => r.Clone())"
                                              Validate="EndpointUrlRow.ValidateRow">
                                <HeaderTemplate>
                                    <tr><th>Endpoint URL (failover list — first reachable wins)</th><th></th></tr>
                                </HeaderTemplate>
                                <RowTemplate Context="e">
                                    <td class="mono">@e.Url</td>
                                </RowTemplate>
                                <EditTemplate Context="e">
                                    <label class="form-label">Endpoint URL</label>
                                    <input class="form-control form-control-sm mono" @bind="e.Url"
                                           placeholder="opc.tcp://plc.internal:4840" />
                                    <div class="form-text">When this list is non-empty, the single Endpoint URL above is ignored.</div>
                                </EditTemplate>
                            </CollectionEditor>
                        </div>
                    </div>
                </div>
            </section>

            @* Security *@
            <section class="panel rise mt-3" style="animation-delay:.08s">
                <div class="panel-head">Security</div>
                <div style="padding:1rem">
                    <div class="row g-3">
                        <div class="col-md-4">
                            <label class="form-label">Security mode</label>
                            <InputSelect @bind-Value="_form.OpcUa.SecurityMode" class="form-select form-select-sm">
                                @foreach (var e in Enum.GetValues<OpcUaSecurityMode>())
                                {
                                    <option value="@e">@e</option>
                                }
                            </InputSelect>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Security policy</label>
                            <InputSelect @bind-Value="_form.OpcUa.SecurityPolicy" class="form-select form-select-sm">
                                @foreach (var e in Enum.GetValues<OpcUaSecurityPolicy>())
                                {
                                    <option value="@e">@e</option>
                                }
                            </InputSelect>
                        </div>
                    </div>
                </div>
            </section>

            @* Authentication *@
            <section class="panel rise mt-3" style="animation-delay:.10s">
                <div class="panel-head">Authentication</div>
                <div style="padding:1rem">
                    <div class="row g-3">
                        <div class="col-md-4">
                            <label class="form-label">Auth type</label>
                            <InputSelect @bind-Value="_form.OpcUa.AuthType" class="form-select form-select-sm">
                                @foreach (var e in Enum.GetValues<OpcUaAuthType>())
                                {
                                    <option value="@e">@e</option>
                                }
                            </InputSelect>
                        </div>
                        @if (_form.OpcUa.AuthType == OpcUaAuthType.Username)
                        {
                            <div class="col-md-4">
                                <label class="form-label">Username</label>
                                <InputText @bind-Value="_form.OpcUa.Username" class="form-control form-control-sm" />
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">Password</label>
                                <InputText @bind-Value="_form.OpcUa.Password" type="password" class="form-control form-control-sm" autocomplete="new-password" />
                            </div>
                        }
                        @if (_form.OpcUa.AuthType == OpcUaAuthType.Certificate)
                        {
                            <div class="col-md-6">
                                <label class="form-label">User certificate path (PFX/PEM)</label>
                                <InputText @bind-Value="_form.OpcUa.UserCertificatePath" class="form-control form-control-sm mono"
                                           placeholder="C:\certs\user.pfx" />
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">Certificate password (if PFX-locked)</label>
                                <InputText @bind-Value="_form.OpcUa.UserCertificatePassword" type="password" class="form-control form-control-sm" autocomplete="new-password" />
                            </div>
                        }
                    </div>
                </div>
            </section>

            @* Namespace mapping *@
            <section class="panel rise mt-3" style="animation-delay:.12s">
                <div class="panel-head">Namespace mapping</div>
                <div style="padding:1rem">
                    <div class="row g-3">
                        <div class="col-md-4">
                            <label class="form-label">Target namespace kind</label>
                            <InputSelect @bind-Value="_form.OpcUa.TargetNamespaceKind" class="form-select form-select-sm">
                                @foreach (var e in Enum.GetValues<OpcUaTargetNamespaceKind>())
                                {
                                    <option value="@e">@e</option>
                                }
                            </InputSelect>
                            <div class="form-text">Equipment = raw data re-mapped to UNS. SystemPlatform = processed data; hierarchy preserved as-is.</div>
                        </div>
                        <div class="col-12">
                            <label class="form-label">UNS mapping table (read-only — edit via raw JSON import)</label>
                            <pre class="form-control form-control-sm mono" style="min-height:3rem;overflow:auto;white-space:pre-wrap;">@_unsMappingTableJson</pre>
                            <div class="form-text">Keys = remote browse-path prefixes; values = UNS Area/Line/Name paths. Required when TargetNamespaceKind = Equipment.</div>
                        </div>
                    </div>
                </div>
            </section>

            @* Diagnostics *@
            <section class="panel rise mt-3" style="animation-delay:.14s">
                <div class="panel-head">Diagnostics</div>
                <div style="padding:1rem">
                    <div class="row g-3">
                        <div class="col-md-3">
                            <label class="form-label">Admin UI probe timeout (seconds)</label>
                            <InputNumber @bind-Value="_form.OpcUa.ProbeTimeoutSeconds" class="form-control form-control-sm" />
                            <div class="form-text">Max 60. Used by Test Connect. Default 15.</div>
                        </div>
                    </div>
                </div>
            </section>

            <DriverResilienceSection @bind-ResilienceConfig="_form.ResilienceConfig" />
        </DriverFormShell>
    </EditForm>
}

@code {
    [Parameter] public string ClusterId { get; set; } = "";
    [Parameter] public string? DriverInstanceId { get; set; }

    private const string DriverTypeKey = "OpcUaClient";

    private bool IsNew => string.IsNullOrEmpty(DriverInstanceId);

    private static readonly System.Text.Json.JsonSerializerOptions _jsonOpts = new()
    {
        PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase,
        UnmappedMemberHandling = System.Text.Json.Serialization.JsonUnmappedMemberHandling.Skip,
        WriteIndented = false,
    };

    private FormModel _form = new();
    private DriverIdentitySection.DriverIdentityModel _identityModel = new() { DriverType = DriverTypeKey };
    private DriverInstance? _existing;
    private List<Namespace> _namespaces = new();
    private bool _loaded;
    private bool _busy;
    private string? _error;

    // Address picker state
    private bool _showPicker;
    private string _pickedAddress = "";

    private void OnAddressPicked(string address) => _pickedAddress = address;

    // Held separately because EndpointUrls is a collection — edited via the CollectionEditor modal.
    private List<EndpointUrlRow> _endpoints = [];

    // Read-only JSON snippet for the UnsMappingTable, which has no list editor yet.
    private string _unsMappingTableJson = "{}";

    protected override async Task OnInitializedAsync()
    {
        await using var db = await DbFactory.CreateDbContextAsync();
        _namespaces = await db.Namespaces.AsNoTracking()
            .Where(n => n.ClusterId == ClusterId)
            .OrderBy(n => n.NamespaceId)
            .ToListAsync();

        if (IsNew)
        {
            _identityModel = new()
            {
                DriverInstanceId = "",
                Name = "",
                DriverType = DriverTypeKey,
                NamespaceId = _namespaces.FirstOrDefault()?.NamespaceId ?? "",
                Enabled = true,
            };
            _form = new FormModel();
        }
        else
        {
            _existing = await db.DriverInstances.AsNoTracking()
                .FirstOrDefaultAsync(d => d.ClusterId == ClusterId && d.DriverInstanceId == DriverInstanceId);
            if (_existing is not null)
            {
                _identityModel = new()
                {
                    DriverInstanceId = _existing.DriverInstanceId,
                    Name = _existing.Name,
                    DriverType = _existing.DriverType,
                    NamespaceId = _existing.NamespaceId,
                    Enabled = _existing.Enabled,
                };
                var opts = TryDeserialize(_existing.DriverConfig) ?? new OpcUaClientDriverOptions();
                _form = new FormModel();
                _form.OpcUa = OpcUaClientFormModel.FromRecord(opts);
                _endpoints = opts.EndpointUrls.Select(EndpointUrlRow.FromUrl).ToList();
                _unsMappingTableJson = System.Text.Json.JsonSerializer.Serialize(opts.UnsMappingTable, _jsonOpts);
                _form.ResilienceConfig = _existing.ResilienceConfig;
                _form.RowVersion = _existing.RowVersion;
            }
        }
        _loaded = true;
    }

    private async Task SubmitAsync()
    {
        _busy = true; _error = null;
        try
        {
            var opts = _form.OpcUa.ToRecord(_endpoints.Select(r => r.ToUrl()).ToList());
            var configJson = System.Text.Json.JsonSerializer.Serialize(opts, _jsonOpts);
            await using var db = await DbFactory.CreateDbContextAsync();
            if (IsNew)
            {
                if (await db.DriverInstances.AnyAsync(d => d.DriverInstanceId == _identityModel.DriverInstanceId))
                {
                    _error = $"Driver instance '{_identityModel.DriverInstanceId}' already exists."; return;
                }
                db.DriverInstances.Add(new DriverInstance
                {
                    DriverInstanceId = _identityModel.DriverInstanceId,
                    ClusterId = ClusterId,
                    NamespaceId = _identityModel.NamespaceId,
                    Name = _identityModel.Name,
                    DriverType = DriverTypeKey,
                    Enabled = _identityModel.Enabled,
                    DriverConfig = configJson,
                    ResilienceConfig = string.IsNullOrWhiteSpace(_form.ResilienceConfig) ? null : _form.ResilienceConfig,
                });
            }
            else
            {
                var entity = await db.DriverInstances.FirstOrDefaultAsync(
                    d => d.ClusterId == ClusterId && d.DriverInstanceId == DriverInstanceId);
                if (entity is null) { _error = "Row no longer exists."; return; }
                db.Entry(entity).Property(e => e.RowVersion).OriginalValue = _form.RowVersion;
                entity.NamespaceId = _identityModel.NamespaceId;
                entity.Name = _identityModel.Name;
                entity.Enabled = _identityModel.Enabled;
                entity.DriverConfig = configJson;
                entity.ResilienceConfig = string.IsNullOrWhiteSpace(_form.ResilienceConfig) ? null : _form.ResilienceConfig;
            }
            await db.SaveChangesAsync();
            Nav.NavigateTo($"/clusters/{ClusterId}/drivers");
        }
        catch (DbUpdateConcurrencyException)
        {
            _error = "Another user changed this driver instance while you were editing. Reload to see the latest values, then re-apply your changes.";
        }
        catch (Exception ex) { _error = ex.Message; }
        finally { _busy = false; }
    }

    private async Task DeleteAsync()
    {
        if (IsNew) return;
        _busy = true; _error = null;
        try
        {
            await using var db = await DbFactory.CreateDbContextAsync();
            var entity = await db.DriverInstances.FirstOrDefaultAsync(
                d => d.ClusterId == ClusterId && d.DriverInstanceId == DriverInstanceId);
            if (entity is null) { Nav.NavigateTo($"/clusters/{ClusterId}/drivers"); return; }
            db.Entry(entity).Property(e => e.RowVersion).OriginalValue = _form.RowVersion;
            db.DriverInstances.Remove(entity);
            await db.SaveChangesAsync();
            Nav.NavigateTo($"/clusters/{ClusterId}/drivers");
        }
        catch (DbUpdateConcurrencyException)
        {
            _error = "Another user changed this driver instance while you were viewing it. Reload before deleting.";
        }
        catch (Exception ex)
        {
            _error = $"Delete failed: {ex.Message}. (Likely because equipment/tags still reference this driver — remove them first.)";
        }
        finally { _busy = false; }
    }

    private string SerializeCurrentConfig()
        => System.Text.Json.JsonSerializer.Serialize(
            _form.OpcUa.ToRecord(_endpoints.Select(r => r.ToUrl()).ToList()), _jsonOpts);

    private static OpcUaClientDriverOptions? TryDeserialize(string json)
    {
        try { return System.Text.Json.JsonSerializer.Deserialize<OpcUaClientDriverOptions>(json, _jsonOpts); }
        catch { return null; }
    }

    public sealed class FormModel
    {
        public OpcUaClientFormModel OpcUa { get; set; } = new();
        public string? ResilienceConfig { get; set; }
        public byte[] RowVersion { get; set; } = [];
    }

    /// <summary>
    ///     Mutable VM for a single endpoint URL row. EndpointUrls is a plain
    ///     <c>List&lt;string&gt;</c> (a failover list) so the row is a thin wrapper the
    ///     <see cref="CollectionEditor{TRow}"/> modal can bind to.
    /// </summary>
    public sealed class EndpointUrlRow
    {
        public string Url { get; set; } = "";
        public EndpointUrlRow Clone() => (EndpointUrlRow)MemberwiseClone();
        public static EndpointUrlRow FromUrl(string u) => new() { Url = u };
        public string ToUrl() => Url.Trim();

        public static string? ValidateRow(EndpointUrlRow row, IReadOnlyList<EndpointUrlRow> all, int? editIndex)
        {
            if (string.IsNullOrWhiteSpace(row.Url)) return "URL is required.";
            if (!row.Url.Trim().StartsWith("opc.tcp://", StringComparison.OrdinalIgnoreCase))
                return "Endpoint URL must start with opc.tcp://";
            for (var i = 0; i < all.Count; i++)
                if (i != editIndex && string.Equals(all[i].Url.Trim(), row.Url.Trim(), StringComparison.OrdinalIgnoreCase))
                    return $"Duplicate endpoint '{row.Url}'.";
            return null;
        }
    }

    /// <summary>
    ///     Mutable mirror of <see cref="OpcUaClientDriverOptions"/> with int wrappers for
    ///     TimeSpan fields so Blazor InputNumber can bind them.
    ///     EndpointUrls is edited via the CollectionEditor (held on the page as a row list and
    ///     threaded into <see cref="ToRecord"/>); UnsMappingTable is shown as read-only JSON and
    ///     survives round-trip via the original deserialized record, re-serialized unchanged.
    /// </summary>
    public sealed class OpcUaClientFormModel
    {
        // Connection
        public string EndpointUrl { get; set; } = "opc.tcp://localhost:4840";
        public string? BrowseRoot { get; set; }
        public string ApplicationUri { get; set; } = "urn:localhost:OtOpcUa:GatewayClient";
        public string SessionName { get; set; } = "OtOpcUa-Gateway";
        public bool AutoAcceptCertificates { get; set; } = false;
        public int PerEndpointConnectTimeoutSeconds { get; set; } = 3;
        public int TimeoutSeconds { get; set; } = 10;
        public int SessionTimeoutSeconds { get; set; } = 120;
        public int KeepAliveIntervalSeconds { get; set; } = 5;
        public int ReconnectPeriodSeconds { get; set; } = 5;
        public int MaxDiscoveredNodes { get; set; } = 10_000;
        public int MaxBrowseDepth { get; set; } = 10;

        // Security
        public OpcUaSecurityMode SecurityMode { get; set; } = OpcUaSecurityMode.None;
        public OpcUaSecurityPolicy SecurityPolicy { get; set; } = OpcUaSecurityPolicy.None;

        // Authentication
        public OpcUaAuthType AuthType { get; set; } = OpcUaAuthType.Anonymous;
        public string? Username { get; set; }
        public string? Password { get; set; }
        public string? UserCertificatePath { get; set; }
        public string? UserCertificatePassword { get; set; }

        // Namespace mapping
        public OpcUaTargetNamespaceKind TargetNamespaceKind { get; set; } = OpcUaTargetNamespaceKind.Equipment;

        // Diagnostics
        public int ProbeTimeoutSeconds { get; set; } = 15;

        // Preserved read-only collection (round-tripped unchanged from original record)
        internal IReadOnlyDictionary<string, string> _unsMappingTable = new System.Collections.Generic.Dictionary<string, string>();

        public static OpcUaClientFormModel FromRecord(OpcUaClientDriverOptions r) => new()
        {
            EndpointUrl = r.EndpointUrl,
            BrowseRoot = r.BrowseRoot,
            ApplicationUri = r.ApplicationUri,
            SessionName = r.SessionName,
            AutoAcceptCertificates = r.AutoAcceptCertificates,
            PerEndpointConnectTimeoutSeconds = (int)r.PerEndpointConnectTimeout.TotalSeconds,
            TimeoutSeconds = (int)r.Timeout.TotalSeconds,
            SessionTimeoutSeconds = (int)r.SessionTimeout.TotalSeconds,
            KeepAliveIntervalSeconds = (int)r.KeepAliveInterval.TotalSeconds,
            ReconnectPeriodSeconds = (int)r.ReconnectPeriod.TotalSeconds,
            MaxDiscoveredNodes = r.MaxDiscoveredNodes,
            MaxBrowseDepth = r.MaxBrowseDepth,
            SecurityMode = r.SecurityMode,
            SecurityPolicy = r.SecurityPolicy,
            AuthType = r.AuthType,
            Username = r.Username,
            Password = r.Password,
            UserCertificatePath = r.UserCertificatePath,
            UserCertificatePassword = r.UserCertificatePassword,
            TargetNamespaceKind = r.TargetNamespaceKind,
            ProbeTimeoutSeconds = r.ProbeTimeoutSeconds,
            _unsMappingTable = r.UnsMappingTable,
        };

        public OpcUaClientDriverOptions ToRecord(IReadOnlyList<string> endpointUrls) => new()
        {
            EndpointUrl = EndpointUrl,
            EndpointUrls = endpointUrls,
            BrowseRoot = string.IsNullOrWhiteSpace(BrowseRoot) ? null : BrowseRoot,
            ApplicationUri = ApplicationUri,
            SessionName = SessionName,
            AutoAcceptCertificates = AutoAcceptCertificates,
            PerEndpointConnectTimeout = TimeSpan.FromSeconds(PerEndpointConnectTimeoutSeconds),
            Timeout = TimeSpan.FromSeconds(TimeoutSeconds),
            SessionTimeout = TimeSpan.FromSeconds(SessionTimeoutSeconds),
            KeepAliveInterval = TimeSpan.FromSeconds(KeepAliveIntervalSeconds),
            ReconnectPeriod = TimeSpan.FromSeconds(ReconnectPeriodSeconds),
            MaxDiscoveredNodes = MaxDiscoveredNodes,
            MaxBrowseDepth = MaxBrowseDepth,
            SecurityMode = SecurityMode,
            SecurityPolicy = SecurityPolicy,
            AuthType = AuthType,
            Username = string.IsNullOrWhiteSpace(Username) ? null : Username,
            Password = string.IsNullOrWhiteSpace(Password) ? null : Password,
            UserCertificatePath = string.IsNullOrWhiteSpace(UserCertificatePath) ? null : UserCertificatePath,
            UserCertificatePassword = string.IsNullOrWhiteSpace(UserCertificatePassword) ? null : UserCertificatePassword,
            TargetNamespaceKind = TargetNamespaceKind,
            UnsMappingTable = _unsMappingTable,
            ProbeTimeoutSeconds = ProbeTimeoutSeconds,
        };
    }
}
