@inject IStringLocalizer<Localization> Loc;
@using Iceshrimp.Assets.PhosphorIcons
@using Iceshrimp.Frontend.Components
@using Iceshrimp.Frontend.Core.Services
@using Iceshrimp.Frontend.Localization
@using Microsoft.Extensions.Localization
@implements IDisposable
@inject NavigationManager              Navigation;
@inject SessionService Session;

<GlobalComponents></GlobalComponents>
<div @ref="SidebarElementRef" class="sidebar @(_open ? "open" : "")" tabindex="0">
    <div class="header">
        @*ToDo: account dropdown*@
        <div @ref="_avatar" @onclick="OpenAccountDropdown" class="avatar" @onclick:stopPropagation="true" >
            <UserAvatar User="Session.Current"/>
            <AccountDropdown @ref="_accountDropdown" />
        </div>
    </div>
    <div class="nav">
        <NavLink href="/" Match="NavLinkMatch.All">
            <div class="sidebar-btn">
                <Icon Name="Icons.House"/>
                <span class="text">@Loc["Timeline"]</span>
            </div>
        </NavLink>
        <NavLink href="/notifications">
            <div class="sidebar-btn">
                <Icon Name="Icons.Bell"/>
                <span class="text">@Loc["Notifications"]</span>
            </div>
        </NavLink>
        <NavLink href="/search">
            <div class="sidebar-btn">
                <Icon Name="Icons.MagnifyingGlass"/>
                <span class="text">@Loc["Search"]</span>
            </div>
        </NavLink>
        <NavLink href="/follow-requests">
            <div class="sidebar-btn">
                <Icon Name="Icons.HandWaving"/>
                <span class="text">@Loc["Follow requests"]</span>
            </div>
        </NavLink>
        <NavLink href="/drive">
            <div class="sidebar-btn">
                <Icon Name="Icons.Cloud"></Icon>
                <span class="text">@Loc["Drive"]</span>
            </div>
        </NavLink>
        <NavLink href="/settings/">
            <div class="sidebar-btn">
                <Icon Name="Icons.Gear"></Icon>
                <span class="text">@Loc["Settings"]</span>
            </div>
        </NavLink>
        @if ((Session?.Current?.IsAdmin ?? false) || (Session?.Current?.IsModerator ?? false))
        {
            <NavLink href="/mod">
                <div class="sidebar-btn">
                    <Icon Name="Icons.Shield"></Icon>
                    <span class="text">@Loc["Moderation"]</span>
                </div>
            </NavLink>
        }
    <hr class="rule"/>
    <button class="sidebar-btn post-btn" @onclick="Open">
        <Icon Name="Icons.Pencil"></Icon>@Loc["Note"]
    </button>
    </div>
    @if (_open)
    {
        <ClosingBackdrop OnClose="Close"/>
    }
</div>

<div class="bottom-bar">
    <div class="bottom-nav">
        <button @onclick="ToggleSidebar" class="bottom-bar-btn">
            <Icon Name="Icons.List" class="bottom-bar-icon"/>
        </button>
        <NavLink href="/" Match="NavLinkMatch.All">
            <div class="bottom-bar-btn">
                <Icon Name="Icons.House" class="bottom-bar-icon"/>
            </div>
        </NavLink>
        <NavLink href="/notifications">
            <div class="bottom-bar-btn">
                <Icon Name="Icons.Bell" class="bottom-bar-icon"/>
            </div>
        </NavLink>
        <div>
            <button @onclick="Open" class="bottom-bar-btn post-btn">
                <Icon Name="Icons.Pencil" class="bottom-bar-icon"/>
            </button>
        </div>
    </div>

</div>

<Compose @ref="_compose"/>

@code {
    private Compose          _compose = null!;
    private bool             _open    = false;
    private ElementReference SidebarElementRef { get; set; }
    private AccountDropdown  _accountDropdown = null!;
    private ElementReference _avatar;

    private void OpenAccountDropdown()
    {
        _accountDropdown.Menu.Toggle(_avatar, false);
    }

    private async Task Open()
    {
        await _compose.OpenDialog();
    }

    private void ToggleSidebar()
    {
        _open = !_open;
        StateHasChanged();
    }

    private void Close()
    {
        _open = false;
        StateHasChanged();
    }

    private void HandleLocationChanged(object? sender, LocationChangedEventArgs e)
    {
        _open = false;
        StateHasChanged();
    }

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

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