@using MudBlazor
@using DiunaBI.UI.Shared.Services
@inject AppConfig AppConfig
@inject EntityChangeHubService HubService
@inject AuthService AuthService
@inherits LayoutComponentBase
@implements IDisposable

<AuthGuard>
    <MudThemeProvider Theme="_theme" />
    <MudPopoverProvider />
    <MudDialogProvider />
    <MudSnackbarProvider />

    <MudLayout>
        <MudBreakpointProvider OnBreakpointChanged="OnBreakpointChanged"></MudBreakpointProvider>
        <MudAppBar Elevation="0">
            <MudIconButton Icon="@Icons.Material.Filled.Menu" Color="Color.Inherit" Edge="Edge.Start"
                OnClick="ToggleDrawer" Class="mud-hidden-md-up" />
            <MudSpacer />
            <MudText Typo="Typo.h6">@AppConfig.AppName</MudText>
        </MudAppBar>

        <MudDrawer @bind-Open="_drawerOpen" Anchor="Anchor.Start" Variant="@_drawerVariant" Elevation="1"
            ClipMode="DrawerClipMode.Always" Class="mud-width-250">
            <div class="nav-logo" style="text-align: center; padding: 20px;">
                <a href="https://www.diunabi.com" target="_blank">
                    <img src="_content/DiunaBI.UI.Shared/images/logo.png" alt="DiunaBI"
                        style="max-width: 180px; height: auto;" />
                </a>
            </div>
            <MudNavMenu>
                <MudNavLink Href="/dashboard" Icon="@Icons.Material.Filled.Dashboard">Dashboard</MudNavLink>
                <MudNavLink Href="/layers" Icon="@Icons.Material.Filled.Inventory">Layers</MudNavLink>
                <MudNavLink Href="/datainbox" Icon="@Icons.Material.Filled.Inbox">Data Inbox</MudNavLink>
                <MudNavLink Href="/jobs" Icon="@Icons.Material.Filled.WorkHistory">Jobs</MudNavLink>
            </MudNavMenu>
            <div class="nav-logo" style="text-align: center; padding: 20px;">
                <img src="_content/DiunaBI.UI.Shared/images/clients/@AppConfig.ClientLogo" alt="DiunaBI"
                    style="max-width: 180px; height: auto;" />
            </div>
        </MudDrawer>

        <MudMainContent>
            <MudContainer MaxWidth="MaxWidth.ExtraExtraLarge" Class="my-4">
                @Body
            </MudContainer>
        </MudMainContent>
    </MudLayout>
</AuthGuard>

@code {

    private bool _drawerOpen = true;
    private DrawerVariant _drawerVariant = DrawerVariant.Persistent;

    protected override void OnInitialized()
    {
        // Subscribe to authentication state changes
        AuthService.AuthenticationStateChanged += OnAuthenticationStateChanged;

        // If already authenticated (e.g., from restored session), initialize SignalR
        if (AuthService.IsAuthenticated)
        {
            _ = HubService.InitializeAsync();
        }
    }

    private async void OnAuthenticationStateChanged(bool isAuthenticated)
    {
        if (isAuthenticated)
        {
            Console.WriteLine("🔐 MainLayout: User authenticated, initializing SignalR...");
            await HubService.InitializeAsync();
        }
    }

    public void Dispose()
    {
        AuthService.AuthenticationStateChanged -= OnAuthenticationStateChanged;
    }

    private MudTheme _theme = new MudTheme()
    {
        PaletteLight = new PaletteLight()
        {
            Primary = "#e7163d",
            PrimaryDarken = "#c01234",
            PrimaryLighten = "#f04366",
            Secondary = "#424242",
            AppbarBackground = "#e7163d",
        }
    };

    void ToggleDrawer()
    {
        Console.WriteLine($"ToogleDrawer clickkk {DateTime.Now}");
        _drawerOpen = !_drawerOpen;
    }

    private void OnBreakpointChanged(Breakpoint breakpoint)
    {
        if (breakpoint < Breakpoint.Md)
        {
            _drawerVariant = DrawerVariant.Temporary;
            _drawerOpen = false;
        }
        else
        {
            _drawerVariant = DrawerVariant.Persistent;
            _drawerOpen = true;
        }

        StateHasChanged();
    }

}