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

@inject ToastService ToastService
@inject SmartTranslateService SmartTranslateService

<InputFile OnChange="OnFileChanged" type="file" id="fileUpload" hidden="" multiple=""/>
<label for="fileUpload" class="btn btn-primary me-3 @(Uploading ? "disabled" : "")">
    @if (Uploading)
    {
        <span>@(Percent)%</span>
    }
    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>
        <TL>Upload</TL>
    }
</label>

@code
{
    [Parameter]
    public FileAccess Access { get; set; }
    
    [Parameter]
    public Func<Task> OnUploadComplete { get; set; }

    private bool Uploading = false;
    private int Percent = 0;

    private async Task OnFileChanged(InputFileChangeEventArgs arg)
    {
        await ToastService.CreateProcessToast("upload", SmartTranslateService.Translate("Uploading files"));

        Uploading = true;
        await InvokeAsync(StateHasChanged);

        int i = 1;
        foreach (var browserFile in arg.GetMultipleFiles())
        {
            if (browserFile.Size < 1024 * 1024 * 100)
            {
                Percent = 0;

                try
                {
                    await Access.Upload(
                        browserFile.Name,
                        browserFile.OpenReadStream(1024 * 1024 * 100),
                        async (i) =>
                        {
                            Percent = i;

                            Task.Run(() => { InvokeAsync(StateHasChanged); });
                        });

                    OnUploadComplete?.Invoke();
                }
                catch (Exception e)
                {
                    await ToastService.Error(SmartTranslateService.Translate("An unknown error occured while uploading a file"));
                    Logger.Error("Error uploading file");
                    Logger.Error(e);
                }

                await ToastService.UpdateProcessToast("upload", $"{i}/{arg.GetMultipleFiles().Count} {SmartTranslateService.Translate("complete")}");
            }
            else
            {
                await ToastService.Error(SmartTranslateService.Translate("The uploaded file should not be bigger than 100MB"));
            }

            i++;
        }

        Uploading = false;
        await InvokeAsync(StateHasChanged);

        await ToastService.UpdateProcessToast("upload", SmartTranslateService.Translate("Upload complete"));
        await ToastService.RemoveProcessToast("upload");
    }
}
