<?php
namespace App\Components\Www;

use Symfony\Component\Form\FormView;
use App\Repository\SponsorRepository;
use App\Entity\Interface\ICachableImage;
use Psr\Log\LoggerInterface;
use Liip\ImagineBundle\Service\FilterService;
use Liip\ImagineBundle\Config\StackCollection;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Liip\ImagineBundle\Config\FilterFactoryCollection;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent(name: 'ImageOrVideo', template: "www/components/image-or-video.html.twig")]
class ImageOrVideo
{
    public $entity;
    public string $filter = "";
    public string $alt = "";
    public string $videoField = "";
    public string $imageFileNameField = "";
    public string $imageFileField = "";
    public string $class = "";
    public string $href = "";

    const COEFF_MAX_SIZE = 5;
    
    public function __construct(private LoggerInterface $logger)
    {
    }

    public function getCachableImage() : ICachableImage {
        return $this->entity;
    }

    private string $_width_img = "";
    private string $_height_img = "";

    private int $_width_video = 0;
    private int $_height_video = 0;

    private string $_media_type = "none";

    private string $_image = "";
    private string $_imagefile = "";
    private string $_video = "";
    private string $_video_mode = "fixed";

    private $prepared = false;
    private function __prepare() {
        if ($this->prepared) return;
        
        try {
            // REFACTO ImageOrVideo / gérer la dimensions par la config

            switch ($this->filter) {
                case 'admin_event_list_filter':
                    $this->_width_img = "" . 420/2 ."px";
                    $this->_height_img = "" . 314/2 ."px";
                    $this->_width_video = 420/2;
                    $this->_height_video = 314/2;
                    $this->_video_mode = "fixed";
                    break;
                case 'calendar_event_filter_jpeg':
                    $this->_width_img = "" . 554/2 ."px";
                    $this->_height_img = "" . 384/2 ."px";
                    $this->_width_video = 554/2;
                    $this->_height_video = 384/2;
                    $this->_video_mode = "fixed";
                    break;
                case 'home_event_filter_jpeg':
                case 'home_event_filter_webp':
                case 'galery_media_filter_jpeg':
                case 'galery_media_filter_webp':
                    $this->_width_img = "100%";
                    $this->_height_img = "auto";

                    $this->_width_video = 1300/2;
                    $this->_height_video = 900/2;
                    $this->_video_mode = "100%";
                    break;
                default:
                    throw new \Exception("filter " . $this->filter . " non géré");
                    // $this->_width = 420/2;
                    // $this->_height = 314/2;
            }

            $propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
                ->enableExceptionOnInvalidIndex()
                ->getPropertyAccessor();

            $video = $propertyAccessor->getValue($this->entity, $this->videoField);

            // dump($video);

            if ($video && $video != "") {

                // dd($this->_video);
                // $this->logger->critical("video : " . $video);

                $this->_image = "";
                $this->_imagefile = "";
                $this->_video = $video;

                $image = $propertyAccessor->getValue($this->entity, $this->imageFileNameField);

                if ($image && $image != "") {
                    $this->_media_type = "video+image";
                    $this->_imagefile = $this->imageFileField;
                } else {
                    $this->_media_type = "video";
                }

            } else {
                // $this->logger->critical("image : " . $this->imageFileNameField);

                $image = $propertyAccessor->getValue($this->entity, $this->imageFileNameField);
                // dd($image);

                $this->_image = $image;
                $this->_media_type = "image";

                if ($image && $image != "") {
                    $this->_video = "";
                    // $this->_image = $this->imageFileField;
                    $this->_imagefile = $this->imageFileField;
                }

                // On choisit de ne pas mettre de lien quand il y a une image seulement
                // demande Arnaud - 2023.07.13
                $this->href = ""; 
            }
        } catch (\Throwable $th) {
            $this->logger->critical($th);
        }

        $this->prepared = true;
    }

    public function getImageWidth() : string { $this->__prepare(); return $this->_width_img; }
    public function getImageHeight() : string { $this->__prepare(); return $this->_height_img; }
    public function getImage() : string { $this->__prepare(); return $this->_image; }
    public function getImageFile() : string { $this->__prepare(); return $this->_imagefile; }

    public function getVideoWidth() : int { $this->__prepare(); return $this->_width_video; }
    public function getVideoMode() : string { $this->__prepare(); return $this->_video_mode; }
    public function getVideoHeight() : int { $this->__prepare(); return $this->_height_video; }
    public function getVideo() : string { $this->__prepare(); return $this->_video; }
    public function getMediaType() : string { $this->__prepare(); return $this->_media_type; }   
}

// https://img.youtube.com/vi/9_OCD1dNtOg/maxresdefault.jpg
// https://img.youtube.com/vi/9_OCD1dNtOg/hqdefault.jpg
