@* Driver-type picker for the /raw "New driver" action: pick a driver type + name, then the caller
   creates a minimal driver (CreateDriverAsync with "{}") and the operator configures it afterwards via
   the Configure-driver modal. Calculation is included so a Calculation driver row is authorable now
   (its factory lands in Wave C). Name is inline-validated as a RawPath segment. Markup mirrors the other
   /raw modal shells. *@
@using ZB.MOM.WW.OtOpcUa.Commons.Types
@using ZB.MOM.WW.OtOpcUa.Core.Abstractions

@if (Visible)
{
    <div class="modal-backdrop fade show" style="display:block"></div>
    <div class="modal fade show" tabindex="-1" role="dialog" style="display:block">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">New driver</h5>
                    <button type="button" class="btn-close" aria-label="Close" @onclick="Cancel"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label" for="raw-drv-type">Driver type</label>
                        <select id="raw-drv-type" class="form-select form-select-sm" @bind="_type">
                            @foreach (var t in Types)
                            {
                                <option value="@t.Value">@t.Label</option>
                            }
                        </select>
                    </div>
                    <div class="mb-1">
                        <label class="form-label" for="raw-drv-name">Name</label>
                        <input id="raw-drv-name" class="form-control form-control-sm mono"
                               @bind="_name" @bind:event="oninput" placeholder="line3-modbus" />
                        <div class="form-text">A RawPath segment — no '/', no leading or trailing spaces.</div>
                    </div>
                    @if (_error is not null)
                    {
                        <div class="text-danger small mt-1">@_error</div>
                    }
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-outline-secondary" @onclick="Cancel">Cancel</button>
                    <button type="button" class="btn btn-primary" @onclick="Submit">Create</button>
                </div>
            </div>
        </div>
    </div>
}

@code {
    /// <summary>Whether the dialog is shown (supports <c>@bind-Visible</c>).</summary>
    [Parameter] public bool Visible { get; set; }

    /// <summary>Two-way visibility callback.</summary>
    [Parameter] public EventCallback<bool> VisibleChanged { get; set; }

    /// <summary>Raised with the chosen driver type + name when the operator confirms; the dialog self-closes after.</summary>
    [Parameter] public EventCallback<(string DriverType, string Name)> OnSubmit { get; set; }

    // Label → DriverType value. Galaxy's factory registers as "GalaxyMxGateway"; Calculation is
    // authorable now though its factory arrives in Wave C.
    private static readonly (string Label, string Value)[] Types =
    [
        ("Modbus", DriverTypeNames.Modbus),
        ("S7", DriverTypeNames.S7),
        ("AbCip", DriverTypeNames.AbCip),
        ("AbLegacy", DriverTypeNames.AbLegacy),
        ("TwinCAT", DriverTypeNames.TwinCAT),
        ("FOCAS", DriverTypeNames.FOCAS),
        ("OpcUaClient", DriverTypeNames.OpcUaClient),
        ("Galaxy", DriverTypeNames.Galaxy),
        ("Calculation", "Calculation"),
    ];

    private string _type = DriverTypeNames.Modbus;
    private string _name = "";
    private string? _error;
    private bool _open;   // seed once per open; preserve edits across unrelated re-renders

    protected override void OnParametersSet()
    {
        if (!Visible) { _open = false; return; }
        if (_open) { return; }
        _open = true;
        _type = DriverTypeNames.Modbus;
        _name = "";
        _error = null;
    }

    private async Task Submit()
    {
        var err = RawPaths.ValidateSegment(_name);
        if (err is not null) { _error = err; return; }
        await OnSubmit.InvokeAsync((_type, _name));
        await Close();
    }

    private Task Cancel() => Close();

    private async Task Close()
    {
        Visible = false;
        _open = false;
        await VisibleChanged.InvokeAsync(false);
    }
}
