<?php

namespace Modules\Consultation\Rules;

use Entity\Model\AoEntet;
use Helpers\Outil\Roles;
use Psr\Http\Message\ServerRequestInterface;

class GestionConsultation
{
    /** @var ServerRequestInterface $request */
    protected ServerRequestInterface $request;

    /** @var \PDO $pdo */
    protected \PDO $pdo;

    /** @var string|mixed $currentUser */
    protected $currentUser;

    /** @var AoEntet $consultationEntity */
    protected AoEntet $consultationEntity;
        
    /**
     * __construct
     *
     * @param  ServerRequestInterface $request
     * @return void
     */
    public function __construct(ServerRequestInterface $request, \PDO $pdo)
    {
        $this->request              = $request;
        $this->pdo                  = $pdo;
        $this->currentUser          = $_SESSION['Session_Login'];
        $this->consultationEntity   = new AoEntet($pdo);
    }

    /**
     * @return bool
     */
    public function baseRule(): bool
    {
        if (Roles::droitsUtilisateur($this->pdo, 'HA_AO_ADM')) {
            return true;
        }

        $idAO           = $this->request->getAttribute('idAO');
        $consultation   = $this->consultationEntity->find($idAO);

        if (Roles::droitsUtilisateur($this->pdo, 'HA_AO_CMV') || $this->currentUser == $consultation['ACH']) {
            return true;
        }

        return false;
    }
    
    /**
     * init
     *
     * @return bool
     */
    public function init(): bool
    {
        return true;
    }
    
    /**
     * index
     *
     * @return bool
     */
    public function index(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    public function popupTransfere(): bool
    {
        if (!$this->baseRule()) {
            return false;
        }

        return (bool) Roles::droitsUtilisateur($this->pdo, 'HA_AO_TRA');
    }

    /**
     * @return bool
     */
    public function ajaxTransfere(): bool
    {
        return (bool) Roles::droitsUtilisateur($this->pdo, 'HA_AO_TRA');
    }

    /**
     * @return bool
     */
    public function ajaxValider(): bool
    {
        if (!$this->baseRule()) {
            return false;
        }

        $idAO           = $this->request->getAttribute('idAO');
        $consultation   = $this->consultationEntity->find($idAO);

        if ($consultation['DTE_VALID_AO'] !== '00000000') {
            return false;
        }

        return true;
    }

    /**
     * @return bool
     */
    public function confirmationValidate(): bool
    {
        return $this->ajaxValider();
    }

    /**
     * @return bool
     */
    public function ajaxDevalider(): bool
    {
        if (!$this->baseRule()) {
            return false;
        }
        
        return true;
    }

    /**
     * @return bool
     */
    public function ajaxDevaliderSelectFrs(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    public function ajaxImpression(): bool
    {
        if (!$this->baseRule()) {
            return false;
        }

        return true;
    }

    /**
     * @return bool
     */
    public function ajaxImpressionComparatif(): bool
    {
        if (!$this->baseRule()) {
            return false;
        }

        return true;
    }

    /**
     * @return bool
     */
    public function envoyer(): bool
    {
        if (!$this->baseRule()) {
            return false;
        }

        return true;
    }

    /**
     * @return bool
     */
    public function ajaxEnvoie(): bool
    {
        if (!$this->baseRule()) {
            return false;
        }

        return true;
    }

    /**
     * @return bool
     */
    public function creerPanier(): bool
    {
        $idAO           = $this->request->getAttribute('idAO');
        $consultation   = $this->consultationEntity->find($idAO);

        if (Roles::droitsUtilisateur($this->pdo, 'HA_DA_CRE') ||Roles::GetALL()) {
            $P2PActive  = 1;
        } else {
            $P2PActive  = 0;
        }

        if ($consultation['VALCHXFOU'] == '00000000' &&
            !$P2PActive &&
            ($consultation['RFQ_I'] != 'Q' || $consultation['RFQ_I'] != '')
        ) {
            return false;
        }

        return true;
    }

    /**
     * @return bool
     */
    public function ajaxCreerPanier(): bool
    {
        return $this->creerPanier();
    }

    /**
     * @return bool
     */
    public function popupImportExcel(): bool
    {
        return ($this->currentUser === 'MSA');
    }

    /**
     * @return bool
     */
    public function popupConfirmImport(): bool
    {
        return $this->popupImportExcel();
    }


    /**
     * @return bool
     */
    public function ajaxDetailEntete(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    public function popupListeFournisseur(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    public function popupExport(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    public function export(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    // @phpcs:ignore
    public function popupCommunicationFournisseur(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    // @phpcs:ignore
    public function envoieEmailCommunicationFournisseur(): bool
    {
        return true;
    }
}
