@using Iceshrimp.Frontend.Core.Services
@using Iceshrimp.Frontend.Localization
@using Microsoft.Extensions.Localization
@inject GlobalComponentSvc             GlobalComponentSvc;
@inject IJSRuntime                     Js;
@inject IStringLocalizer<Localization> Loc;

<dialog class="dialog" @ref="Dialog">
    <div class="prompt @(Waiting ? "waiting" : "")">
        @if (Waiting)
        {
            <LoadingSpinner Scale="1.5"/>
        }
        else
        {
            <span>@Text</span>
            @if (MultiLine)
            {
                <textarea class="input" @bind="Input" @bind:event="oninput" rows="5" placeholder="@Placeholder"></textarea>
            }
            else
            {
                <input type="text" class="input" @bind="Input" @bind:event="oninput" placeholder="@Placeholder"/>
            }

            <div class="buttons">
                <button class="button submit-btn" @onclick="ConfirmAction"
                        disabled="@(Required && string.IsNullOrWhiteSpace(Input))">@(ButtonText ?? Loc["Submit"])</button>
                <button class="button" @onclick="CancelAction">@Loc["Cancel"]</button>
            </div>
        }
    </div>
</dialog>

@code {
    private ElementReference       Dialog { get; set; }
    private IJSObjectReference     _module = null!;
    private EventCallback<string?> Action      { get; set; }
    private string                 Text        { get; set; } = "";
    private string                 Placeholder { get; set; } = "";
    private bool                   Required    { get; set; }
    private bool                   MultiLine   { get; set; }
    private string?                ButtonText  { get; set; }
    private string                 Input       { get; set; } = "";
    private bool                   Waiting     { get; set; }

    private async Task CloseDialog()
    {
        await _module.InvokeVoidAsync("closeDialog", Dialog);
    }

    public async Task Prompt(EventCallback<string?> action, string text, string placeholder, string? defaultValue, bool allowEmpty = false, bool multiLine = false, string? buttonText = null)
    {
        Action      = action;
        Text        = text;
        Placeholder = placeholder;
        Required    = !allowEmpty;
        MultiLine   = multiLine;
        ButtonText  = buttonText;
        Input       = defaultValue ?? "";
        Waiting     = false;

        StateHasChanged();

        await _module.InvokeVoidAsync("openDialog", Dialog);
    }

    private async Task ConfirmAction()
    {
        Waiting = true;
        await Action.InvokeAsync(MultiLine ? Input.ReplaceLineEndings("\n").Trim() : Input.ReplaceLineEndings(" ").Trim());
        Waiting = false;
        await CloseDialog();
    }

    private async Task CancelAction()
    {
        await Action.InvokeAsync(null);
        await CloseDialog();
    }

    protected override async Task OnInitializedAsync()
    {
        _module = await Js.InvokeAsync<IJSObjectReference>("import",
                                                           "./Components/PromptDialog.razor.js");
        GlobalComponentSvc.PromptDialog = this;
    }
}