<?php

namespace Lc\SovBundle\Component;

use Doctrine\ORM\EntityManagerInterface;
use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
use Lc\SovBundle\Doctrine\Extension\SluggableInterface;
use Lc\SovBundle\Model\File\FileInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

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

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

    public function duplicateEntity($entity, $flush = true)
    {
        $newEntity = clone $entity;

        if ($newEntity instanceof FileInterface) {
            $newEntity = $this->duplicateImage($newEntity);
        }

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

        if (method_exists($newEntity, 'getAddress') && is_object($newEntity->getAddress())) {
            $address = $newEntity->getAddress();
            $newAddress = $this->duplicateEntity($address);
            $newEntity->setAddress($newAddress);
            $this->entityManager->persist($newAddress);
        }

        if ($newEntity instanceof SluggableInterface) {
            $this->entityManager->persist($newEntity);
            if ($flush) {
                $this->entityManager->flush();
            }
            $newEntity->setSlug(null);
        }
        $this->entityManager->persist($newEntity);
        if ($flush) {
            $this->entityManager->flush();
        }


        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;
    }
}