<?php

namespace OfficeBrain\Bundle\HomeSectionBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use OfficeBrain\Bundle\CoreBundle\Cache\RedisCacheService;

/**
 *
 * @author OfficeBrain 4571 <info@officebrain.com>
 * Description: Manage Banner to perform CRUD operation
 *
 * @Route("{_locale}")
 *
 */
class HomeSectionController extends Controller
{
    
    private $request;
    private $responseArray;
    
        
    /**
     * @author Employee Id: 4571
     * Description : To set member variables default value
     * @param  : null
     * @return null
     * @throws null
     *
     **/
    public function __construct()
    {
        $this->request = null;
        $this->homeSectionManager = null;
        $this->responseArray = array();
        $this->statsh_cache_wrapper=RedisCacheService::create();
    }
        
    /**
     * @author Employee Id: 4571
     * Description : To unset member variables
     *
     * @param  : null
     *
     * @return null
     *
     * @throws null
     *
     **/
    public function __destruct()
    {
        unset($this->request);
        unset($this->homeSectionManager);
        unset($this->responseArray);
        unset($this->statsh_cache_wrappe);
    }
        
    /**
     * @author Employee Id: 4571
     * Description : To initialize request and services
     *
     * @param  : null
     *
     * @return null
     *
     * @throws null
     *
     **/
    public function initAction()
    {
        $this->request = $this->getRequest();
        $this->homeSectionManager = $this->get('office_brain_home_section_manager');
    }
    
    /**
     * @Route("/home-section/{section}", name="office_brain_homesectionbundle_homesectiontype")
     */
    public function homesectiontypeAction($section)
    {
        //$this->initAction();
        $section=$this->getRequest()->get('section');
        
        // -----------------------------------------------------------------------
        $cacheSiteKey = array($section,$locale,$this->projectSetting['instance_id'],$this->container->getParameter('kernel.environment'));
		$this->moduleCachePrefix='__HomePageSectionList__:';
		$cacheKey = $this->moduleCachePrefix.RedisCacheService::generateKey($this->statsh_cache_wrapper,$cacheSiteKey);
		$cacheData = RedisCacheService::getCacheData($this->statsh_cache_wrapper,$cacheKey);
		if($cacheData){
			return $cacheData;
		}
		
		
        $this->responseArray['section'] = $this->get('office_brain_home_section_manager')->getHomeSectionByType($section);
        $this->responseArray['section_type'] = $section;
        //print_r($this->responseArray); die;
        $response = $this->render('OfficeBrainHomeSectionBundle:HomeSection:list.html.twig',$this->responseArray);
        if(!empty($cacheKey))
		{
			RedisCacheService::saveCacheData($this->statsh_cache_wrapper,$cacheKey,$response);
		}
		return $response;
    }
}
