<?php

namespace Lc\ShopBundle\Controller\Backend;

use Lc\ShopBundle\Context\MerchantConfigInterface;
use Lc\ShopBundle\Context\MerchantInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;

class MerchantController extends AdminController
{

        public function persistEntity($entity)
        {
                $this->createAvailableOptions($entity) ;
                parent::persistEntity($entity);
        }

        public function updateEntity($entity)
        {
                $this->createAvailableOptions($entity) ;
                parent::updateEntity($entity);
        }

        public function createAvailableOptions($entity)
        {
                $merchantConfigs = $entity->getMerchantConfigs() ;
                $classMerchantConfig = $this->em->getClassMetadata(MerchantConfigInterface::class)->getName() ;
                $availableOptions = $classMerchantConfig::getAvailableOptions() ;

                $addConfig = false ;
                foreach($availableOptions as $key => $option) {
                        $optionExist = false ;
                        foreach($merchantConfigs as $merchantConfig) {
                                if($merchantConfig->getName() == $key) {
                                        $optionExist = true ;
                                }
                        }
                        if(!$optionExist) {
                                $newMerchantConfig = new $classMerchantConfig ;
                                $newMerchantConfig
                                        ->setName($key)
                                        ->setMerchant($entity) ;
                                if(isset($option['default'])) {
                                        $newMerchantConfig->setValue($option['default']) ;
                                }
                                $addConfig = true ;
                                $this->em->persist($newMerchantConfig);
                        }
                }

                if($addConfig) {
                        $this->em->flush() ;
                }
        }

        public function editAction()
        {
                // paramètres (admin)
                $isSettings = $this->request->query->get('is_settings') ;
                if($isSettings) {
                        $idMerchant = $this->security->getUser()->getMerchant()->getId() ;
                        $this->request->query->set('id', $idMerchant) ;
                        $easyadmin = $this->request->attributes->get('easyadmin');
                        $merchant = $this->getDoctrine()
                                ->getRepository(MerchantInterface::class)
                                ->find($idMerchant);
                        $easyadmin['item'] = $merchant;
                        $this->request->attributes->set('easyadmin', $easyadmin) ;

                        $response = parent::editAction() ;

                        if ($response instanceof RedirectResponse) {

                                $referer = $this->request->headers->get('referer');
                                return new RedirectResponse($referer);
                        }
                        else {
                                return $response ;
                        }
                }
                // édition des Merchant (super admin)
                else {
                        return parent::editAction() ;
                }
        }

        public function switchMerchantAction(Request $request): RedirectResponse
        {
                $em = $this->getDoctrine()->getManager();
                $user = $this->security->getUser() ;
                if($user->hasRole('ROLE_SUPER_ADMIN')) {
                        $idMerchant = $request->request->get('id_merchant');
                        $merchant = $this->getDoctrine()
                                ->getRepository(MerchantInterface::class)
                                ->find($idMerchant);

                        if ($merchant) {

                                $user->setMerchant($merchant);
                                $em->persist($user);
                                $em->flush();
                        }
                }
                return $this->redirect('/admin/dashboard') ;
        }

}