<?php

class UserController
{
    private Connection $con;

    function __construct() {
        try{
            echo "UserController";
            global $dsn, $rep, $vues, $error;
            $this->con=new Connection($dsn);
            echo "USconection";
            $action=$_REQUEST['action'];
            switch($action) {
                case NULL:
                    $this->goToHome();
                    break;
                case "signUp":
                    $this->signUp();
                    break;
                case "login":
                    $this->login();
                    break;
                case "goToPresentation":
                    $this->goToPresentation();
                    break;
                case "goToHome":
                    $this->goToHome();
                    break;
                case "goToLogin":
                    $this->goToLogin();
                    break;
                case "goToSignUp":
                    $this->goToSignUp();
                    break;
                case "goToEnigme":
                    $this->goToEnigme();
                    break;
            }
        } catch (PDOException $e)
        {
            $error = $e->getMessage();
            require ($rep.$vues['erreur']);
        }catch (Exception $e)
        {
            $error = $e->getMessage();
            require ($rep.$vues['erreur']);
        }
    }

    private function signUp() {
        global $rep, $vues, $sel, $error;
        try {
            $gateway = new UtilisateurGateway($this->con);
            $validation = new Validation();
            if (! $validation->ValidateEmail($_REQUEST['email'])) {
                $error = "Email invalides.";
                throw (new Exception("Email non valide"));
            }
            if(! $validation->ValidateUsername($_REQUEST['username'])){
                $error = "Nom d'utilisateur invalides. Il ne doit pas contenir de caractère spéciaux.";
                throw(new Exception("Pseudo non valide"));
            }
            if(! $validation->ValidatePassword($_REQUEST['password'])){
                $error = "Mots de passe invalides. Il ne doit pas dépasser 100 caractères.";
                throw(new Exception("Mot de passe non valide"));
            }
            $j = $gateway->getUtilisateurByEmail($_REQUEST['email']);
            if ($j != null) {
                $error = "Email déjà utilisé.";
                throw (new Exception("Email déjà utilisé"));
            }
            $password = password_hash($_REQUEST['password'], PASSWORD_DEFAULT);
            $estAdmin = $gateway->getEstAdminByEmail($_REQUEST['email']);
            $Utilisateur = new Utilisateur($_REQUEST['email'], $_REQUEST['username'], $password, $estAdmin);
            $gateway->insert($Utilisateur);
            $_SESSION['connected'] = 'true';
            $_SESSION['role'] = 'utilisateur';
            require ($rep.$vues['main']);
        }catch (PDOException $e)
        {
            $error = "Erreur de connexion à la base de données.";
            require ($rep.$vues['erreur']);
        }
        catch (Exception $e){
            require($rep.$vues['erreur']);
        }
    }
    private function login(){
        global $rep, $vues, $sel, $error;
        try {
            $gateway = new UtilisateurGateway($this->con);
            $Utilisateur = $gateway->getUtilisateurByEmail($_REQUEST['email']);
            if ($Utilisateur->getEmail() == null){
                $error = "Utilisateur non trouvé.";
                throw new Exception("Utilisateur introuvable");
            }
            $mdp = $gateway->getMdpByEmail($_REQUEST['email']);
            if (password_verify($mdp, $_REQUEST['password'])){
                $error = "Mot de passe incorrect.";
                throw new Exception("Mot de passe invalide");
            }
            $estAdmin =$gateway->getEstAdminByEmail($_REQUEST['email']);
            if ($estAdmin == true) {
                $_SESSION['role'] = "admin";
            }
            else{
                $_SESSION['role'] = "utilisateur";
            }

            $_SESSION['connected'] = 'true';    
            require ($rep.$vues['main']);
        }catch (Exception $e){
            require($rep.$vues['erreur']);
        }
    }

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

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

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

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

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