<?php
include_once "../Factory/PartieFactory.php";
class UtilisateurGateway
{
    private Connection $con;

    /**
     * @param Connection $con
     */
    public function __construct(Connection $con){
        $this->con = $con;
    }

    /**
     * @param Connection $con
     */
    public function setCon(Connection $con): void
    {
        $this->con = $con;
    }

    public function insert(Utilisateur $utilisateur) : void{
        $query = "INSERT INTO Utilisateur VALUE (:email,:pseudo,:mdp,:estAdmin)";
        $this->con->executeQuery($query,	array(
            ':email'	=>	array($utilisateur->getEmail(),PDO::PARAM_STR),
            ':pseudo'	=>	array($utilisateur->getPseudo(),PDO::PARAM_STR),
            ':mdp'	=>	array($utilisateur->getMdp(),PDO::PARAM_STR),
            ':estAdmin'	=>	array($utilisateur->getEstAdmin(),PDO::PARAM_BOOL)));
    }

    public function delete(string $email) : void{
        $query = "DELETE FROM utilisateur WHERE email=:email";
        $this->con->executeQuery($query,	array(
            ':email'	=>	array($email ,PDO::PARAM_STR)
        ));
    }

    public function getUtilisateurByEmail(string $email) : Utilisateur{
        global $error;
        $query = "SELECT * FROM Utilisateur WHERE email=:email";
        $this->con->executeQuery($query,	array(
            ':email'	=>	array($email,PDO::PARAM_STR)
        ));
        $results=$this->con->getResults();
        foreach ($results as $row) {
            $email=$row['email'];
            $pseudo=$row['pseudo'];
            $mdp=$row['mdp'];
            $estAdmin=$row['estAdmin'];
        }
        if ($results == null){
            return new Utilisateur("null", "null", "null", false);
        }
        return new Utilisateur($email, $pseudo, $mdp, $estAdmin);
    }

    public function getMdpByEmail(string $email) : string{
        global $error;
        $query = "SELECT mdp FROM Utilisateur WHERE email=:email";
        $this->con->executeQuery($query,	array(
            ':email'	=>	array($email,PDO::PARAM_STR)
        ));
        $results=$this->con->getResults();
        foreach ($results as $row) {
            $mdp=$row['mdp'];
        }
        if ($results == null){
            $error = "Mot de passe non trouvé.";
            throw new Exception("Mots de passe Incorrect");
        }
        return $mdp;
    }

    public function getEstAdminByEmail(string $email) : bool{
        $query = "SELECT estAdmin FROM Utilisateur WHERE email=:email";
        $this->con->executeQuery($query,	array(
            ':email'	=>	array($email,PDO::PARAM_STR)
        ));
        $results=$this->con->getResults();
        foreach ($results as $row) {
            $estAdmin=$row['estAdmin'];
        }
        return $estAdmin;
    }

    public function showAll() : void{
        $query = "SELECT * FROM Utilisateur";
        $this->con->executeQuery($query);
        $results=$this->con->getResults();
        foreach ($results as $row) {
            echo $row['email'] . '</br>';
            echo $row['pseudo'] . '</br>';
            echo $row['mdp'] . '</br>';
            echo $row['estAdmin'] . '</br>';
        }

    }
}