@inject ISnackbar Snackbar

<MudDialog>
    <TitleContent>
        <MudText Typo="Typo.h6">
            @Title
        </MudText>
    </TitleContent>
    <DialogContent>
        <div class="d-flex flex-column" style="width: 500px;">
            <MudTextField @bind-Value="FileName" Label="@Label" Class="mb-3" ShrinkLabel Variant="Variant.Outlined"
                          DebounceInterval="500" OnDebounceIntervalElapsed="ValidateName" OnBlur="ValidateName"
                          Error="@NameError" ErrorText="@NameErrorText" Adornment="@AdornmentVisible" AdornmentText=".cs" />
        </div>
    </DialogContent>
    <DialogActions>
        <MudButton OnClick="OnCancel">Cancel</MudButton>
        <MudButton Color="Color.Primary" OnClick="OnCreate" Disabled="@CreateButtonDisable">Create</MudButton>
    </DialogActions>
</MudDialog>

@code {
    [CascadingParameter]
    private IMudDialogInstance Dialog { get; set; } = null!;

    [Parameter]
    public string FolderName { get; set; } = "";

    [Parameter]
    public bool IsFolder { get; set; } = false;

    private string Title => $"Tạo mới {(IsFolder ? "folder" : "file code")}";
    private string Label => $"/{FolderName}";
    private Adornment AdornmentVisible => IsFolder ? Adornment.None : Adornment.End;

    private string FileName { get; set; } = "";

    private string NameErrorText = "";
    private bool NameError = false;

    private bool CreateButtonDisable => NameError;

    private void OnCancel() => Dialog.Cancel();

    public void OnCreate()
    {
        ValidateName();

        if (NameError)
        {
            Snackbar.Add("Tên chưa đúng!", Severity.Error);
        }
        else
        {
            Dialog.Close(DialogResult.Ok(FileName));
        }
    }

    private void ValidateName()
    {
        NameErrorText = string.IsNullOrEmpty(FileName) ? "Tên không được để trống" : "";
        NameError = !string.IsNullOrEmpty(NameErrorText);
    }
}
