@using Vaitr.Bus;

@inherits LayoutComponentBase
@implements IDisposable

@inject Bus Bus

<MudSnackbarProvider />
<MudDialogProvider />

<MudLayout>
    <MudAppBar Elevation="0" Fixed>
        <MudLink Href="@Navigation.Home" Typo="Typo.h6" Underline="Underline.None" Class="mr-4" Style="color: white !important; text-transform:uppercase;">
            @Global.Name
        </MudLink>
        <AuthorizeView>
            <Authorized>
                <MudIconButton OnClick="()=>_drawer?.Toggle()" Icon="@Icons.Material.Filled.Menu" Color="Color.Inherit" Edge="Edge.Start" />
            </Authorized>
        </AuthorizeView>
        <MudSpacer />
        <ChatProvider />
        <ThemeProvider />
        <ProfileProvider />
    </MudAppBar>
    <AuthorizeView>
        <Authorized>
            <CascadingValue Value="RouteValues">
                <DrawerProvider @ref="_drawer" />
            </CascadingValue>
        </Authorized>
    </AuthorizeView>
    <MudMainContent>
        <MudContainer MaxWidth="MaxWidth.ExtraExtraLarge">
            @Body
        </MudContainer>
    </MudMainContent>
</MudLayout>

@code{
    public IReadOnlyDictionary<string, object>? RouteValues { get; set; }

    private DrawerProvider? _drawer;
    private bool _disposed;
    
    protected override void OnParametersSet()
    {
        RouteValues = (Body?.Target as RouteView)?.RouteData.RouteValues;

        base.OnParametersSet();
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {

        }

        await Bus.PublishAsync(Events.Layout.Rendered).ConfigureAwait(false);
    }

    public void Dispose()
    {
        Dispose(disposing: true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        // auto disposed when starved circuit timeouts (disconnects)
        if (_disposed is false)
        {
            if (disposing)
            {

            }

            _disposed = true;
        }
    }
}