<?php

namespace App\modele;

use App\gateway\Connection;
use App\metier\Image;
use App\gateway\ImageGateway;
class ImageModele
{
    private ImageGateway $gw;
    public function __construct()
    {
        $this->gw = new ImageGateway(new Connection("mysql:host=localhost;dbname=dbAlica","test","test"));
    }

    public function publierImage() : Image
    {
        $img = new Image($this->gw->getNewId(),
            $_FILES["image"]["name"],
            $_FILES["image"]["size"],
            $_FILES["image"]["type"],
            file_get_contents($_FILES["image"]["tmp_name"]));

        $this->insertImage($img);

       return $img;
    }

    public function insertImage(Image $img)
    {
        $this->gw->insertImage($img);
    }

    public function obtenirParId(int $id)
    {
        $this->gw->obtenirParId($id);
        $res = $this->gw->getResults();
        return new Image($this->gw->getNewId(),$res[0]['nom'], $res[0]['taille'], $res[0]['type'], $res[0]['blob']);
    }

    public function obtenirToutesImages() : array
    {
        $this->gw->obtenirToutesImages();
        $res = $this->gw->getResults();
        foreach ($res as $r) {
            $array[] = new Image($this->gw->getNewId(),$r['nom'], $r['taille'], $r['type'], $r['blob']);
        }
        return $array;
    }


}