@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.PermissionList) && PageState.Action == Constants.DefaultAction)
        {
            _useadminborder = true;
            _panetitle = "<div class=\"app-pane-admin-title\">" + Name + " Pane</div>";
        }
        else
        {
            _useadminborder = false;
            _panetitle = "";
        }

        DynamicComponent = builder =>
        {
            foreach (Module module in PageState.Modules.Where(item => item.PageId == PageState.Page.PageId))
            {
                // set renderid - this allows the framework to determine which components should be rendered when PageState changes
                if (module.RenderId != PageState.RenderId)
                {
                    module.RenderId = PageState.RenderId;
                }

                var pane = module.Pane;
                if (module.ModuleId == PageState.ModuleId && PageState.Action != Constants.DefaultAction)
                {
                    if (PageState.Page.Panes.FindIndex(item => item.Equals(PaneNames.Default, StringComparison.OrdinalIgnoreCase)) != -1)
                    {
                        pane = PaneNames.Default;
                    }
                    else
                    {
                        pane = PaneNames.Admin;
                    }
                }

                // pane matches current pane
                if (Name.ToLower() == pane.ToLower())
                {
                    if (PageState.ModuleId == module.ModuleId && PageState.Action != Constants.DefaultAction)
                    {
                        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.PermissionList);
                            }
                            else
                            {
                                switch (module.SecurityAccessLevel)
                                {
                                    case SecurityAccessLevel.Anonymous:
                                        authorized = true;
                                        break;
                                    case SecurityAccessLevel.View:
                                        authorized = UserSecurity.IsAuthorized(PageState.User, PermissionNames.View, module.PermissionList);
                                        break;
                                    case SecurityAccessLevel.Edit:
                                        authorized = UserSecurity.IsAuthorized(PageState.User, PermissionNames.Edit, module.PermissionList);
                                        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, module.PageModuleId);
                            }
                        }
                    }
                    else
                    {
                        if (PageState.ModuleId == -1 || PageState.ModuleId == module.ModuleId)
                        {
                            // check if user is authorized to view module
                            if (UserSecurity.IsAuthorized(PageState.User, PermissionNames.View, module.PermissionList))
                            {
                                CreateComponent(builder, module, -1);
                            }
                        }
                    }
                }
            }            
        };
    }

    private void CreateComponent(RenderTreeBuilder builder, Module module, int key)
    {
        builder.OpenComponent(0, typeof(ContainerBuilder));
        builder.AddAttribute(1, "ModuleState", module);
        if (key != -1)
        {
            builder.SetKey(module.PageModuleId);
        }
        builder.CloseComponent();
    }
}
