@using Moonlight.App.Helpers.Files
@using Moonlight.App.Services
@using Moonlight.App.Services.Interop
@using Logging.Net

@inject ToastService ToastService
@inject SmartTranslateService SmartTranslateService

<InputFile OnChange="OnFileChanged" type="file" id="fileUpload" hidden=""/>
<label for="fileUpload" class="btn btn-primary me-3">
    @if (SelectedFile != null)
    {
        <div class="input-group">
            <input type="text" class="form-control" value="@(SelectedFile.Name)">
            <button class="btn btn-danger" type="button" @onclick="RemoveSelection">
                <i class="bx bx-md bx-x"></i>
            </button>
        </div>
    }
    else
    {
        <span class="svg-icon svg-icon-2">
            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path opacity="0.3" d="M10 4H21C21.6 4 22 4.4 22 5V7H10V4Z" fill="currentColor"></path>
                <path d="M10.4 3.60001L12 6H21C21.6 6 22 6.4 22 7V19C22 19.6 21.6 20 21 20H3C2.4 20 2 19.6 2 19V4C2 3.4 2.4 3 3 3H9.20001C9.70001 3 10.2 3.20001 10.4 3.60001ZM16 11.6L12.7 8.29999C12.3 7.89999 11.7 7.89999 11.3 8.29999L8 11.6H11V17C11 17.6 11.4 18 12 18C12.6 18 13 17.6 13 17V11.6H16Z" fill="currentColor"></path>
                <path opacity="0.3" d="M11 11.6V17C11 17.6 11.4 18 12 18C12.6 18 13 17.6 13 17V11.6H11Z" fill="currentColor"></path>
            </svg>
        </span>
    }
</label>

@code
{
    public IBrowserFile? SelectedFile { get; set; }

    private async Task OnFileChanged(InputFileChangeEventArgs arg)
    {
        if (arg.FileCount > 0)
        {
            if (arg.File.Size < 1024 * 1024 * 5)
            {
                SelectedFile = arg.File;
                
                await InvokeAsync(StateHasChanged);
                return;
            }
            
            await ToastService.Error(SmartTranslateService.Translate("The uploaded file should not be bigger than 5MB"));
        }

        SelectedFile = null;
        
        await InvokeAsync(StateHasChanged);
    }

    public async Task RemoveSelection()
    {
        SelectedFile = null;
        await InvokeAsync(StateHasChanged);
    }
}