<?php
    namespace Model;
    use Entity\Quote;
    use Gateway\QuoteGateway;
    use Gateway\Gateway;
    use Enum\TypeSourceEnum;

    class QuoteModel extends Model
    {

        public function searchId(int $id): Quote{
            $res = $this->gateway->searchId($id);
            if( count($res) == 0)
                return new Quote(-1,"NULL","NULL","NULL","NULL","NULL",0,"Default");
            else 
                return new Quote($res[0]["id_quote"],$res[0]["content"],$res[0]["caracter"],$res[0]["imgpath"],$res[0]["title"],$res[0]["dates"],$res[0]["likes"],$res[0]["langue"],TypeSourceEnum::Movie);
        }

        public function getSuggest(int $numpage, string $language):array{
            $res = $this->gateway->getSuggestions($numpage,$language);
            $tabQ=[];

            foreach($res as $q ){
                $q["content"] = (strlen($q["content"]) > 153) ? substr($q["content"],0,150).'...' : $q["content"];
                $tabQ[]= new Quote($q["id_quote"],$q["content"],$q["caracter"],$q["imgpath"],$q["title"],$q["dates"],$q["likes"],$q["langue"],TypeSourceEnum::Movie) ;
            }
            return $tabQ;
        }

        public function getQuoteOfTheDay(string $language):Quote{
            $res = $this->gateway->getQuoteOfTheDay($language);
            if( count($res) == 0)
                return new Quote(-1,"NULL","NULL","NULL","NULL","NULL",0,"Default",TypeSourceEnum::Movie);
            else 
                $res["content"] = (strlen($res["content"]) > 153) ? substr($res["content"],0,150).'...' : $res["content"];
                return new Quote($res["id_quote"],$res["content"],$res["caracter"],$res["imgpath"],$res["title"],$res["dates"],$res["likes"],$res["langue"],TypeSourceEnum::Movie) ;
        }

        public function getFavorites(string $userId): array {
            $res = $this->gateway->getFavorites($userId);
            $tabQ=[];

            foreach($res as $q ){
                $q["content"] = (strlen($q["content"]) > 153) ? substr($q["content"],0,150).'...' : $q["content"];
                $tabQ[]= new Quote($q["id_quote"],$q["content"],$q["caracter"],$q["imgpath"],$q["title"],$q["dates"],$q["likes"],$q["langue"],TypeSourceEnum::Movie) ;
            }
            return $tabQ;
        }

        public function addQuote(string $content, string $lang, int $idChar, int $idSrc, int $idUsr){
            $this -> gateway -> addQuote($content,$lang,$idChar,$idSrc,$idUsr);
        }
    }

?>
