@using NetinaShop.AdminPanel.PWA.Pages
@using Radzen.Blazor
@inherits LayoutComponentBase
@inject IPWAUpdaterService PwaUpdaterService
@inject NavigationManager NavigationManager
@inject IUserUtility UserUtility
@inject IJSRuntime Js
<style>
    body .pwa-updater[b-pwa-updater] {
        --pwa-updater-bar-height: 40px;
        --pwa-updater-font-size: 16px;
        --pwa-updater-bar-color: rgba(253, 216, 53, 1);
        --pwa-updater-bar-backcolor: #444;
        padding-top: 35px;
        padding-right: 10px;
        padding-bottom: 35px;
        padding-left: 10px;
    }

    body .pwa-updater-updatenow-button {
        border: dashed 2px rgba(253, 216, 53, 1) !important;
        font-family: iranyekan !important;
        font-weight: 800 !important;
    }

    body .pwa-updater-close-button {
        color: #fff !important;
    }
</style>


<AuthorizeView>
    <Authorized>
        <MudRTLProvider RightToLeft="true">
            <MudThemeProvider Theme="MyCustomTheme" />
            <MudDialogProvider />
            <MudSnackbarProvider />
            <RadzenContextMenu />
            <RadzenDialog />

            <MudLayout>
                <MudAppBar class="py-2" Color="Color.Transparent" Fixed="false" Elevation="2">
                    <MudHidden Breakpoint="Breakpoint.SmAndUp">
                        <MudIconButton Icon="@Icons.Material.Filled.Menu" Color="Color.Inherit" Edge="Edge.Start" />
                    </MudHidden>
                    @* <MudAvatar Size="Size.Large" Variant="Variant.Outlined">
                        <MudImage Src="https://img.freepik.com/free-photo/portrait-white-man-isolated_53876-40306.jpg?size=626&ext=jpg&ga=GA1.1.632798143.1705708800&semt=ais"></MudImage>
                    </MudAvatar> *@
                    
                    <RadzenGravatar class="w-14 h-14" Email="@_user?.Email" />
                    <MudStack class="mr-2" Spacing="0">
                        <MudText Color="Color.Inherit" Typo="Typo.body1"><b>@_user?.FullName</b></MudText>
                        <MudText Color="Color.Inherit" Typo="Typo.caption">@_user?.PhoneNumber</MudText>
                    </MudStack>
                    <MudSpacer />
                    <MudIconButton Size="Size.Medium" Color="Color.Inherit" Icon="@Icons.Material.Outlined.Settings"/>
                    <MudIconButton Size="Size.Medium" Color="Color.Error" OnClick="LogoutAsync" Icon="@Icons.Material.Outlined.ExitToApp" />
                </MudAppBar>

                <MudGrid Spacing="0">

                    <MudItem sm="0" md="3" lg="2">
                        <MudHidden Breakpoint="Breakpoint.SmAndDown">
                            <SideBarUi/>
                        </MudHidden>
                    </MudItem>
                    <MudItem sm="12" md="9" lg="10">

                        <div>
                            @Body
                            <div dir="ltr">
                                <PWAUpdater Text="@_updateText" ButtonCaption="اپدیت کنید"/>
                            </div>
                        </div>
                    </MudItem>
                </MudGrid>
            </MudLayout>
        </MudRTLProvider>
        
    </Authorized>
    <NotAuthorized>
        <MudRTLProvider RightToLeft="true">
            <MudThemeProvider Theme="MyCustomTheme" />
            <MudDialogProvider />
            <MudSnackbarProvider />

            <MudLayout>
                <div>
                    <LoginPage/>
                    <div dir="ltr">
                        <PWAUpdater Text="@_updateText" ButtonCaption="اپدیت کنید"/>
                    </div>
                </div>
            </MudLayout>
        </MudRTLProvider>

    </NotAuthorized>
</AuthorizeView>
@code
{
    MudTheme MyCustomTheme = new MudTheme()
        {
            Typography = new Typography()
            {
                Default = new Default()
                {
                    FontFamily = new[] { "iranyekan" }
                }
            },
            Palette = new PaletteLight()
            {
                Primary = "#001A46",
                Secondary = "#E59F2E",
            },
            PaletteDark = new PaletteDark()
            {
                Primary = "#001A46",
                Secondary = "#E59F2E",
            }
        };

    private string _updateText = "! نسخه جدید پنل ادمین رسید";
    private ApplicationUserSDto? _user;
    private async Task LogoutAsync()
    {
        await UserUtility.LogoutAsync();
        NavigationManager.NavigateTo("login",true,true);
    }
    protected override void OnInitialized()
    {
        string? version = typeof(Program)?.Assembly.GetName()?.Version?.ToString();
        if (version != null)
            _updateText = $"نسخه جدید پنل ادمین رسید - {version}";


        base.OnInitialized();
    }

    protected override async Task OnInitializedAsync()
    {
        _user = await UserUtility.GetUserAsync();

        await base.OnInitializedAsync();
    }
}
