@using Microsoft.AspNetCore.Components.Rendering
@namespace Oqtane.UI
@inject IUserService UserService
@inject IModuleService ModuleService
@inject IModuleDefinitionService ModuleDefinitionService

@if (_useadminborder)
{
    <div class="app-pane-admin-border">
        @((MarkupString)_panetitle)
        @DynamicComponent
    </div>
}
else
{
    @DynamicComponent
}

@code {
    private bool _useadminborder = false;
    private string _panetitle = "";

    [CascadingParameter]
    protected PageState PageState { get; set; }

    [Parameter]
    public string Name { get; set; }

    RenderFragment DynamicComponent { get; set; }

    protected override void OnParametersSet()
    {
        if (PageState.EditMode && UserSecurity.IsAuthorized(PageState.User, PermissionNames.Edit, PageState.Page.Permissions) && PageState.Action == Constants.DefaultAction)
        {
            _useadminborder = true;
            _panetitle = "<div class=\"app-pane-admin-title\">" + Name + " Pane</div>";
        }
        else
        {
            _useadminborder = false;
            _panetitle = "";
        }

        DynamicComponent = builder =>
        {
            if (PageState.ModuleId != -1 && PageState.Action != Constants.DefaultAction)
            {
                if (Name.ToLower() == PaneNames.Admin.ToLower())
                {
                    Module module = PageState.Modules.FirstOrDefault(item => item.ModuleId == PageState.ModuleId);
                    if (module != null && !module.IsDeleted)
                    {
                        var moduleType = Type.GetType(module.ModuleType);
                        if (moduleType != null)
                        {
                            bool authorized = false;
                            if (Constants.DefaultModuleActions.Contains(PageState.Action))
                            {
                                authorized = UserSecurity.IsAuthorized(PageState.User, PermissionNames.Edit, PageState.Page.Permissions);
                            }
                            else
                            {
                                switch (module.SecurityAccessLevel)
                                {
                                    case SecurityAccessLevel.Anonymous:
                                        authorized = true;
                                        break;
                                    case SecurityAccessLevel.View:
                                        authorized = UserSecurity.IsAuthorized(PageState.User, PermissionNames.View, module.Permissions);
                                        break;
                                    case SecurityAccessLevel.Edit:
                                        authorized = UserSecurity.IsAuthorized(PageState.User, PermissionNames.Edit, module.Permissions);
                                        break;
                                    case SecurityAccessLevel.Admin:
                                        authorized = UserSecurity.IsAuthorized(PageState.User, RoleNames.Admin);
                                        break;
                                    case SecurityAccessLevel.Host:
                                        authorized = UserSecurity.IsAuthorized(PageState.User, RoleNames.Host);
                                        break;
                                }
                            }

                            if (authorized)
                            {
                                CreateComponent(builder, module);
                            }
                        }
                        else
                        {
                            // module type does not exist
                        }
                    }
                }
            }
            else
            {
                if (PageState.ModuleId != -1)
                {
                    Module module = PageState.Modules.FirstOrDefault(item => item.ModuleId == PageState.ModuleId);
                    if (module != null && module.Pane.ToLower() == Name.ToLower() && !module.IsDeleted)
                    {
                        // check if user is authorized to view module
                        if (UserSecurity.IsAuthorized(PageState.User, PermissionNames.View, module.Permissions))
                        {
                            CreateComponent(builder, module);
                        }
                    }
                }
                else
                {
                    foreach (Module module in PageState.Modules.Where(item => item.PageId == PageState.Page.PageId && item.Pane.ToLower() == Name.ToLower() && !item.IsDeleted).OrderBy(x => x.Order).ToArray())
                    {
                        // check if user is authorized to view module
                        if (UserSecurity.IsAuthorized(PageState.User, PermissionNames.View, module.Permissions))
                        {
                            CreateComponent(builder, module);
                        }
                    }
                }
            }
        };
    }

    private void CreateComponent(RenderTreeBuilder builder, Module module)
    {
        builder.OpenComponent(0, Type.GetType(Constants.ContainerComponent));
        builder.AddAttribute(1, "ModuleState", module);
        builder.SetKey(module.PageModuleId);
        builder.CloseComponent();
    }
}
