@using Moonlight.Core.Services
@using Moonlight.Core.Models.Abstractions.Feature

@inject FeatureService FeatureService
@inject NavigationManager Navigation
@inject IdentityService IdentityService

@{
    var uri = new Uri(Navigation.Uri);
    var path = uri.LocalPath.TrimEnd('/');

    if (string.IsNullOrEmpty(path))
        path = "/";

    bool IsMatch(SidebarItem item)
    {
        if (item.NeedsExactMath)
            return path == item.Target;

        return path!.StartsWith(item.Target);
    }
}

<div class="app-sidebar flex-column">
    <div class="app-sidebar-navs flex-column-fluid">
        <div class="app-sidebar-wrapper">
            <div class="mt-8">
                <div class="app-sidebar-menu-primary menu menu-column menu-rounded menu-sub-indention menu-state-bullet-primary px-3 mb-5">
                    @foreach (var sidebarItem in FeatureService.UiContext.SidebarItems.Where(x => !x.IsAdmin).OrderBy(x => x.Index).ToArray())
                    {
                        if (IsMatch(sidebarItem))
                        {
                            <div class="menu-item">
                                <a href="@(sidebarItem.Target)" class="menu-link border-start border-3 border-primary rounded-start-0 bg-secondary">
                                    <span class="menu-icon">
                                        <i class="bx bx-sm @(sidebarItem.Icon) text-white"></i>
                                    </span>
                                    <span class="menu-title fs-4 text-white fw-normal">
                                        @(sidebarItem.Name)
                                    </span>
                                </a>
                            </div>
                        }
                        else
                        {
                            <div class="menu-item">
                                <a href="@(sidebarItem.Target)" class="menu-link ms-1 bg-hover-secondary">
                                    <span class="menu-icon">
                                        <i class="bx bx-sm @(sidebarItem.Icon)"></i>
                                    </span>
                                    <span class="menu-title fs-4 fw-normal">
                                        @(sidebarItem.Name)
                                    </span>
                                </a>
                            </div>
                        }
                    }

                    @if (IdentityService.IsLoggedIn && IdentityService.CurrentUser.Permissions > 0)
                    {
                        <div class="menu-item my-5">
                            <div class="app-sidebar-separator separator"></div>
                        </div>

                        @foreach (var sidebarItem in FeatureService.UiContext.SidebarItems.Where(x => x.IsAdmin).OrderBy(x => x.Index).ToArray())
                        {
                            if (IsMatch(sidebarItem))
                            {
                                <div class="menu-item">
                                    <a href="@(sidebarItem.Target)" class="menu-link border-start border-3 border-primary rounded-start-0 bg-secondary">
                                        <span class="menu-icon">
                                            <i class="bx bx-sm @(sidebarItem.Icon) text-white"></i>
                                        </span>
                                        <span class="menu-title fs-4 text-white fw-normal">
                                            @(sidebarItem.Name)
                                        </span>
                                    </a>
                                </div>
                            }
                            else
                            {
                                <div class="menu-item">
                                    <a href="@(sidebarItem.Target)" class="menu-link ms-1 bg-hover-secondary">
                                        <span class="menu-icon">
                                            <i class="bx bx-sm @(sidebarItem.Icon)"></i>
                                        </span>
                                        <span class="menu-title fs-4 fw-normal">
                                            @(sidebarItem.Name)
                                        </span>
                                    </a>
                                </div>
                            }
                        }
                    }
                </div>
            </div>
        </div>
    </div>
</div>

@code
{
    protected override void OnAfterRender(bool firstRender)
    {
        if (firstRender)
        {
            Navigation.LocationChanged += async (_, _) =>
            {
                try
                {
                    await InvokeAsync(StateHasChanged);
                }
                catch (Exception) { /* just in case... */ }
            };
        }
    }
}
