<?php 
 
namespace AutomarketBundle\Controller; 
 
use AutomarketBundle\Services\CatalogService; 
use AutomarketBundle\Services\SeoService; 
use CoreBundle\Component\CoreFormFactory; 
use CoreBundle\Model\Vehicles\VehicleType; 
use DcSiteBundle\Model\Pagination; 
use Doctrine\ORM\EntityManagerInterface; 
use PortalBundle\Model\SeoMetaTag; 
use Symfony\Component\HttpFoundation\JsonResponse; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\RequestStack; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 
 
class CatalogController extends BaseController 
{ 
    private CatalogService $catalogService; 
    private SeoService $seoService; 
    private Pagination $modelPagination; 
 
 
    public function __construct(CoreFormFactory $coreFormFactory, RequestStack $requestStack, EntityManagerInterface $em, 
                                SeoMetaTag      $seoMetaTag, CatalogService $catalogService, SeoService $seoService, 
                                Pagination      $modelPagination) 
    { 
        parent::__construct($coreFormFactory, $requestStack, $em, $seoMetaTag); 
        $this->catalogService = $catalogService; 
        $this->seoService = $seoService; 
        $this->modelPagination = $modelPagination; 
    } 
 
    const PRE_PAGES = 18; 
    const MAX_PAGINATION_BUTTON = 5; 
    const TYPE_FILTER_BY_MODEL_UNIQUE = ['BT', 'FT', 'DT', 'TT', 'BC', 'SC']; 
 
    public function catalog(Request $request): Response 
    { 
        $lang = $request->getLocale(); 
 
        $routeParams = $this->catalogService->getRouteFilterParams($this->getDealer()); 
 
        $vehicleTypeData = VehicleType::getTypeDataByUrl($request->get('type')); 
        if (!$vehicleTypeData) { 
            throw new NotFoundHttpException(); 
        } 
 
        $findVehicles = $this->catalogService->findVehicleByParams(self::PRE_PAGES); 
        if (!$findVehicles['data']) { 
            $findVehicles = $this->catalogService->noFindVehicleByParams(self::PRE_PAGES); 
        } 
        $vehicles = $this->catalogService->getVehicleByCatalog($this->getDealer(), $findVehicles['data'], $this->getUser(), $lang); 
        $vehicleIds = []; 
        foreach ($vehicles as $vehicle) { 
            $vehicleIds[] = $vehicle['id']; 
        } 
        $pagination = $this->modelPagination->initPagination($findVehicles['count'], self::PRE_PAGES, self::MAX_PAGINATION_BUTTON); 
 
        if (isset($routeParams['characteristic']) 
            && count($routeParams['characteristic']) == 1 
            && isset($routeParams['characteristic']['body'])) { 
            $seoLinks = $this->seoService->buildSeoLinksByBodyType($vehicleIds); 
        } else { 
            $seoLinks = $this->seoService->buildSeoLinks(); 
        } 
 
 
        $modelRangePage = $this->seoService->findModelRangePage(); 
 
        $breadcrumbs = $this->catalogService->getBreadcrumbs(); 
 
 
        return $this->baseAutomarketRender('@Automarket/Catalog/catalog.html.twig', [ 
            'vehicles' => $vehicles, 
            'seoLinks' => $seoLinks, 
            'modelRangePage' => $modelRangePage, 
            'countVehicle' => $findVehicles['count'], 
            'dataPriceVehicle' => $findVehicles['dataPrice'], 
            'routeParams' => $routeParams, 
            'pagination' => $pagination, 
            'breadcrumbs' => $breadcrumbs, 
            'pageReload' => true, 
        ]); 
    } 
 
    public function catalogSearch(Request $request): Response 
    { 
        $routeParams = $request->query->all(); 
 
        return $this->baseAutomarketRender('@Automarket/Catalog/catalog.html.twig', [ 
            'routeParams' => $routeParams, 
            'pageReload' => false, 
        ]); 
    } 
 
    public function initFilter(Request $request): JsonResponse 
    { 
 
        $lang = $request->getLocale(); 
        $filter = $this->catalogService->buildFilter(self::TYPE_FILTER_BY_MODEL_UNIQUE); 
        $findVehicles = $this->catalogService->findVehicleByParams(self::PRE_PAGES); 
        if (!$findVehicles['data']) { 
            $findVehicles = $this->catalogService->noFindVehicleByParams(self::PRE_PAGES); 
        } 
        $vehicle = $this->catalogService->getVehicleByCatalog($this->getDealer(), $findVehicles['data'], $this->getUser(), $lang); 
        $pagination = $this->modelPagination->initPagination($findVehicles['count'], self::PRE_PAGES, 5); 
        $showMore = $this->catalogService->showMoreCatalog($findVehicles['count'], self::PRE_PAGES); 
        $filterHeader = $this->catalogService->getFilterHeader(); 
 
 
        return $this->json([ 
            'filter' => $filter, 
            'countVehicle' => $findVehicles['count'], 
            'vehicle' => $vehicle, 
            'pagination' => $pagination, 
            'showMore' => $showMore, 
            'filterHeader' => $filterHeader 
        ]); 
    } 
 
    public function getCountVehicleByFilter(): JsonResponse 
    { 
        $findVehicles = $this->catalogService->findVehicleByParams(self::PRE_PAGES); 
        $buildUrl = $this->catalogService->buildSearchUrl(); 
        return $this->json([ 
            'countVehicle' => $findVehicles['count'], 
            'url' => $buildUrl, 
        ]); 
    } 
 
    public function getVehicleByFilter(Request $request): JsonResponse 
    { 
        $lang = $request->getLocale(); 
        $findVehicles = $this->catalogService->findVehicleByParams(self::PRE_PAGES); 
        if (!$findVehicles['data']) { 
            $findVehicles = $this->catalogService->noFindVehicleByParams(self::PRE_PAGES); 
        } 
        $vehicle = $this->catalogService->getVehicleByCatalog($this->getDealer(), $findVehicles['data'], $this->getUser(), $lang); 
        $filterHeader = $this->catalogService->getFilterHeader(); 
        $pagination = $this->modelPagination->initPagination($findVehicles['count'], self::PRE_PAGES, 5); 
        $showMore = $this->catalogService->showMoreCatalog($findVehicles['count'], self::PRE_PAGES); 
        $buildUrl = $this->catalogService->buildSearchUrl(); 
 
        return $this->json([ 
            'countVehicle' => $findVehicles['count'], 
            'vehicle' => $vehicle, 
            'url' => $buildUrl, 
            'pagination' => $pagination, 
            'showMore' => $showMore, 
            'filterHeader' => $filterHeader 
        ]); 
    } 
}