@inject NavigationManager Navigation
@inject IJSRuntime JS
@inject VersionInfo VersionInfo
@implements IDisposable

<MudPopoverProvider />
<MudDialogProvider />
<MudSnackbarProvider />

<MudLayout Class="admin-shell">
    <MudAppBar Elevation="0" Class="admin-topbar">
        <MudIconButton Icon="@Icons.Material.Filled.Menu"
                       Color="Color.Inherit"
                       Edge="Edge.Start"
                       Class="admin-menu-button"
                       OnClick="@ToggleDrawer" />
        <div class="admin-topbar-title">
            <MudText Typo="Typo.body2" Class="font-weight-bold admin-brand-text">TaxBaik</MudText>
            <MudText Typo="Typo.body2" Class="admin-brand-subtitle">세무회계 관리 대시보드</MudText>
        </div>
        <MudSpacer />

        <div class="admin-topbar-actions">
            <MudButton Class="admin-topbar-action"
                       Variant="Variant.Text"
                       Color="Color.Inherit"
                       Size="Size.Small"
                       StartIcon="@Icons.Material.Filled.OpenInNew"
                       Href="/taxbaik"
                       Target="_blank">
                공개 사이트
            </MudButton>

            <MudDivider Vertical="true" FlexItem="true" Class="mx-2" />

            <MudButton Class="admin-topbar-action"
                       Variant="Variant.Text"
                       Color="Color.Error"
                       Size="Size.Small"
                       StartIcon="@Icons.Material.Filled.Logout"
                       Href="/taxbaik/admin/logout">
                로그아웃
            </MudButton>
        </div>
    </MudAppBar>

    <MudDrawer @bind-open="@drawerOpen"
               Elevation="0"
               Variant="DrawerVariant.Responsive"
               Breakpoint="Breakpoint.Md"
               Class="admin-drawer">
        <div class="admin-drawer-brand">
            <div class="admin-brand-mark">T</div>
            <div>
                <MudText Typo="Typo.subtitle1">TaxBaik</MudText>
                <MudText Typo="Typo.caption">세무 운영 콘솔</MudText>
            </div>
        </div>
        <MudNavMenu Class="admin-nav">
            <MudNavLink Href="/taxbaik/admin/dashboard" Match="NavLinkMatch.All" Icon="@Icons.Material.Filled.Dashboard">대시보드</MudNavLink>
            <MudNavGroup Title="CRM & 세무관리" Icon="@Icons.Material.Filled.BusinessCenter" @bind-Expanded="@expandedCRMGroup">
                <MudNavLink Href="/taxbaik/admin/tax-profiles" Icon="@Icons.Material.Filled.Assignment">세무 프로필</MudNavLink>
                <MudNavLink Href="/taxbaik/admin/tax-filing-schedules" Icon="@Icons.Material.Filled.CalendarMonth">신고 일정</MudNavLink>
                <MudNavLink Href="/taxbaik/admin/contracts" Icon="@Icons.Material.Filled.Description">계약 관리</MudNavLink>
                <MudNavLink Href="/taxbaik/admin/consulting-activities" Icon="@Icons.Material.Filled.ChatBubble">상담 활동</MudNavLink>
                <MudNavLink Href="/taxbaik/admin/revenue-trackings" Icon="@Icons.Material.Filled.Receipt">수익 추적</MudNavLink>
            </MudNavGroup>
            <MudNavGroup Title="고객 관리" Icon="@Icons.Material.Filled.PeopleAlt" @bind-Expanded="@expandedCustomerGroup">
                <MudNavLink Href="/taxbaik/admin/clients" Icon="@Icons.Material.Filled.ContactPage">고객 카드</MudNavLink>
                <MudNavLink Href="/taxbaik/admin/tax-filings" Icon="@Icons.Material.Filled.Assessment">세무신고</MudNavLink>
            </MudNavGroup>
            <MudNavGroup Title="홈페이지" Icon="@Icons.Material.Filled.Home" @bind-Expanded="@expandedWebsiteGroup">
                <MudNavLink Href="/taxbaik/admin/announcements" Icon="@Icons.Material.Filled.Campaign">공지사항</MudNavLink>
                <MudNavLink Href="/taxbaik/admin/faqs" Icon="@Icons.Material.Filled.QuestionAnswer">FAQ 관리</MudNavLink>
                <MudNavLink Href="/taxbaik/admin/blog" Icon="@Icons.Material.Filled.Article">블로그 관리</MudNavLink>
                <MudNavLink Href="/taxbaik/admin/season-simulator" Icon="@Icons.Material.Filled.Preview">시즌 시뮬레이터</MudNavLink>
            </MudNavGroup>
            <MudNavLink Href="/taxbaik/admin/inquiries" Icon="@Icons.Material.Filled.Forum">문의 관리</MudNavLink>
            <MudNavLink Href="/taxbaik/admin/settings" Icon="@Icons.Material.Filled.Tune">설정</MudNavLink>
            <MudNavLink Href="/taxbaik/admin/common-codes" Icon="@Icons.Material.Filled.Category">공통관리</MudNavLink>
        </MudNavMenu>
        <div class="admin-drawer-version">
            <div class="admin-drawer-version-label">Version</div>
            <div class="admin-drawer-version-value">v@(VersionInfo.Version)</div>
            <div class="admin-drawer-version-built">@VersionInfo.Built</div>
        </div>
    </MudDrawer>

    <MudMainContent Class="admin-main">
        <MudContainer MaxWidth="MaxWidth.False" Class="admin-content">
            @ChildContent
        </MudContainer>
    </MudMainContent>
</MudLayout>

@code {
    [Parameter]
    public RenderFragment? ChildContent { get; set; }

    private bool drawerOpen = true;
    private bool expandedCRMGroup = true;
    private bool expandedCustomerGroup = false;
    private bool expandedWebsiteGroup = false;

    protected override void OnInitialized()
    {
        Navigation.LocationChanged += OnLocationChanged;
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await JS.InvokeVoidAsync("taxbaikAdminSession.setContext", "admin/shell", "navigation", "layout", "shell", "shell", "", "main");
            await JS.InvokeVoidAsync("taxbaikAdminSession.hideLoading");
        }
    }

    private void OnLocationChanged(object? sender, LocationChangedEventArgs args)
    {
        var route = new Uri(args.Location).AbsolutePath;
        _ = JS.InvokeVoidAsync("taxbaikAdminSession.setContext", route, "navigation", "route-change", "layout", "shell", "", route);
        _ = InvokeAsync(() => JS.InvokeVoidAsync("taxbaikAdminSession.hideLoading"));
    }

    private void ToggleDrawer()
    {
        drawerOpen = !drawerOpen;
        _ = JS.InvokeVoidAsync("taxbaikAdminSession.setContext", "admin/shell", "navigation", "drawer", drawerOpen ? "opened" : "closed", "shell", "", "drawer");
        _ = JS.InvokeVoidAsync("taxbaikAdminSession.traceUiState", "admin-shell", drawerOpen ? "drawer opened" : "drawer closed");
    }

    public void Dispose()
    {
        Navigation.LocationChanged -= OnLocationChanged;
    }
}
