<?php

namespace OfficeBrain\Bundle\ProductBundle\Helper;

use OfficeBrain\Bundle\CoreBundle\Cache\RedisCacheService;

/**
 * Product Detail Helper class
 *
 * @author OfficeBrain 4014 <info@officebrain.com>
 * Description : This class having common function which are available globally
 */
class CacheAPIProductHelper
{
    protected $cacheClient = null;
    CONST PRODUCT_CACHE_CLIENT = 'product';
    protected $productCachePartialMergeFlag = false;
    
    protected $productCachePartialResult = array();
    
    public function __construct()
    {
        
    }
    
    public function __destruct()
    {
        unset($this->cacheClient);
        unset($this->productCachePartialMergeFlag);
        unset($this->productCachePartialResult);
    }
    
    public function viewProduct(&$productApiClient)
    {
        if(!RedisCacheService::isCacheEnable())
        {
            return null;
        }
        
        if(!empty($productApiClient->getCallParameters()['product_slug']))
        {
            $cacheClient=RedisCacheService::create(null,self::PRODUCT_CACHE_CLIENT);
            $keys=RedisCacheService::getCacheByPrefix($cacheClient,"slug##".$productApiClient->getCallParameters()['product_slug'].":".$productApiClient->getCulture());
            if(!empty($keys[0])) {
                $cacheKey=$keys[0];
                $cacheData=RedisCacheService::getCacheDataWithoutPrifix($cacheClient,$cacheKey);
                if($cacheData) {
                    $productApiClient->setResultSet($cacheData);
                    return true;
                }
            }
        }
        if(!empty($productApiClient->getCallParameters()['product_ids']) && empty($productApiClient->getCallParameters()['is_solr']))
        {
            $productIds=(json_decode($productApiClient->getCallParameters()['product_ids']));
            if($productIds!='' && !is_array($productIds))
            {
                $productIds=array($productIds);
            }
            $arrProduct=array();
            $cacheClient=RedisCacheService::create(null,self::PRODUCT_CACHE_CLIENT);
            $arrProductNoCache=array();
            foreach ($productIds as $key => $value) {
                $keys=RedisCacheService::getCacheByPrefix($cacheClient,":".$value.":slug##*:".$productApiClient->getCulture());
                if($keys){
                $cacheKey=$keys[0];
                $cacheData=RedisCacheService::getCacheDataWithoutPrifix($cacheClient,$cacheKey);
                if($cacheData) {
                    $arrProduct[$value]=$cacheData;
                }
                else {
                    $arrProductNoCache[]=$value;
                }
            }
                else {
                	$arrProductNoCache[]=$value;
                }
            }
            if(count($arrProductNoCache)==0)
            {
                $productApiClient->setResultSet($arrProduct);
                return true;
            }
            $arr['product_ids']=json_encode($arrProductNoCache);
            
            $productApiClient->updateCallParameters($arr);
            $this->setProductCachePartialMergeFlag(true);
            
            $this->productCachePartialResult=$arrProduct;
        }
        
        return false;
    }
    
    public function updatePartialResultSet(&$productApiClient)
    {
        $arrProduct=array_replace($productApiClient->getResultSet(),$this->productCachePartialResult);
        $productApiClient->setResultSet($arrProduct);
    }
    
    public function setProductCachePartialMergeFlag($flag=false)
    {
        $this->productCachePartialMergeFlag=$flag;
    }
    
    public function getProductCachePartialMergeFlag()
    {
        return $this->productCachePartialMergeFlag;
    }
}
