@namespace ZB.MOM.WW.ScadaBridge.CentralUI.Components.Forms
@using ZB.MOM.WW.ScadaBridge.Commons.Types.DataConnections
@using ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening

<div class="mxgateway-endpoint-editor">
    <h6 class="text-muted border-bottom pb-1">@Title</h6>

    <div class="row g-2 mb-2">
        <div class="col-md-7">
            <label class="form-label small">Gateway endpoint</label>
            <input type="text" class="form-control form-control-sm"
                   @bind="Config.Endpoint"
                   placeholder="http://host:5000" />
            @RenderFieldError("Endpoint")
        </div>
        <div class="col-md-5">
            <label class="form-label small">API key</label>
            <input type="password" class="form-control form-control-sm"
                   @bind="Config.ApiKey"
                   placeholder="gateway API key" />
            @RenderFieldError("ApiKey")
        </div>
    </div>

    <div class="row g-2 mb-2">
        <div class="col-md-5">
            <label class="form-label small">Client name</label>
            <input type="text" class="form-control form-control-sm"
                   @bind="Config.ClientName"
                   placeholder="(defaults to scadabridge)" />
        </div>
        <div class="col-md-3">
            <label class="form-label small">Write user id</label>
            <input type="number" class="form-control form-control-sm"
                   @bind="Config.WriteUserId" min="0" />
        </div>
        <div class="col-md-4">
            <label class="form-label small">Read timeout (ms)</label>
            <input type="number" class="form-control form-control-sm"
                   @bind="Config.ReadTimeoutMs" min="1" />
            @RenderFieldError("ReadTimeoutMs")
        </div>
    </div>

    <div class="text-muted small mt-2 mb-1">Transport security</div>
    <div class="row g-2 mb-2">
        <div class="col-md-2 d-flex align-items-end">
            <div class="form-check">
                <input class="form-check-input" type="checkbox"
                       id="@($"{IdPrefix}-usetls")"
                       @bind="Config.UseTls" />
                <label class="form-check-label small"
                       for="@($"{IdPrefix}-usetls")">Use TLS</label>
            </div>
        </div>
        @if (Config.UseTls)
        {
            <div class="col-md-6">
                <label class="form-label small">CA certificate path</label>
                <input type="text" class="form-control form-control-sm"
                       @bind="Config.CaFile"
                       placeholder="/etc/scadabridge/pki/gateway-ca.pem" />
            </div>
            <div class="col-md-4">
                <label class="form-label small">Server name override</label>
                <input type="text" class="form-control form-control-sm"
                       @bind="Config.ServerName"
                       placeholder="gateway.example.local" />
            </div>
        }
    </div>
</div>

@code {
    [Parameter, EditorRequired] public MxGatewayEndpointConfig Config { get; set; } = default!;
    [Parameter] public string Title { get; set; } = "Endpoint";
    [Parameter] public string IdPrefix { get; set; } = "mxgateway-endpoint";
    [Parameter] public ValidationResult? Errors { get; set; }

    private RenderFragment? RenderFieldError(string field)
    {
        var match = Errors?.Errors.FirstOrDefault(e =>
            e.EntityName != null
            && (e.EntityName == field || e.EntityName.EndsWith("." + field)));
        return match is null
            ? null
            : @<div class="text-danger small">@match.Message</div>;
    }
}
