<?php

namespace Model;

use Entity\ImageEntity;
use Gateway\ImageGateway;
use Gateway\Gateway;

class ImageModel extends Model
{
    public function createImgModel(int $idImg, string $imgPath) : bool
    {
        return $this -> gateway -> createImgGateway($idImg, $imgPath);
    }

    public function createImgModelWithoutId (string $imgPath) : int
    {
        $idImg = $this->gateway ->getLastId() ;
        $this -> gateway -> createImgGateway($idImg, $imgPath, false);
        return $idImg;
    }

    public function getImgById(int $idImg) : ?ImageEntity
    {
        $res = $this -> gateway -> findImgById($idImg);

        if ($res)
        {
            return new ImageEntity(
                $res[0]['id_img'],
                $res[0]['imgpath']
            );
        }
        return null;
    }

    public function getAllImg() : array
    {
        $res = $this -> gateway -> findAllImg();

        $images = [];

        foreach ($res as $img)
        {
            $images[] = new ImageEntity(
                $img['id_img'],
                $img['imgpath']
            );
        }
        return $images;
    }


    public function deleteImgModel(int $idImg) : bool
    {
        return $this -> gateway -> deleteImgGateway($idImg);
    }

    public function updateImgModel(int $idImg, string $imgPath) : bool
    {
        return $this -> gateway -> updateImgGateway($idImg, $imgPath);
    }
}