<?php

namespace App\Service;

use Exception;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class PictureService
{
    public function __construct(private ParameterBagInterface $params)
    {

    }

    // Redimensionner en carré 250x250
    public function square(UploadedFile $picture, ?string $folder = '', ?string $newName): string
    {
        // Dimensions de la vignette
        $width = 250;
        $height = $width;
        
        // On donne un nouveau nom à l'image
        $file = $newName . '.webp';

        // On récupère les informations de l'image
        $pictureInfos = getimagesize($picture);

        if($pictureInfos === false){
            throw new Exception('Format d\'image incorrect');
        }

        // On vérifie le type mime
        switch($pictureInfos['mime']){
            case 'image/png':
                $sourcePicture = imagecreatefrompng($picture);
                break;
            case 'image/jpeg':
                $sourcePicture = imagecreatefromjpeg($picture);
                break;
            case 'image/webp':
                $sourcePicture = imagecreatefromwebp($picture);
                break;
            default:
                throw new Exception('Format d\'image incorrect');
        }

        // On recadre l'image
        $imageWidth = $pictureInfos[0];
        $imageHeight = $pictureInfos[1];

        switch($imageWidth <=> $imageHeight){
            case -1: // portrait
                $squareSize = $imageWidth;
                $srcX = 0;
                $srcY = ($imageHeight - $imageWidth) / 2;
                break;

            case 0: // carré
                $squareSize = $imageWidth;
                $srcX = 0;
                $srcY = 0;
                break;

            case 1: // paysage
                $squareSize = $imageHeight;
                $srcX = ($imageWidth - $imageHeight) / 2;
                $srcY = 0;
                break;
        }

        // On crée une nouvelle image vierge
        $resizedPicture = imagecreatetruecolor($width, $width);

        // On génère le contenu de l'image
        imagecopyresampled($resizedPicture, $sourcePicture, 0, 0, $srcX, $srcY, $width, $width, $squareSize, $squareSize);

        // On crée le chemin de stockage
        $path = $this->params->get('upload_images_directory') . $folder;

        // On stocke l'image réduite
        imagewebp($resizedPicture, $path . $file);

        return $file;
    }

    // Redientionner en bannière 600x200
    public function banner(UploadedFile $picture, ?string $folder = '', ?string $newName): string
    {
         // Dimensions de la vignette
        $width = 600;
        $height = 200;
        
        // On donne un nouveau nom à l'image
        $file = $newName . '.webp';

        // On récupère les informations de l'image
        $pictureInfos = getimagesize($picture);

        if($pictureInfos === false){
            throw new Exception('Format d\'image incorrect');
        }

        // On vérifie le type mime
        switch($pictureInfos['mime']){
            case 'image/png':
                $sourcePicture = imagecreatefrompng($picture);
                break;
            case 'image/jpeg':
                $sourcePicture = imagecreatefromjpeg($picture);
                break;
            case 'image/webp':
                $sourcePicture = imagecreatefromwebp($picture);
                break;
            default:
                throw new Exception('Format d\'image incorrect');
        }

        // On recadre l'image
        $imageWidth = $pictureInfos[0];
        $imageHeight = $pictureInfos[1];

        $newWidth = $imageWidth;
        $newHeight = ($newWidth / $width) * $height;
        $srcY = ($imageHeight - $newHeight) / 2;

        // On crée une nouvelle image vierge
        $resizedPicture = imagecreatetruecolor($width, $height);

        // On génère le contenu de l'image
        imagecopyresampled($resizedPicture, $sourcePicture, 0, 0, 0, $srcY, $width, $height, $newWidth, $newHeight);

        // On crée le chemin de stockage
        $path = $this->params->get('upload_images_directory') . $folder;

        // On stocke l'image réduite
        imagewebp($resizedPicture, $path . $file);


        return $file;
    }
}

?>