<?php 
 
 
namespace AutomarketBundle\Controller; 
 
use CoreBundle\Component\CoreFormFactory; 
use CoreBundle\Controller\ViDiController; 
use CoreBundle\Entity\Dealer; 
use Doctrine\ORM\EntityManagerInterface; 
use PortalBundle\Model\SeoMetaTag; 
use Symfony\Component\HttpFoundation\RequestStack; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 
use Twig\Error\Error; 
 
class BaseController extends ViDiController 
{ 
    protected RequestStack $requestStack; 
    protected EntityManagerInterface $em; 
    protected SeoMetaTag $seoMetaTag; 
 
    public function __construct(CoreFormFactory $coreFormFactory, RequestStack $requestStack, EntityManagerInterface $em, 
                                SeoMetaTag $seoMetaTag) 
    { 
        parent::__construct($coreFormFactory); 
        $this->requestStack = $requestStack; 
        $this->em = $em; 
        $this->seoMetaTag = $seoMetaTag; 
    } 
 
    /** 
     * @var array 
     */ 
    private static $dealers = []; 
 
 
    /** 
     * @return mixed 
     */ 
    public function getDealer() 
    { 
        $request = $this->requestStack->getCurrentRequest(); 
        $host = $request->getHost(); 
        if (!isset(self::$dealers[md5($host)])) { 
            $Dealer = $this->em->getRepository(Dealer::class)->findOneBy(['domain' => $host]); 
 
            if (!$Dealer) { 
                throw new NotFoundHttpException(); 
            } 
            self::$dealers[md5($host)] = $Dealer; 
        } 
 
        return self::$dealers[md5($host)]; 
    } 
 
    public function formFactory() 
    { 
        return $this->CoreFormFactory(); 
    } 
 
    /** 
     * @param string $view 
     * @param array $parameters 
     * @param Response|null $response 
     * @return Response 
     * @throws Error 
     */ 
    protected function baseAutomarketRender($view, array $parameters = [], Response $response = null): Response 
    { 
        $request = $this->requestStack->getCurrentRequest(); 
 
        $dealer = $this->getDealer(); 
 
        $seoMeta = $this->seoMetaTag->getSeoMeta($request); 
 
        $compareCookie = $request->cookies->get('compare'); 
        $countCompare = ($compareCookie)? count( explode(',', $compareCookie)) : ''; 
 
        $parameters = array_merge($parameters, ['forms' => $this->buildForms(), 'backLoginUrl' => $request->getUri(), 'dealer' => $dealer,  'privacyUrl' => $this->get('router')->generate('automarket_personal_data_agreement'), 'seoMeta' => $seoMeta, 'countCompare' => $countCompare]); 
 
        $helpCrunchParams = $this->getParameter('help_crunch_params')[$dealer->getUniqueId()] ?? null; 
        if ($helpCrunchParams) { 
            $parameters = array_merge($parameters,['helpCrunch' => $helpCrunchParams]); 
        } 
 
        return parent::render($view, $parameters, $response); 
    } 
 
    protected function buildForms() 
    { 
        return [ 
            'callback' => $this->CoreFormFactory()->orderCallForm(null, $this->getDealer(), null, true)->createView(), 
            'question' => $this->CoreFormFactory()->feedbackQuestionForm(null, $this->getDealer())->createView(), 
            'sendQuestionForm' => $this->CoreFormFactory()->sendQuestionForm(null, $this->getDealer())->createView(), 
        ]; 
    } 
}