<?php

namespace Hura8\Components\Staff\AdminController;

use Hura8\Components\Staff\Model\StaffPermissionModel;
use Hura8\Components\Staff\PublicController\StaffLoginController;


class StaffPermissionController extends ClientPermissionController
{

    protected  $objStaffPermissionModel;


    public function __construct() {
        $this->objStaffPermissionModel = new StaffPermissionModel();
    }

    /**
     * @param $entity_group
     * @param $entity
     * @param string $action valid values in PermissionType::
     * @return bool
     */
    public function checkEntityActionPermission($is_super_user, $entity_group, $entity, $action ) : bool {

        // super is allowed by default
        if($is_super_user) {
            return true;
        }

        $current_permission = $this->getCurrentUserEntityPermission();

        if (
            isset($current_permission[$entity_group]) &&
            isset($current_permission[$entity_group][$entity]) &&
            isset($current_permission[$entity_group][$entity][$action])
        ) {
            return $current_permission[$entity_group][$entity][$action] ;
        }

        return false;
    }


    public function getUserProductMenuList() {

        return self::getCache("getUserProductMenuList", function (){
            $menu_list  = include CONFIG_DIR. '/client/admin/product_menu.php';

            $final_list = [];
            foreach (array_filter($menu_list, function ($item){ return $item['enabled'];}) as $key => $value) {

                // check if the current staff can see
                // todo
                // ...

                $final_list[$key] = $value;
            }

            return $final_list;
        });

    }


    public function saveUserMenuPermission($admin_id, array $new_permission) {
        $this->objStaffPermissionModel->saveUserMenuPermission($admin_id, $new_permission);
    }


    public function saveUserEntityPermission($admin_id, array $new_permission) {
        $this->objStaffPermissionModel->saveUserEntityPermission($admin_id, $new_permission);
    }

    /*
     * get menu for currently logged-in user
     */
    public function getUserMenu($is_super_user) {

        // super can see all
        if($is_super_user) {
            return $this->getClientMenu();
        }

        $current_permission = $this->getCurrentUserMenuPermission();

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

            $rebuild_menu = [];
            foreach ($group_info['menu'] as $index => $menu) {
                if(!in_array($menu['id'], $current_permission)) continue;

                $rebuild_menu[$index] = $menu;
            }

            if(sizeof($rebuild_menu)) {
                $user_menu_settings[$group_id] = $group_info;
                $user_menu_settings[$group_id]['menu'] = $rebuild_menu;
            }
        }

        return $user_menu_settings;
    }


    public function getCurrentUserMenuPermission() {
        $current_staff_id = StaffLoginController::getLoggedInStaffId();
        return $this->objStaffPermissionModel->getUserMenuPermission($current_staff_id);
    }


    public function getCurrentUserEntityPermission() {
        $current_staff_id = StaffLoginController::getLoggedInStaffId();
        return $this->objStaffPermissionModel->getUserEntityPermission($current_staff_id);
    }


    public function getUserMenuPermissionSetting($admin_id) {

        $current_permission = $this->objStaffPermissionModel->getUserMenuPermission($admin_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 getUserEntityPermissionSetting($admin_id) {

        $current_permission = $this->objStaffPermissionModel->getUserEntityPermission($admin_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;
    }

}
