<?php

namespace OfficeBrain\Bundle\SearchBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use OfficeBrain\Bundle\CoreBundle\Cache\RedisCacheService;

class BrowseBrandController extends Controller
{
	/**
	 *
	 * @author Employee Id: 4532
	 *
	 * To set member variables default value
	 *
	 * @return NULL
	 */
	public function __construct()
	{
		$this->translator = null;
		$this->request = null;
		$this->responseArray = array();
		$this->statsh_cache_wrapper=RedisCacheService::create();
	}

	/**
	 *
	 * @author Employee Id: 4861
	 *
	 * Desciption: To unset member variables
	 *
	 * @param NULL
	 *
	 * @return null
	 *
	 * @throws NULL
	 */
	public function __destruct()
	{
		unset($this->translator);
		unset($this->request);
		unset($this->responseArray);
		unset($this->statsh_cache_wrapper);
	}


	/**
	 *
	 * @author Employee Id: 4532
	 *
	 * Create various objects required action in this class
	 *
	 * @return NULL
	 */
	public function initAction()
	{
		$this->request = $this->getRequest();
        $this->projectSetting = $this->request->get('project_setting');
        $this->serviceGroupId = $this->projectSetting['service_group_id'];
        $this->instanceId = $this->projectSetting['instance_id'];
        $this->userId = $this->projectSetting['user_id'];

        $this->searchManager = $this->get('office_brain_search_manager');
        $this->languageId = $this->searchManager->getLanguageId();
        $this->entityManager = $this->getDoctrine()->getManager();
        $this->locale = $this->projectSetting['locale_language'];
        $this->translator = $this->get('translator');
	}

    /**
     * @author Employee Id: 4014
     *
	 * Browse by brand
	 *
	 * @param string brand
	 *
	 * @return if result found return brand  else null
	 *
	 * @Route("/{_locale}/brand", name="office_brain_search_bundle_browse_brand", options={"expose"=true})
	 *
	 * @Method({"GET"})
	 *
     * @Template("OfficeBrainSearchBundle:BrowseBrand:browseBrand.html.twig")
     */
    public function browseBrandAction()
    {
    	$this->initAction();
        $locale=$this->locale;
        $arrLocale=explode("_", $this->locale);
        $data=array();
        $countryId = $this->container->getParameter('country')[$arrLocale[1]]['id'];
        // Brand List
        

         // -----------------------------------------------------------------------
        $this->moduleCachePrefix='__AllBrandPageList__:';
       
		$cacheSiteKey = array('IDME',$this->projectSetting['instance_id'],$this->container->getParameter('kernel.environment'));
		$cacheKey = $this->moduleCachePrefix.RedisCacheService::generateKey($this->statsh_cache_wrapper,$cacheSiteKey);
		$cacheData = RedisCacheService::getCacheData($this->statsh_cache_wrapper,$cacheKey);
		if($cacheData){
			return $cacheData;
		}

		$this->responseArray['brandList'] = $this->get('office_brain_subshop_manager')->frontGetBrands($this->get("officebrain_brand_api_manager"),$arrLocale[0],$this->projectSetting['instance_id']);

		$response = $this->render('OfficeBrainSearchBundle:BrowseBrand:browseBrand.html.twig',$this->responseArray);

		if(!empty($cacheKey))
		{
			RedisCacheService::saveCacheData($this->statsh_cache_wrapper,$cacheKey,$response);
		}
	   

       /* $this->responseArray['brandList'] = $this->get('office_brain_subshop_manager')->frontGetBrands($this->get("officebrain_brand_api_manager"),$arrLocale[0],$this->projectSetting['instance_id']);
        return $this->responseArray;*/

        return $response;
    }
}
