<?php

namespace Lc\ShopBundle\Services;

use Cocur\Slugify\Slugify;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Configuration\ConfigManager;
use Geocoder\Model\Coordinates;
use Geocoder\Provider\Addok\Addok;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
use Lc\ShopBundle\Context\ImageInterface;
use Lc\ShopBundle\Context\MerchantUtilsInterface;
use Lc\ShopBundle\Context\PageInterface;
use Lc\ShopBundle\Context\PointSaleInterface;
use Lc\ShopBundle\Context\ProductFamilyInterface;
use Lc\ShopBundle\Context\ProductFamilyUtilsInterface;
use Lc\ShopBundle\Context\ReminderInterface;
use Lc\ShopBundle\Context\SluggableInterface;
use Lc\ShopBundle\Context\TaxRateInterface;
use Lc\ShopBundle\Context\UnitInterface;
use Lc\ShopBundle\Context\UserInterface;
use Lc\ShopBundle\Context\UserPointSaleInterface;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpClient\HttplugClient;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class UtilsProcess
{
        protected $em;
        protected $parameterBag;
        protected $merchantUtils;
        protected $productFamilyUtils;


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


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

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

                if ($newEntity instanceof ProductFamilyInterface) {
                        $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->em->persist($newAddress);
                }

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


                return $newEntity;
        }

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

                if ($hub) {
                        $newEntity->setMerchant($hub);
                }
                $this->em->persist($newEntity) ;
                $this->em->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 = (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);
                }
                return $entity;
        }


}
