<?php

namespace Hura8\Components\Staff\AdminController;

use Hura8\Components\Staff\Model\StaffDepartmentPermissionModel;


class StaffDepartmentPermissionController extends ClientPermissionController
{

    protected  $objStaffDepartmentPermissionModel;


    public function __construct() {
        $this->objStaffDepartmentPermissionModel = new StaffDepartmentPermissionModel();
    }


    public function getDepartmentMenuPermissionSetting($department_id) {

        $current_permission = $this->getDepartmentMenuPermission($department_id);

        $user_menu_settings = [];
        foreach ($this->getClientMenu() as $group_id => $group_info) {

            $rebuild_menu = [];
            foreach ($group_info['menu'] as $index => $menu) {
                $rebuild_menu[$index] = $menu;

                $rebuild_menu[$index]['is_user_permitted'] = (in_array($menu['id'], $current_permission)) ? 1 : 0;
            }

            $user_menu_settings[$group_id] = $group_info;
            $user_menu_settings[$group_id]['menu'] = $rebuild_menu;
        }

        return $user_menu_settings;
    }


    public function saveDepartmentMenuPermission($department_id, array $new_permission) {
        $this->objStaffDepartmentPermissionModel->saveDepartmentMenuPermission($department_id, $new_permission);
    }


    public function getDepartmentMenuPermission($department_id) {
        return $this->objStaffDepartmentPermissionModel->getDepartmentMenuPermission($department_id);
    }


    public function getDepartmentEntityPermissionSetting($department_id) {

        $current_permission = $this->getDepartmentEntityPermission($department_id);

        $_settings = [];
        foreach ($this->getClientEntityPermission() as $group_id => $group_info) {

            $rebuild_children = [];
            foreach ($group_info['children'] as $entity => $info) {

                $actions = [];
                foreach ($info['action'] as $action_key => $action_title) {
                    $actions[] = [
                        'action'            => $action_key,
                        'title'             => $action_title,
                        'is_user_permitted' => (isset($current_permission[$group_id]) && isset($current_permission[$group_id][$entity]) && isset($current_permission[$group_id][$entity][$action_key])) ? $current_permission[$group_id][$entity][$action_key] : false ,
                    ];
                }

                $rebuild_children[$entity] = [
                    'title' => $info['title'],
                    'action_list' => $actions,
                ];

            }

            $_settings[$group_id] = $group_info;
            $_settings[$group_id]['children'] = $rebuild_children;
        }

        return $_settings;
    }


    public function saveDepartmentEntityPermission($department_id, array $new_permission) {
        $this->objStaffDepartmentPermissionModel->saveDepartmentEntityPermission(
            $department_id, $new_permission
        );
    }


    public function getDepartmentEntityPermission($department_id) {
        return static::getCache("getDepartmentEntityPermission-".$department_id, function () use ($department_id){
            return $this->objStaffDepartmentPermissionModel->getDepartmentEntityPermission($department_id);
        }) ;
    }

}
