@namespace AntSK
@using System.Security.Claims
@using AntSK.Services.Auth
@using Microsoft.AspNetCore.Components.Authorization
@using AntSK.Domain.Options
@using AntSK.Domain.Repositories
@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage
@inject INotificationService _notice
@inherits LayoutComponentBase

<AntDesign.ProLayout.BasicLayout Logo="@("https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg")"
                                 MenuData="_menuData">
    <RightContentRender>
        <AntSK.Components.RightContent />
    </RightContentRender>
    <ChildContent>
        <ErrorBoundary @ref="errorBoundary">
            <ChildContent>
                @Body
            </ChildContent>
            <ErrorContent Context="ex">
                @{
                    ShowNotification(ex);
                }
            </ErrorContent>
        </ErrorBoundary>
    </ChildContent>
    <FooterRender>
        <FooterView Copyright="2024 许泽宇的技术分享" Links="Links"></FooterView>
    </FooterRender>
</AntDesign.ProLayout.BasicLayout>
<SettingDrawer />

@code
{
    ErrorBoundary errorBoundary;
    private MenuDataItem[] _menuData = { };

    [Inject] public HttpClient HttpClient { get; set; }
    [Inject] public AuthenticationStateProvider AuthenticationStateProvider { get; set; }
    [Inject] protected IUsers_Repositories _users_Repositories { get; set; }
    [Inject] protected ProtectedSessionStorage _protectedSessionStore { get; set; }
    private ClaimsPrincipal context => ((AntSKAuthProvider)AuthenticationStateProvider).GetCurrentUser();

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        //菜单权限控制
        var menuList = await HttpClient.GetFromJsonAsync<MenuDataItem[]>("data/menu.json");

        var userSessionStorageResult = await _protectedSessionStore.GetAsync<UserSession>("UserSession");
        var userSession = userSessionStorageResult.Success ? userSessionStorageResult.Value : null;

        if (userSession != null || (bool)context?.Identity.IsAuthenticated)
        {
            string username = "";
            if (userSession != null)
            {
                username = userSession.UserName;
            }
            else
            {
                username = context.Identity.Name;

            }

            if (username == LoginOption.User)
            {
                _menuData = menuList;
            }
            else
            {
                var userMenuList = _users_Repositories.GetFirst(p => p.No == username).MenuRole.Split(",").ToList();
                //非管理员用户不允许操作系统设置
                _menuData = menuList.Where(p => p.Key != "setting" && userMenuList.Contains(p.Key)).ToArray();
            }
        }
    }


    void ShowNotification(Exception ex)
    {
        _ = _notice.Error(new()
            {
                Message = ex.Message,
                Description = ex.StackTrace
            });

        errorBoundary.Recover();
    }

    public LinkItem[] Links { get; set; } =
    {
        new LinkItem
        {
            Key = "许泽宇的技术分享",
            Title = "许泽宇的技术分享",
            Href = "http://studiogpt.cn/",
            BlankTarget = true,
        },
        new LinkItem
        {
            Key = "github",
            Title = (RenderFragment)(@<Icon Type="github" />),
            Href = "https://github.com/AIDotNet/AntSK",
            BlankTarget = true,
        }

    };
}