<?php

namespace Hura8\Components\Staff\AdminController;

use Hura8\System\Permission;
use Hura8\Traits\ClassCacheTrait;

class ClientPermissionController
{

    use ClassCacheTrait;

    public function getClientEntityPermission(){
        return static::getCache("getClientEntityPermission", function (){
            return $this->getClientEntityPermission_raw();
        });
    }

    protected function getClientEntityPermission_raw() {

        $system_file = ROOT_DIR. "/config/system/admin.entity.permission.php";
        $entity_group = include $system_file;

        $client_allowed_entities  = Permission::getClientEntities();

        $final_config = [];
        foreach ($entity_group as $_group) {

            $settings = include ROOT_DIR. "/config/system/entity_permission/".$_group.".php";

            $children_match = [];
            foreach ($settings['children'] as $_entity => $_p) {
                if(in_array($_entity, $client_allowed_entities)) {
                    $children_match[$_entity] = $_p;
                }
            }

            if(sizeof($children_match)) {
                $final_config[$_group] = [
                    'title' => $settings['title'],
                    'children' => $children_match,
                ];
            }
        }


        return $final_config;
    }


    public function getClientMenu() {
        return static::getCache("getClientMenu", function (){
            $menu_config_file = ROOT_DIR. "/config/client/admin/admin.menu.php";

            $header_admin_config = [];
            $menu_group = include $menu_config_file;
            foreach ($menu_group as $_group) {
                $content = include ROOT_DIR. "/config/client/admin/admin_menu/".$_group.".php";
                $enabled_menu_item = array_filter($content['menu'], function ($item){ return $item['enable'];});

                if(sizeof($enabled_menu_item) > 0) {
                    $header_admin_config[$_group] = [
                        'enable' => $content['enable'],
                        'name'  => $content['name'],
                        'url'   => $content['url'],
                        'menu'  => $enabled_menu_item,
                    ];
                }
            }

            return $header_admin_config;
        });
    }


}
