<?php

namespace App\Security\Voter;

use App\Service\UserRolesManager;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class RhDepartVoter extends Voter
{
    public const MODULE_RH_DEPART = 'MODULE_RH_DEPART';
    public const CREATE = 'CREATE';
    public const VIEW_OWN = 'VIEW_OWN';
    public const VIEW_GROUP = 'VIEW_GROUP';
    public const VIEW_ORGANIZATION = 'VIEW_ORGANIZATION';
    public const VIEW_ALL = 'VIEW_ALL';
    public const EDIT_OWN = 'EDIT_OWN';
    public const EDIT_GROUP = 'EDIT_GROUP';
    public const EDIT_ORGANIZATION = 'EDIT_ORGANIZATION';
    public const EDIT_ALL = 'EDIT_ALL';
    public const DELETE_OWN = 'DELETE_OWN' ;
    public const DELETE_GROUP = 'DELETE_GROUP' ;
    public const DELETE_ORGANIZATION = 'DELETE_ORGANIZATION';
    public const DELETE_ALL = 'DELETE_ALL';

    private $UserRolesManager;
    private $params;

    public function __construct(UserRolesManager $UserRolesManager, ContainerBagInterface $params )
    {
        $this->UserRolesManager = $UserRolesManager;
        $this->params = $params;
    }
    
    protected function supports(string $attribute, $subject): bool
    {
        // replace with your own logic
        // https://symfony.com/doc/current/security/voters.html
        return in_array($attribute, [
                                    self::MODULE_RH_DEPART,
                                    self::CREATE,
                                    self::VIEW_OWN,
                                    self::VIEW_GROUP,
                                    self::VIEW_ORGANIZATION,
                                    self::VIEW_ALL,
                                    self::EDIT_OWN,
                                    self::EDIT_GROUP,
                                    self::EDIT_ORGANIZATION,
                                    self::EDIT_ALL,
                                    self::DELETE_OWN,
                                    self::DELETE_GROUP,
                                    self::DELETE_ORGANIZATION,
                                    self::DELETE_ALL,

                ]) && $subject instanceof \App\Entity\RhDepart;
    }

    protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
    {
        $user = $token->getUser();
        // if the user is anonymous, do not grant access
        if (!$user instanceof UserInterface) {
            return false;
        }

        // ... (check conditions and return true to grant permission) ...
        switch ($attribute) {
            case self::MODULE_RH_DEPART:
                // logic to determine if the user can EDIT
                // return true or false
                $isModuleActive = $this->params->get('active_modules')['module_departs'];
                return $isModuleActive;
                break;
            case self::CREATE:
                return $this->UserRolesManager->hasPermission('perm_rhDepart_create');
                break;
            case self::VIEW_OWN:
                // logic to determine if the user can VIEW
                // return true or false
                break;
            case self::VIEW_GROUP:
                // logic to determine if the user can VIEW
                // return true or false
                break;
            case self::VIEW_ORGANIZATION:
                // logic to determine if the user can VIEW
                // return true or false
                break;
            case self::VIEW_ALL:
                // logic to determine if the user can VIEW
                // return true or false
                break;
            case self::EDIT_OWN:
                // logic to determine if the user can EDIT
                // return true or false
                break;
            case self::EDIT_GROUP:
                // logic to determine if the user can EDIT
                // return true or false
                break;
            case self::EDIT_ORGANIZATION:
                // logic to determine if the user can EDIT
                // return true or false
                break;
            case self::EDIT_ALL:
                // logic to determine if the user can EDIT
                // return true or false
                break;
            case self::DELETE_OWN:
                // logic to determine if the user can VIEW
                // return true or false
                break;
            case self::DELETE_GROUP:
                // logic to determine if the user can VIEW
                // return true or false
                break;
            case self::DELETE_ORGANIZATION:
                // logic to determine if the user can VIEW
                // return true or false
                break;
            case self::DELETE_ALL:
                // logic to determine if the user can VIEW
                // return true or false
                break;
        }

        return false;
    }
}
