<?php

namespace OfficeBrain\CustomBundle\ProductBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use OfficeBrain\Bundle\ProductBundle\Controller\ProductController as BaseProductController;
use OfficeBrain\Bundle\CoreBundle\Cache\RedisCacheService;
/**
 *
 * @author Employee Id: 4464
 *
 * To create custom code
 *
 * @return NULL
 *
 * @Route("/{_locale}")
 */
class ProductController extends BaseProductController
{
    
    /**
     *
     * @author Employee Id: 4464
     *
     * To set member variables default value
     *
     * @return NULL
     *
     */
    public function __construct()
    {
        parent::__construct();
    }
    
    /**
     *
     * @author Employee Id: 4464
     * To unseset member variables
     *
     * @return NULL
     */
    public function __destruct()
    {
        parent::__destruct();
    }
    
    /**
     *
     * @author Employee Id: 4464
     * To Initialize the member variables
     *
     * @return NULL
     */
    public function initAction()
    {
        parent::initAction();
        $this->categoryApi = $this->get("officebrain_category_api");
        $this->userId = $this->projectSetting['user_id'];
//       	$this->productDetailManager = $this->get('office_brain_product_detail_manager_custom');
    }
        
    /**
     * @author Employee Id: 5069
     * Desciption: Related Product Information.
     *
     * @param integer product id
     *
     * @return product related information
     *
     * @Route("/custom-related-product/{productId}",
     * 	name="officebrain_custom_bundle_product_bundle_product_relatedproduct",
     * )
     *
     * @Template("OfficeBrainProductBundle:Product:relatedProduct.html.twig")
     *
     */
    
    public function customRelatedProductAction()
    {  
    	$this->initAction();
        $productId = $this->request->get('productId', null);
        $this->productDetailCoreManager = $this->get('office_brain_product_detail_manager');
        $relatedProductIds = $this->productDetailCoreManager->getRelatedProductDetailInfo($productId, $this->cultureCode);
        $comproductreletedid = array_column($relatedProductIds['relatedProducts'],'relatedProductId');
        if(count($comproductreletedid) > 0)
        {
            $comproductreletedid = array_unique($comproductreletedid);
            $this->request->query->set('related_product_ids', $comproductreletedid);
            $searchResult =  $this->searchManager->searchService();
            $this->responseArray['related_product'] = $searchResult['search_result_array']['data'];
        }
    	return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 5429
     *
     * @Route("/image-library/{page}",name="officebrain_custom_bundle_product_bundle_image_library",
     * requirements={"records_per_page"="12|24|48|100|996","page"="\d+"},
     * defaults={"records_per_page"=20,"page"=1}),
     * @Template("OfficeBrainCustomBundleProductBundle:Product:imageLibrary.html.twig")
     *
     */
    public function imageLibraryAction()
    {
    	$this->initAction();
    	
    	if($this->userId==0)
    	{
    		$this->get('session')->set('_security.main.target_path', $this->generateUrl($this->request->get('_route'),array('page'=>$this->request->get('page'))));
    		return $this->redirect($this->generateUrl('office_brain_userbundle_login_login'));
    	}
    	
    	$culture = $this->cultureCode;
    	list($cultureName[],$country[]) = explode("_",$culture);
    	
    	$this->categoryApiManager = $this->get('officebrain_category_api_manager');
    	$category = $this->categoryApiManager->getCategoryMapping();
    	
    	$masterIds = array_column($category, 'pms_id');

    	$params = array('cultures'=>json_encode($cultureName),'master_id'=>json_encode($masterIds));
    	
    	$rCultures = implode($cultures_to_pass);
    	$rmasterIds= implode($masterIds);
    	
    	/* Redis cache processing started */
			$locale=$this->request->getLocale();
			$smallString = md5($rCultures.$rmasterIds);
			$cacheSiteKey = array('cuniquep'.$smallString,$locale,$this->projectSetting['instance_id'],$this->container->getParameter('kernel.environment'));
			
			$this->moduleCachePrefix='__SiteModuleHomePage__:';
			$this->statsh_cache_wrapper=RedisCacheService::create();
			//dump($this->statsh_cache_wrapper);exit;
			$cacheKey = $this->moduleCachePrefix.RedisCacheService::generateKey($this->statsh_cache_wrapper,$cacheSiteKey);
			
			//echo $cacheKey;exit;
			$cacheData = RedisCacheService::getCacheData($this->statsh_cache_wrapper,$cacheKey);
			if($cacheData){
				$data = $cacheData;
			}
			else
			{
				$this->categoryApi->init('list_category');
				$this->categoryApi->setCallParameters($params);
				$this->categoryApi->doProcess();
				if ($this->categoryApi->isSuccess()) {
					$data = $this->categoryApi->getResultArray();/* API processing ended */
					if(!empty($cacheKey))
					{
						RedisCacheService::saveCacheData($this->statsh_cache_wrapper,$cacheKey,$data);
					}
				}
			}
			/* Redis cache processing ended */

    	foreach ($category as $key)
    	{
    		if (array_key_exists($key['pms_id'],$data['en']))
    		{
    			//$data['en'][$key['pms_id']]['parentid']=$key['parentid'];
    			$data['en'][$key['pms_id']]['haschildid']=$key['haschildid'];
    		}
    			
    	}
    	return $data;
    }
}
