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

@inject SmartTranslateService SmartTranslateService
@inject AlertService AlertService
@inject ToastService ToastService
@inject ServerService ServerService

<WButton Text="@(SmartTranslateService.Translate("Reset"))"
         WorkingText="@(SmartTranslateService.Translate("Resetting"))"
         CssClasses="btn-danger"
         OnClick="OnClick">
</WButton>

@code
{
    [CascadingParameter]
    public Server CurrentServer { get; set; }

    private async Task OnClick()
    {
        var b = await AlertService.YesNo(
            SmartTranslateService.Translate("Are you sure you want to reset this server?"),
            SmartTranslateService.Translate("Are you sure? This cannot be undone"),
                SmartTranslateService.Translate("Yes"),
                SmartTranslateService.Translate("No")
        );

        if (b)
        {
            await ToastService.CreateProcessToast("serverReset", SmartTranslateService.Translate("Resetting server"));

            var access = await ServerService.CreateFileAccess(CurrentServer, null!);

            var files = await access.Ls();

            int i = 1;
            foreach (var file in files)
            {
                await access.Delete(file);
                await ToastService.UpdateProcessToast("serverReset", $"{SmartTranslateService.Translate("Deleted file")} {i}/{files.Length}");
                i++;
            }

            await ToastService.UpdateProcessToast("serverReset", SmartTranslateService.Translate("Reinstalling server"));

            await ServerService.Reinstall(CurrentServer);
            await ToastService.RemoveProcessToast("serverReset");
        }
    }
}
