@page "/user"
@attribute [Authorize]
@using SteUp.Shared.Components.Layout
@using SteUp.Shared.Components.Layout.Overlay
@using SteUp.Shared.Components.SingleElements
@using SteUp.Shared.Core.Authorization.Enum
@using SteUp.Shared.Core.Dto
@using SteUp.Shared.Core.Interface.IntegryApi
@using SteUp.Shared.Core.Interface.System
@using SteUp.Shared.Core.Services
@using SteUp.Shared.Core.Utility
@inject AppAuthenticationStateProvider AuthenticationStateProvider
@inject INetworkService NetworkService
@inject IGenericSystemService GenericSystemService
@inject IFileManager FileManager
@inject IIntegryApiService IntegryApiService

<HeaderLayout Title="Profilo"/>

@if (IsLoggedIn)
{
    <div class="container content pb-safe-area">
        <div class="container-primary-info mud-elevation-1">
            <div class="section-primary-info">
                <MudAvatar Style="height: 70px; width: 70px; font-size: 2rem; font-weight: bold"
                           Color="Color.Secondary">
                    @UtilityString.ExtractInitials(UserSession.User.Fullname)
                </MudAvatar>

                <div class="personal-info">
                    <span class="info-nome">@UserSession.User.Fullname</span>
                    @if (UserSession.User.KeyGroup is not null)
                    {
                        <span
                            class="info-section">@(((KeyGroupEnum)UserSession.User.KeyGroup).ConvertToHumanReadable())</span>
                    }
                </div>
            </div>

            <div class="divider"></div>

            <div class="section-info">
                <div class="section-personal-info">
                    <div>
                        <span class="info-title">Profilo azienda</span>
                        <span class="info-text">@CodHash</span>
                    </div>
                </div>
                
                <div class="section-personal-info">
                    <div>
                        <span class="info-title">Status</span>
                        @if (NetworkService.ConnectionAvailable)
                        {
                            <div class="status online">
                                <i class="ri-wifi-line"></i>
                                <span>Online</span>
                            </div>
                        }
                        else
                        {
                            <div class="status offline">
                                <i class="ri-wifi-off-line"></i>
                                <span>Offline</span>
                            </div>
                        }
                    </div>
                </div>
            </div>
        </div>
        
        <div class="container-button ripple-container mud-elevation-1">
            <MudButton Class="button-settings red-icon"
                       FullWidth="true"
                       StartIcon="@Icons.Material.Rounded.UploadFile"
                       Size="Size.Medium"
                       OnClick="@ExportLog"
                       Variant="Variant.Outlined">
                Esporta log
            </MudButton>
        </div>

        <div class="container-button ripple-container mud-elevation-1">
            <MudButton Class="button-settings green-icon"
                       FullWidth="true"
                       StartIcon="@Icons.Material.Outlined.Sync"
                       Size="Size.Medium"
                       OnClick="@UpdateDb"
                       Variant="Variant.Outlined">
                Sincronizza ispezioni esportate
            </MudButton>
        </div>

        <div class="container-button ripple-container mud-elevation-1">
            <MudButton Class="button-settings exit"
                       FullWidth="true"
                       Color="Color.Error"
                       Size="Size.Medium"
                       OnClick="@Logout"
                       Variant="Variant.Outlined">
                Esci
            </MudButton>
        </div>
        
        <AppVersion/>
    </div>
}

<SpinnerOverlay VisibleOverlay="VisibleOverlay"/>

@code {
    private bool IsLoggedIn { get; set; }
    private bool VisibleOverlay { get; set; }
    private string? CodHash { get; set; } = "";

    protected override async Task OnInitializedAsync()
    {
        IsLoggedIn = await UserSession.IsLoggedIn();
        CodHash = LocalStorage.GetString("codHash");
        StateHasChanged();
    }

    private async Task ExportLog()
    {
        VisibleOverlay = true;
        StateHasChanged();
        
        var profiloAzienda = LocalStorage.GetString("codHash");
        
        var email = new SendEmailDto
        {
            FromName = "Integry Log",
            To = "developer@integry.it",
            Subject = $"SteUP - Log del {DateTime.Today:d} di {UserSession.User.Username}",
            IsHtml = true,
            MsgText = $"Username: <b>{UserSession.User.Username}</b><br/>" +
                      $"Profilo azienda: <b>{profiloAzienda}</b><br/>" +
                      $"ProfileDb: <b>{UserSession.ProfileDb}</b><br/>" +
                      $"Versione app: <b>{GenericSystemService.GetCurrentAppVersion()}</b>",
            Attachments = FileManager.GetFileForExport()
        };
        
        await IntegryApiService.SendEmail(email);
        
        VisibleOverlay = false;
        StateHasChanged();
    }

    private async Task UpdateDb()
    {
        VisibleOverlay = true;
        StateHasChanged();

        await SteupDataService.CheckAndUpdateStatus();
        
        VisibleOverlay = false;
        StateHasChanged();
    }

    private async Task Logout()
    {
        await AuthenticationStateProvider.SignOut();
        IsLoggedIn = await UserSession.IsLoggedIn();
        StateHasChanged();
    }

}