<?php

class offers
{
	const REDIS_KEY_PREFIX = 'cp_';
    const REDIS_HOST = "127.0.0.1";
	const REDIS_PORT = "22133";
	// const REDIS_PORT = "6378";
	
	protected $_redis;

	function __construct() {
		try {
			$this->_redis = new Redis();
            $this->_redis->pconnect( self::REDIS_HOST, self::REDIS_PORT );
		} catch( Exception $e ) {
			return;
		}
	}

	public function getCategories() {
		return $this->getRedisKeyData( self::REDIS_KEY_PREFIX . "categories" );
	}

	public function getPopularStores() {
		return $this->getRedisKeyData( self::REDIS_KEY_PREFIX . "popular_stores" );
	}

	public function getTopStores() {
		return $this->getRedisKeyData( self::REDIS_KEY_PREFIX . "top_stores" );
	}

	public function getLatestOffers() {
		return $this->getRedisKeyData( self::REDIS_KEY_PREFIX . "latest_offers" );
	}

	public function getLeisureAndTravelStores() {
		return $this->getRedisKeyData( self::REDIS_KEY_PREFIX . "travel_offers" );
	}

	public function getPopularCategories() {
		return $this->getRedisKeyData( self::REDIS_KEY_PREFIX . "popular_categories" );
	}

	public function getTopSavings() {
		return $this->getRedisKeyData( self::REDIS_KEY_PREFIX . "top_savings" );
	}

	public function getTop20Deals() {
		return $this->getRedisKeyData( self::REDIS_KEY_PREFIX . "top20_deals" );
	}

	public function getAllLatestOffers() {
		return $this->getRedisKeyData( self::REDIS_KEY_PREFIX . "all_latest_offers" );
	}

	public function getAllTopSavingOffers() {
		return $this->getRedisKeyData( self::REDIS_KEY_PREFIX . "all_top_savings" );
	}

	public function getAllBestCoupons() {
		return $this->getRedisKeyData( self::REDIS_KEY_PREFIX . "all_best_coupons" );
	}

	public function getFeaturedStores() {
		return $this->getRedisKeyData( self::REDIS_KEY_PREFIX . "featured_stores" );
	}

	public function getAlphabeticalStores( $alphabet ) {
		return $this->getRedisHashKeyData( self::REDIS_KEY_PREFIX . "alphabetical_stores", $alphabet );
	}

	public function getAllStores() {
		return $this->getRedisHashAllData( self::REDIS_KEY_PREFIX . "alphabetical_stores");
	}

	public function getAlphabeticalCategoryStores( $alphabet, $category_name ) {
		return $this->getRedisHashKeyData( self::REDIS_KEY_PREFIX . "alphabetical_category_stores_$category_name", $alphabet );
	}

	public function getAllCategoryStores( $category_name ) {
		return $this->getRedisHashAllData( self::REDIS_KEY_PREFIX . "alphabetical_category_stores_$category_name");
	}

	public function getSearchBrands( $term ) {
		return  $this->searchInRedis( $term );
	}

    public function getRedisKeyData( $key ) {
        try {
            $result = $this->_redis->get($key);
            return json_decode( $result, true );
        } catch( Exception $e ) {
            return false;
        }
    }
    
    public function getRedisHashKeyData( $hash, $key ) {
		try{
            $result = $this->_redis->hget( $hash, $key ) ;
			return json_decode( $result, true );
		} catch( Exception $e ) {
			return false;
		}
	}    
    public function searchInRedis( $keyword ) {
        $keyword = strtolower( preg_replace('/\s+/', '', $keyword) );
		$cursor = NULL;
		$this->_redis->setOption( Redis::OPT_SCAN, Redis::SCAN_RETRY );
        $result = $this->_redis->hScan('cp_brands', $cursor, "*$keyword*", 100000);
		
        return $result;
    }

	public function getCashbackCategories() {
		return $this->getRedisKeyData( self::REDIS_KEY_PREFIX . "cashback_merchant_categories" );
	}

	public function getCashbackMerchants() {
		return $this->getRedisKeyData( self::REDIS_KEY_PREFIX . "cashback_stores" );
	}

	public function getSearchCashbackCategories() {
		return $this->getRedisKeyData( self::REDIS_KEY_PREFIX . "search_cashback_merchant_categories" );
	}

	public function getRedisHashAllData($hash_name) {
		try{
			return $this->_redis->hgetall($hash_name) ;
		} catch( Exception $e ) {
			return false;
		}
	}
}
