<?php

namespace Lc\SovBundle\Component;

use Doctrine\ORM\EntityManagerInterface;
use Lc\SovBundle\Event\EntityComponentEvent;
use Lc\SovBundle\Model\File\FileInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class EntityComponent
{
    protected EntityManagerInterface $entityManager;
    protected ParameterBagInterface $parameterBag;
    protected EventDispatcherInterface $eventDispatcher;

    public function __construct(
            EntityManagerInterface $entityManager,
            ParameterBagInterface $parameterBag,
            EventDispatcherInterface $eventDispatcher
    ) {
        $this->entityManager = $entityManager;
        $this->parameterBag = $parameterBag;
        $this->eventDispatcher = $eventDispatcher;
    }

    public function duplicateEntity($entity)
    {
        $newEntity = clone $entity;
        $classMetadata = $this->entityManager->getClassMetadata(get_class($newEntity));

        foreach ($classMetadata->getAssociationMappings() as $associationMapping){
            if(in_array(FileInterface::class, class_implements($associationMapping['targetEntity']))){
                $methodGet = 'get'.ucfirst($associationMapping['fieldName']);
                $methodSet = 'set'.ucfirst($associationMapping['fieldName']);
                if(method_exists($newEntity, $methodGet) && method_exists($newEntity, $methodSet)){
                    if(!is_null($newEntity->$methodGet())){
                        $newFile = clone $newEntity->$methodGet();
                        $newEntity->$methodSet($newFile);
                    }
                }
            }

        }

        //TODO dispatch event duplicate
      /*  if ($newEntity instanceof ProductFamilyInterface) {
            // @TODO : à adapter
            //$newEntity = $this->productFamilyUtils->processBeforePersistProductFamily($newEntity, false, true);
        }
*/
        $this->entityManager->create($newEntity);

        $this->eventDispatcher->dispatch(new EntityComponentEvent($newEntity), EntityComponentEvent::DUPLICATE_EVENT);

        return $newEntity;
    }

    public function duplicateEntityToOtherHub($entity, $hub)
    {
        $newEntity = $this->duplicateEntity($entity);

        if ($hub) {
            $newEntity->setMerchant($hub);
        }
        $this->entityManager->persist($newEntity);
        $this->entityManager->flush();

        return $newEntity;
    }

    public function duplicateImage($entity, $folder = false)
    {
        $basePath = $this->parameterBag->get('kernel.project_dir') . '/public/uploads/images/';

        if ($entity->getImage() && file_exists($basePath . $entity->getImage())) {
            $extension = strtolower(pathinfo($basePath . $entity->getImage(), PATHINFO_EXTENSION));

            if ($extension == "jpg" || $extension == "png" || $extension == "gif") {
                $newImage = md5(uniqid()) . '.' . $extension;
                if ($folder) {
                    $newImage = $folder . '/' . $newImage;
                }
                copy($basePath . $entity->getImage(), $basePath . $newImage);
                $entity->setImage($newImage);
            } else {
                $entity->setImage(null);
            }
        } else {
            $entity->setImage(null);
        }
        return $entity;
    }
}