<?php

namespace Lc\ShopBundle\Controller\Frontend ;

use Doctrine\ORM\EntityManagerInterface;
use Lc\ShopBundle\Context\MerchantUtilsInterface;
use Lc\ShopBundle\Context\OrderUtilsInterface;
use Lc\ShopBundle\Context\ProductFamilyInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;

class FavoriteController extends BaseController
{
        protected $productFamilyRepository ;

        public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils)
        {
                parent::__construct($em, $security, $merchantUtils);
                $this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetaData(ProductFamilyInterface::class)->getName()) ;
        }

        public function toggle(Request $request)
        {
                $user = $this->_getUser() ;
                $productFamily = $this->_getProductFamily($request) ;

                if($user->getFavoriteProductFamilies()->contains($productFamily)) {
                        $user->removeFavoriteProductFamily($productFamily) ;
                        $isFavorite = false ;
                        $message = 'Le produit a bien été supprimé de vos favoris' ;
                }
                else {
                        $user->addFavoriteProductFamily($productFamily) ;
                        $isFavorite = true ;
                        $message = 'Le produit a bien été ajouté à vos favoris' ;
                }

                $this->_saveUser($user) ;

                return new JsonResponse([
                        'return' => 'success',
                        'is_favorite' => $isFavorite,
                        'message' => $message
                ]) ;
        }

        public function add(Request $request)
        {
                $user = $this->_getUser() ;
                $productFamily = $this->_getProductFamily($request) ;

                $user->addFavoriteProductFamily($productFamily) ;
                $this->_saveUser($user) ;

                return new JsonResponse([
                        'return' => 'success',
                        'message' => 'Le produit a bien été ajouté à vos favoris'
                ]) ;
        }

        public function delete(Request $request)
        {
                $user = $this->_getUser() ;
                $productFamily = $this->_getProductFamily($request) ;

                $user->removeFavoriteProductFamily($productFamily) ;
                $this->_saveUser($user) ;

                return new JsonResponse([
                        'return' => 'success',
                        'message' => 'Le produit a bien été supprimé de vos favoris'
                ]) ;
        }

        private function _getUser()
        {
                $user = $this->security->getUser() ;
                if(!$user) {
                        throw new \ErrorException('Vous devez être connecté pour gérer vos favoris') ;
                }

                return $user ;
        }

        private function _saveUser($user)
        {
                $this->em->persist($user);
                $this->em->flush() ;
        }

        private function _getProductFamily($request)
        {
                $idProductFamily = $request->request->get('idProductFamily') ;
                $productFamily = $this->productFamilyRepository->find($idProductFamily) ;

                if($productFamily) {
                        return $productFamily ;
                }
                else {
                        throw new \ErrorException('Ce produit est introuvable') ;
                }
        }
}