<?php
    namespace Model;
    use Entity\User;
    use Entity\UserEntity;
    use Gateway\UserGateway;

    class UserModel
    {
        private UserGateway $gateway;

        public function __construct(UserGateway $gate){
            $this->gateway = $gate;
        }

        public function insertUser(string $username,string $email,string $passwd) : bool{
            /*global $rep,$image;*/
            return $this->gateway->insertUser( $username, $email, $passwd, false, 1);
        }

        public function getNumberOfUsers() : int
        {

            return $this->gateway->getNumberOfUsers()[0]['count'] ?? 0;
        }

        public function deleteUser(string $id) : bool{
            return $this->gateway->delete($id);
        }


        // public function getFavoriteUser(string $id) : array{
        //     $res[0] = array();
        //     $data = $this->gateway->getFavorite($id);
        //     foreach ($data as $favoris) {
        //         $res[0][] = new Quote();
        //     }
        // }

        public function getDataUser(int $id) : ?UserEntity {
            $res = $this->gateway->findDataUser($id);
            if ($res)
                return new UserEntity(
                    $res[0]['id_user'],
                    $res[0]['username'],
                    $res[0]['password'],
                    $res[0]['email'],
                    $res[0]['img'],
                    $res[0]['creation']
                );
            return null;
        }

        public function getUsername(string $username) : ?UserEntity
        {
            $res = $this->gateway->findUsername($username);
            if ($res)
                return new UserEntity(
                    $res[0]['id_user'],
                    $res[0]['username'],
                    $res[0]['password'],
                    $res[0]['email'],
                    $res[0]['imgpath'],
                    $res[0]['creation']
                );
            return null;
        }

        public function getEmail(string $email) : ?UserEntity
        {
            $res = $this->gateway->findEmail($email);
            if ($res)
                return new UserEntity(
                    $res[0]['id_user'],
                    $res[0]['username'],
                    $res[0]['password'],
                    $res[0]['email'],
                    $res[0]['img'],
                    $res[0]['creation']
                );
            return null;
        }

        public function setUsername(int $id, string $newUsername){
            $res = $this->gateway->updateUsername($id,$newUsername);
            $src[] = $res[0]['username'];

            return $src;
        }

        public function setEmail(int $id, string $newEmail){
            $res = $this->gateway->updateEmail($id,$newEmail);
            $src[] = $res[0]['email'];

            return $src;
        }

        public function setImg(int $id, int $newImg){
            $res = $this->gateway->updateImg($id,$newImg);
            $src[] = $res[0]['img'];

            return $src;
        }

        public function setPassWd(int $id, string $newPassWd){
            $res = $this->gateway->updatePasswd($id,$newPassWd);
            $src[] = $res[0]['pssword'];

            return $src;
        }

        public function getIdByUsername(string $username){
            $res = $this->gateway->getIdUser($username);
            return $res[0]['id_user'];
        }
    }

?>
