<?php

namespace OfficeBrain\Bundle\BannerBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use OfficeBrain\Bundle\BannerBundle\Entity\Banner;
use OfficeBrain\Bundle\BannerBundle\Entity\BannerLanguage;
//use OfficeBrain\Bundle\BannerBundle\Entity\BannerCountry;
use OfficeBrain\Bundle\BannerBundle\Form\BannerType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Validator\Constraints\Count;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use OfficeBrain\Bundle\CoreBundle\Cache\RedisCacheService;

/**
 *
 * @author OfficeBrain 4571 <info@officebrain.com>
 *
 * Description: Front Controller used to Display Banner in front
 *
 * @Route("{_locale}")
 *
 */
class BannerController extends Controller
{
    private $translator;
    private $request;
    private $projectSetting;
    private $manager;
    private $isAjax;
    private $session;
    private $responseArray;
    
    /**
     *
     * @author Employee Id: 4571
     * Description : To initialize variables
     *
     * @param  : null
     *
     * @return null
     *
     * @throws null
     *
     **/
    public function __construct()
    {
        $this->request = null;
        $this->manager = null;
        $this->responseArray = array();
        $this->projectSetting = null;
        $this->session = null;
        $this->isAjax = null;
        $this->translator = null;
         $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->manager);
        unset($this->responseArray);
        unset($this->projectSetting);
        unset($this->session);
        unset($this->isAjax);
        unset($this->translator);
        unset($this->statsh_cache_wrapper);
    }
    
    /**
     *
     * @author Employee Id: 4571
     * Description : to initialize request and services
     *
     * @param : null
     *
     * @return null
     *
     * @throws null
     *
     **/
    public function initAction()
    {
        $this->request = $this->getRequest();
        $this->manager = $this->get('office_brain_banner_manager');
        $this->translator = $this->get('translator');
        $this->session = $this->request->getSession();
        $this->isAjax = $this->request->isXmlHttpRequest();
        $this->projectSetting = $this->request->get('project_setting');
    }
    
    /**
     *
     * @author Employee Id: 4571
     * Description : Fetch Active Banners
     *
     * @param : null
     *
     * @return null
     *
     * @throws null
     *
     * @Route("/banner/get-banners", name="office_brain_bannerbundle_banner_getallactivebanner")
     * @Template("OfficeBrainBannerBundle:Banner:getAllActiveBanner.html.twig")
     *
     **/
    public function getAllActiveBannerAction()
    {	
        //$this->initAction();
        $section = $this->getRequest()->get('displayOn');
        $slug = $this->getRequest()->get('slug',NULL);
        $categoryFilter = $this->getRequest()->get('categoryFilter',NULL);
        
        if(is_array($categoryFilter)){
			$categoryFilter = $categoryFilter[0];
		}
		
        // -----------------------------------------------------------------------
        $this->moduleCachePrefix='__HomePageBannerList__:';
        if($categoryFilter!=NULL){
			$this->moduleCachePrefix='__HomePageBannerList__:__'.$categoryFilter.'__:';
		}
        if($slug!=NULL){
            $this->moduleCachePrefix='__HomePageBannerList__:__'.$section.'__:_'.$slug.'_:';
        }
		$cacheSiteKey = array($section,$locale,$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;
		}
        $directory = $this->getParameter('banner.image_url');
        $this->responseArray['banner_list'] = $this->get('office_brain_banner_manager')->fetchActiveBanner();
        $this->responseArray['image_path'] = $directory;
        $this->responseArray['display_on']= $this->getRequest()->get('displayOn');
        $response = $this->render('OfficeBrainBannerBundle:Banner:getAllActiveBanner.html.twig',$this->responseArray);
		if(!empty($cacheKey))
		{
			RedisCacheService::saveCacheData($this->statsh_cache_wrapper,$cacheKey,$response);
		}
	   
        return $response;
    }
    
    /**
     *
     * @author Employee Id: 4571
     * Description : Fetch Banner Transitions
     *
     * @param : null
     *
     * @return js output
     *
     * @throws null
     *
     * @Route("/assets/js/banner-trans.js/{display}", name="office_brain_bannerbundle_banner_findbannertrans")
     *
     * @Method("GET")
     * @Template()
     *
     **/
    public function findBannerTransAction($display)
    {
        //$this->initAction();
        
      
        $this->responseArray['banner_transition'] = $this->get('office_brain_banner_manager')->findBannerTransition($display);
                
        $response = new Response();
        $response->headers->set('Content-Type', 'text/javascript');
        
        return $this->render(
                'OfficeBrainBannerBundle:Banner:bannerCustomJs.html.twig',
                $this->responseArray,
                $response
        );
    }    
    
     /**
     *
     * @author Employee Id: 4835
     * Description : details save in db for anayltics
     *
     * @param : $id banner id
     *
     * @return null
     *
     * @throws null
     *
     * @Route("/banner/linkajax/{id}", name="office_brain_bannerbundle_banner_linkajax",options={"expose"=true})
     *
     **/
    public function linkAjaxAction($id)
    {
        $this->initAction();
       
        $bannerServiceLog=$this->get("office_brain_activity_api_manager");
        $bannerUrl=$this->manager->fetchBannerDetail($bannerServiceLog, $id);
      
        return new JsonResponse($bannerUrl);
    }
}
