@using System.Text
@using Blazored.LocalStorage
@using Iceshrimp.Assets.PhosphorIcons
@using Iceshrimp.Frontend.Core.InMemoryLogger
@using Iceshrimp.Frontend.Core.Services
@using Iceshrimp.Frontend.Localization
@using Microsoft.Extensions.Localization
@inject IStringLocalizer<Localization> Loc;
@inject NavigationManager              Navigation;
@inject VersionService                 Version;
@inject InMemoryLogService             LogService;
@inject IJSRuntime                     Js;
@inject ISyncLocalStorageService       LocalStorage;
@inject SessionService                 Session;

<div class="error-ui">
    <h3>@Loc["Unhandled Exception has occured"]</h3>
    <div class="body">
        <div class="text">
            <div>
                @Loc["If this issue happens more than once, please join our support chat at: "]<a href="https://chat.iceshrimp.dev/">https://chat.iceshrimp.dev/</a><br/>
                @Loc["Providing the below stack trace and version information will aid in debugging."]
            </div>
        </div>
        <div class="log-block">
            <pre><code>@Exception.GetType(): @Exception.Message
@Exception.StackTrace</code></pre>
        </div>
        <div class="version">
            <span class="name">Iceshrimp.NET</span>
            <span class="value">
                <code>@Version.Version</code>
            </span>
            <span class="name">Codename</span>
            <span class="value">
                <code>@Version.Codename</code>
            </span>
            @if (Version.CommitHash != null)
            {
                <span class="name">Commit</span>
                <span class="value">
                    <code>@Version.CommitHash</code>
                </span>
            }
        </div>
        @Loc["Logs"]
        @Loc["These logs may contain sensitive information, please do not post them publicly.\n" + "Providing them to developers upon request may help with debugging."]
        <div class="log-block">
            <pre><code>
                @foreach (var line in _rawLog)
                {
                    @line
                }
            </code></pre>
        </div>
    </div>
    <button class="button" @onclick="Reload"><Icon Name="Icons.ArrowsClockwise"/>@Loc["Reload Application"]</button>
    <button class="button" @onclick="DownloadLogs"><Icon Name="Icons.DownloadSimple"/>@Loc["Download Logs"]</button>
    <button class="button" @onclick="ClearStorage"><Icon Name="Icons.Warning"/>@Loc["Reset local data"]</button>

</div>

@code {
    [Parameter] [EditorRequired] public required Exception                   Exception { get; set; }
    private                                      IEnumerable<string>         _logs   = [];
    private                                      IJSInProcessObjectReference _module = null!;
    private                                      List<string>                _rawLog = null!;

    private void Reload()
    {
        Navigation.Refresh(true);
    }

    protected override async Task OnInitializedAsync()
    {
        _rawLog = LogService.GetLogs();
        _module = (IJSInProcessObjectReference)await Js.InvokeAsync<IJSObjectReference>("import", "./Components/ErrorUi.razor.js");
    }

    private void DownloadLogs()
    {
        var logBytes = LogService.GetLogs().SelectMany(p => Encoding.UTF8.GetBytes(p)).ToArray();
        _module.InvokeVoid("DownloadFile", "log.txt", "text/plain", logBytes);
    }

    private void ClearStorage()
    {
        Session.EndSession();
        LocalStorage.Clear();
        Navigation.NavigateTo("/", true);
    }
}