<?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 NouvelArrivantVoter extends Voter
{
    public const MODULE_RH_ARRIVEE = 'MODULE_RH_ARRIVEE';
    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';
    public const EDIT_SECTION_RH = 'EDIT_SECTION_RH';
    public const EDIT_SECTION_ORGANISATION_TRAVAIL = 'EDIT_SECTION_ORGANISATION_TRAVAIL';
    public const EDIT_SECTION_FORMATION = 'EDIT_SECTION_FORMATION';
    public const EDIT_SECTION_FINANCES = 'EDIT_SECTION_FINANCES';
    public const EDIT_SECTION_LOGISTIQUE = 'EDIT_SECTION_LOGISTIQUE';
    public const EDIT_SECTION_INFORMATIQUE = 'EDIT_SECTION_INFORMATIQUE';
    public const EDIT_SECTION_DELEGATION = 'EDIT_SECTION_DELEGATION';

    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_ARRIVEE,
                                    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::EDIT_SECTION_RH,
                                    self::EDIT_SECTION_ORGANISATION_TRAVAIL,
                                    self::EDIT_SECTION_FORMATION,
                                    self::EDIT_SECTION_FINANCES,
                                    self::EDIT_SECTION_LOGISTIQUE,
                                    self::EDIT_SECTION_INFORMATIQUE,
                                    self::EDIT_SECTION_DELEGATION,
                                    self::DELETE_OWN,
                                    self::DELETE_GROUP,
                                    self::DELETE_ORGANIZATION,
                                    self::DELETE_ALL,

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

    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_ARRIVEE:
                // logic to determine if the user can EDIT
                // return true or false
                $isModuleActive = $this->params->get('active_modules')['module_arrivees'];
                return $isModuleActive;
                break;
            case self::CREATE:
                return $this->UserRolesManager->hasPermission('perm_nouvelArrivant_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 $this->UserRolesManager->hasPermission('perm_nouvelArrivant_edit_all');
                break;
            case self::EDIT_SECTION_RH:
                // logic to determine if the user can VIEW
                // return true or false
                return $this->UserRolesManager->hasPermission('perm_nouvelArrivant_edit_section_rh');
                break;
            case self::EDIT_SECTION_ORGANISATION_TRAVAIL:
                // logic to determine if the user can VIEW
                // return true or false
                return $this->UserRolesManager->hasPermission('perm_nouvelArrivant_edit_section_travail');
                break;
            case self::EDIT_SECTION_FORMATION:
                // logic to determine if the user can VIEW
                // return true or false
                return $this->UserRolesManager->hasPermission('perm_nouvelArrivant_edit_section_formation');
                break;
            case self::EDIT_SECTION_FINANCES:
                // logic to determine if the user can VIEW
                // return true or false
                return $this->UserRolesManager->hasPermission('perm_nouvelArrivant_edit_section_finances');
                break;
            case self::EDIT_SECTION_LOGISTIQUE:
                // logic to determine if the user can VIEW
                // return true or false
                return $this->UserRolesManager->hasPermission('perm_nouvelArrivant_edit_section_logistique');
                break;
            case self::EDIT_SECTION_INFORMATIQUE:
                // logic to determine if the user can VIEW
                // return true or false
                return $this->UserRolesManager->hasPermission('perm_nouvelArrivant_edit_section_informatique');
                break;
            case self::EDIT_SECTION_DELEGATION:
                // logic to determine if the user can VIEW
                // return true or false
                return $this->UserRolesManager->hasPermission('perm_nouvelArrivant_edit_section_delegation');
                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;
    }
}
