<?php
namespace OfficeBrain\Bundle\CoreBundle\Cache;

use OfficeBrain\Bundle\CoreBundle\Cache\Redis;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;

class RedisCacheService
{
    public static $serviceContainer;
    const REDIS_PREFIX = 'snc_redis.';
    const KEY_PREFIX = '__project__';
    public static $arrRedisClient;
    
    /**
     * constructor
     * @param ContainerInterface $serviceContainer service container
     */
    public function __construct(ContainerInterface $serviceContainer)
    {
        RedisCacheService::$serviceContainer = $serviceContainer;
    }
    
    public static function isCacheEnable()
    {
        if(RedisCacheService::$serviceContainer->hasParameter('cache_enable') && RedisCacheService::$serviceContainer->hasParameter('cache_enable')==1)
        {
            return true;
        }
        return false;
    }
    
    public static function create($cachePrefix=null,$client='default')
    {
        if($cachePrefix==null)
        {
            $cachePrefix=self::KEY_PREFIX;
        }
        $catchEnable=0;
        if(RedisCacheService::isCacheEnable())
        {
            $catchEnable=1;
        }
        if(empty($catchEnable))
        {
            return null;
        }
        
        try{
            $objRedis=RedisCacheService::$serviceContainer->get(self::REDIS_PREFIX.$client);
        }catch(ServiceNotFoundException $e){
            return null;
        }
        $cacheLifeTime=0;
        if(RedisCacheService::$serviceContainer->hasParameter('CacheLifetime'))
        {
            $cacheLifeTime=RedisCacheService::$serviceContainer->getParameter('CacheLifetime');
        }
        //echo "=========$catchEnable,$cachePrefix===$client====";
        if(empty(RedisCacheService::$arrRedisClient) || array_key_exists(self::REDIS_PREFIX.$client, RedisCacheService::$arrRedisClient)==false)
        {
            RedisCacheService::$arrRedisClient[self::REDIS_PREFIX.$client]=new Redis($objRedis,$catchEnable,$cacheLifeTime,$cachePrefix);
        }
        
        return RedisCacheService::$arrRedisClient[self::REDIS_PREFIX.$client]; 
    }
    
    public static function generateKey(&$statsh_cache_wrapper=null,$generateKey=array(),$moduleCachePrefix='')
    {
        if(is_object($statsh_cache_wrapper) && ('OfficeBrain\Bundle\CoreBundle\Cache\Redis'==get_class($statsh_cache_wrapper)))
        {
            return $moduleCachePrefix.$statsh_cache_wrapper->createKey($generateKey);
        }
    }
    
    public static function getCacheData(&$statsh_cache_wrapper=null,$cacheKey)
    {
        if(is_object($statsh_cache_wrapper) && ('OfficeBrain\Bundle\CoreBundle\Cache\Redis'==get_class($statsh_cache_wrapper)))
        {
            $cacheData = $statsh_cache_wrapper->getCacheData($cacheKey);
            if($cacheData){
              return $cacheData;
            }
        }
        return null;
    }
    
    public static function saveCacheData(&$statsh_cache_wrapper=null,$cacheKey,$data)
    {
        if(is_object($statsh_cache_wrapper) && ('OfficeBrain\Bundle\CoreBundle\Cache\Redis'==get_class($statsh_cache_wrapper)))
        {
            $statsh_cache_wrapper->setCacheData($cacheKey,$data);
        }
        return null;
    }
    
    
    public static function getCacheByPrefix(&$statsh_cache_wrapper=null,$cacheKey)
    {
        if(is_object($statsh_cache_wrapper) && ('OfficeBrain\Bundle\CoreBundle\Cache\Redis'==get_class($statsh_cache_wrapper)))
        {
            return $statsh_cache_wrapper->getCacheByPrefix($cacheKey);
        }
        return null;
    }
    
    public static function getCacheDataWithoutPrifix(&$statsh_cache_wrapper=null,$cacheKey)
    {
        if(is_object($statsh_cache_wrapper) && ('OfficeBrain\Bundle\CoreBundle\Cache\Redis'==get_class($statsh_cache_wrapper)))
        {
            return $statsh_cache_wrapper->getCacheDataWithoutPrifix($cacheKey);
        }
        return null;
    }

    public static function removeCacheByPrefix(&$statsh_cache_wrapper=null,$cacheKey)
    {
        if(is_object($statsh_cache_wrapper) && ('OfficeBrain\Bundle\CoreBundle\Cache\Redis'==get_class($statsh_cache_wrapper)))
        {
            return $statsh_cache_wrapper->removeCacheByPrefix($cacheKey);
        }
        return null;
    }

    public static function removeCacheWithPrifix(&$statsh_cache_wrapper=null,$moduleCachePrefix='')
    {
    	if(is_object($statsh_cache_wrapper) && ('OfficeBrain\Bundle\CoreBundle\Cache\Redis'==get_class($statsh_cache_wrapper)))
    	{
    		return $statsh_cache_wrapper->removeCacheWithPrifix($moduleCachePrefix);
    	}
    	return null;
    }
}
