<?php

namespace OfficeBrain\Bundle\SearchBundle\Manager\AbstractManager;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;

abstract class SearchAbstract
{
    protected $recordPerPage;
    protected $entityManager;
    protected $request;
    protected $productDetailService;
    protected $projectSetting;
    protected $instanceId;
    protected $instanceType;
    protected $userId;
    protected $cultureId;
    protected $cultureCode;
    protected $accessRule;
    protected $configurationRule;
    protected $categoryManager;
    protected $sessionManager;
    protected $sessionRecentSearchKey;
    protected $serviceGroupId;
    
    public function __construct($entityManager, $request, $solrManager, $productDetailManager, $categoryManager, $sessionManager, $paginatorManager, $serviceContainer)
    {
        $this->entityManager = $entityManager;
        $this->request = $request;
        $this->solrManager = $solrManager;
        $this->productDetailService = $productDetailManager;
        $this->categoryManager = $categoryManager;
        $this->paginatorManager = $paginatorManager;
        $this->serviceContainer = $serviceContainer;
        $this->sessionManager = $sessionManager;
        $this->languageId = null;
        $this->recordPerPage = null;
        $this->formFactory = $this->serviceContainer->get('form.factory');
        
        $this->projectSetting = $this->request->get('project_setting');
        $this->instanceId = $this->projectSetting['instance_id'];
        $this->instanceType = $this->projectSetting['instance_type'];
        $this->userId = $this->projectSetting['user_id'];
        $this->cultureId = $this->projectSetting['culture_id'];
        $this->cultureCode = $this->projectSetting['locale_language'];
        
        $this->sessionRecentSearchKey = 'recent-search-'.$this->instanceId.'-'.$this->cultureId;
        $this->continueShoppingUrlSessionKey = 'continue-shopping-to-search-'.$this->instanceId.'-'.$this->cultureId;
        $this->serviceGroupId = null;
        //$this->init(); //EMP4464: This method never calles below method. It is calling SolrSearchExtended class's method. So committed.
    }
    
    public function __destruct()
    {
        unset($this->request);
        unset($this->entityManager);
        unset($this->productDetailManager);
        unset($this->paginatorManager);
        unset($this->solrManager);
        unset($this->serviceContainer);
        unset($this->projectSetting);
        unset($this->instanceId);
        unset($this->instanceType);
        unset($this->userId);
        unset($this->cultureId);
        unset($this->cultureCode);
        unset($this->accessRule);
        unset($this->categoryManager);
        unset($this->configurationRule);
        unset($this->searchHelper);
        unset($this->recordPerPage);
        unset($this->sessionManager);
        unset($this->sessionRecentSearchKey);
        unset($this->formFactory);
        unset($this->continueShoppingUrlSessionKey);
        unset($this->serviceGroupId);
    }
    
    public function init()
    {
        $this->prepareProjectSetting();
        $this->languageId = $this->getLanguageId();
    }
    
    public function prepareProjectSetting()
    {
        $this->projectSetting = $this->request->get('project_setting');
        $this->instanceId = $this->projectSetting['instance_id'];
        $this->instanceType = $this->projectSetting['instance_type'];
        $this->userId = $this->projectSetting['user_id'];
        $this->cultureId = $this->projectSetting['culture_id'];
        $this->cultureCode = $this->projectSetting['culture_code'];
        $this->configurationRule = $this->projectSetting['configuration_rule']['search'];
        $this->accessRule = $this->projectSetting['access_rule']['search'];
        $this->createdUid = $this->userId;
        $this->upatedUid =$this->userId;
        
        $this->searchHelper = new SearchHelper();
    }
    
    public function SearchFunction($productSearchData, $getFacet, $searchHelper)
    {
    }

    public function filterProduct()
    {
    }

    /**
     * @author Employee Id: 4532
     * Description : This function is used for get product detail information by calling product detail manager
     *
     * @param string $productSlug
     * @param string $culture
     *
     * @return array
     *
     * @throws Exception
     *
     **/
    public function getQuickProductDetailBySlug($productSlug, $culture)
    {
        return $this->productDetailService->getProductDetailBySlug($productSlug, $culture);
    }
    
    public function getLanguageId()
    {
        switch ($this->cultureCode) {
            case 'fr':
                $language = '3';
                break;
            default:
                $language = '1';
                break;
        }
    }
    
    public function getcontinueshoppingurl($controllerObj)
    {
    	if ($this->sessionManager->has($this->continueShoppingUrlSessionKey)) {
    		$redirectUrl = $this->sessionManager->get($this->continueShoppingUrlSessionKey);
    		$this->sessionManager->remove($this->continueShoppingUrlSessionKey);
    		return $redirectUrl;
    	}
    	else {
    		return $controllerObj->generateUrl('office_brain_search_bundle_search');
    	}
    }
    
    public function setContinueShoppingUrl()
    {
    	$url = $this->request->getScheme().'://'.$this->request->getHttpHost().$this->request->getRequestUri();
    	$this->sessionManager->set($this->continueShoppingUrlSessionKey, $url);
    }
}