<?php
class UserModel
{

    private EnigmeGateway $enigme_gateway;
    private PartieGateway $partie_gateway;
    private UtilisateurGateway $utilisateur_gateway;

    function __construct()
    {
        try {
            global $error, $view, $rep;
            $this->enigme_gateway = new EnigmeGateway();
            $this->partie_gateway = new PartieGateway();
            $this->utilisateur_gateway = new UtilisateurGateway();
        } catch (Exception $e) {
            $error = $e->getMessage();
            require($rep . $view['erreur']);
        }
    }

    public function goToPresentation()
    {
        global $rep, $vues, $error;
        try {
            require($rep . $vues['presentation']);
        } catch (Exception $e) {
            $error = "Erreur Inconnue";
            require($rep . $vues['erreur']);
        }
    }

    public function goToHome()
    {
        global $rep, $vues, $error;
        try {
            require($rep . $vues['main']);
        } catch (Exception $e) {
            $error = "404";
            require($rep . $vues['erreur']);
        }
    }
    public function goToEnigme()
    {
        global $rep, $vues, $error;
        try {
            require($rep . $vues['enigme']);
        } catch (Exception $e) {
            $error = "404";
            require($rep . $vues['erreur']);
        }
    }

    public function goToChouette()
    {
        global $rep, $vues, $error;
        try {
            require($rep . $vues['chouette']);
        } catch (Exception $e) {
            $error = "404";
            require($rep . $vues['erreur']);
        }
    }
    public function goToCesarEncrypt()
    {
        global $rep, $vues, $error;
        try {
            require($rep . $vues['cesarEncrypt']);
        } catch (Exception $e) {
            $error = "404";
            require($rep . $vues['erreur']);
        }
    }
    public function goToCesarDecrypt()
    {
        global $rep, $vues, $error;
        try {
            require($rep . $vues['cesarDecrypt']);
        } catch (Exception $e) {
            $error = "404";
            require($rep . $vues['erreur']);
        }
    }
    public function goToChuckNorris()
    {
        global $rep, $vues, $error;
        try {
            require($rep . $vues['chuckNorris']);
        } catch (Exception $e) {
            $error = "404";
            require($rep . $vues['erreur']);
        }
    }
    public function goToHanoi()
    {
        global $rep, $vues, $error;
        try {
            require($rep . $vues['hanoi']);
        } catch (Exception $e) {
            $error = "404";
            require($rep . $vues['erreur']);
        }
    }
 public function goToTrianglePascal()
    {
        global $rep, $vues, $error;
        try {
            require($rep . $vues['trianglePascal']);
        } catch (Exception $e) {
            $error = "404";
            require($rep . $vues['erreur']);
        }
    }

    public function goToTest()
    {
        global $rep, $vues, $error;
        try {
            require($rep . $vues['test']);
        } catch (Exception $e) {
            $error = "404";
            require($rep . $vues['erreur']);
        }
    }

    public function goToNext()
    {
        global $rep, $vues, $error;
        try {
            require($rep . $vues['next'].$_REQUEST['num'].".html");
        } catch (Exception $e) {
            $error = "Erreur Inconnue";
            require($rep . $vues['erreur']);
        }
    }

    public function goToQueue()
    {
        global $rep, $vues, $error;
        try {
            //require($rep . $vues['Queue']);
            $this->addToQueue();
        } catch (Exception $e) {
            $error = $e->getMessage();
            require($rep . $vues['erreur']);
        }
    }
    public function addToQueue(){
        echo '1';
        if($this->utilisateur_gateway->isAlreadyInqueue($_SESSION['utilisateur']))
        {
            return;
        }
        echo '2';
        if (!$this->partie_gateway->partieInQueueExists()) {
            echo '3';
            $tabEnigme = $this->enigme_gateway->findMultiEnigma();
            $idNewPartie = $this->partie_gateway->findPartieMaxId();
            $partie=$this->partie_gateway->creerPartieMulti($idNewPartie,$tabEnigme);
        }
        else{
            echo '4';
            $idPartieInQueue = $this->partie_gateway->findPartieInQueue();
            echo '5';
            $tabEnigme = $this->enigme_gateway->findEnigmaFromPartie($idPartieInQueue);
            echo '6';
            $partie = $this->partie_gateway->rejoindrePartieMulti($idPartieInQueue, $tabEnigme);
            echo '7';
        }
        echo '8';
        $this->utilisateur_gateway->addToQueue($_SESSION['utilisateur'],$partie);
        $userGroup=$this->utilisateur_gateway->findUsersInQueue();
        //trigger_event('add_to_queue',$userGroup);
    }

    public function AddUserToQueueEvent(){
        $userGroup=$this->utilisateur_gateway->findUsersInQueue();
        if($userGroup.count()>=4)
        {
            $this->utilisateur_gateway->launchGame();
        }
    }

    public function logout()
    {
        $_SESSION['role'] = 'visitor';
        header('Location: index.php');
    }
}