<?php

namespace Model;

use Entity\ImageEntity;
use Gateway\ImageGateway;

class ImageModel
{
    private ImageGateway $gw;

    public function __construct(ImageGateway $gw)
    {
        $this -> gw = $gw;
    }

    public function createImgModel(int $idImg, string $imgPath, bool $isImgProfile) : bool
    {
        return $this -> gw -> createImgGateway($idImg, $imgPath, $isImgProfile);
    }

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

        if ($res)
        {
            return new ImageEntity(
                $res[0]['id_image'],
                $res[0]['img_path'],
                $res[0]['is_img_prfl']
            );
        }
        return null;
    }

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

        $images = [];

        foreach ($res as $img)
        {
            $images[] = new ImageEntity(
                $img['id_image'],
                $img['img_path'],
                $img['is_img_prfl']
            );
        }
        return $images;
    }

    public function getAllImgProfile() : array
    {
        $res = $this -> gw -> findAllImgProfile();

        $images = [];

        foreach ($res as $img)
        {
            $images[] = new ImageEntity(
                $img['id_image'],
                $img['img_path'],
                $img['is_img_prfl']
            );
        }
        return $images;
    }

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

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