<?php
namespace OfficeBrain\Bundle\CompareProductBundle\Manager\Extended;

use OfficeBrain\Bundle\CompareProductBundle\Manager\AbstractManager\CompareProductAbstract;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;

class CompareProductGuestExtended extends CompareProductAbstract
{
    public function __construct($entityManager, $request, $sessionManager, $productDetailManager, $emailManager,  $twig, $commonLibraryManager, $twigExtension)
    {
        parent::__construct($entityManager, $request, $sessionManager, $productDetailManager, $emailManager,  $twig, $commonLibraryManager, $twigExtension);
        $this->currentCulCode = substr($this->cultureCode, strpos($this->cultureCode, "_") + 1);
    }
    
    public function __destruct()
    {
        parent::__destruct();
        unset($this->currentCulCode);
    }
    
    public function setCompareProduct($productId, $cultureProductId)
    {
        $this->totalProduct = $this->getTotalCount();
        
        if ($this->totalProduct >= $this->comapreProductMaxLimit) {
            return 'ERROR_MAX_LIMIT_REACHED';
        } else {
        	$productKey = $productId.':'.$cultureProductId;
            if (array_key_exists($productKey, $this->sessionProductId[$this->sessionInstanceKey])) {
                return 'ERROR_PRODUCT_ALREADY_EXISTS';
            } else {
            	
            	$pDetail = $this->productDetailManager->getProductDetailById($productId,$this->cultureCode);
            	$pstatus = $pDetail[$productId]['currency_for_other_country'];
            	$productCountryDetail = implode(",", array_keys($pstatus));
            	
                $this->sessionProductId[$this->sessionInstanceKey][$productKey] = array('productId'=>$productId, 'cultureProductId'=>$cultureProductId, 'productCalture'=>$productCountryDetail);
                $this->sessionManager->set('compare-product-id', serialize($this->sessionProductId));
                $this->sessionProductId = unserialize($this->sessionManager->get('compare-product-id'));

                unset($productId);
                unset($productKey);
                unset($cultureProductId);

                return 'SUCCESS';
            }
        }
    }
    
    public function getTotalCount()
    {
        /* if (!$this->totalProduct) {
            $this->totalProduct = count($this->sessionProductId[$this->sessionInstanceKey]);
        } */
    	if (is_array($this->sessionProductId[$this->sessionInstanceKey]) && count($this->sessionProductId[$this->sessionInstanceKey]) > 0) {
    		foreach ($this->sessionProductId[$this->sessionInstanceKey] as $productInfo) {
    			$pCalture=explode(',',$productInfo['productCalture']);
        		if(in_array($this->currentCulCode,$pCalture)){
    				$totalProducts[] = $productInfo['productId'];
    			}
    		}
    		unset($productInfo);
    		unset($sessionProductId);
    	}
    	
    	if (!$this->totalProduct) {
    		$this->totalProduct = count($totalProducts);
    	}
        return $this->totalProduct;
    }
    
    public function getCompareProduct()
    {
        $this->totalProduct = $this->getTotalCount();
        
        if ($this->totalProduct) {
            $this->processPrepareCompareProductDetail();
            return $this->compareProducts;
        } else {
            return null;
        }
    }
    
    public function processPrepareCompareProductDetail()
    {
        $this->compareProducts = array();
        $this->processPrepareCompareProductId();
        if (is_array($this->productIdArray) && count($this->productIdArray)) {
            $this->compareProducts = $this->getProductDetailById(json_encode($this->productIdArray), $this->cultureCode);
        }
        unset($productIds);
    }

    public function processPrepareCompareProductId()
    {
        $compareProductDetails = $this->prepareCompareProductId();
        if (is_array($compareProductDetails) && count($compareProductDetails)) {
            foreach ($compareProductDetails as $ids) {
            	$pCalture=explode(',',$ids['productCalture']);
            	if(in_array($this->currentCulCode,$pCalture)){
	                $this->productIdArray[] = $ids['productId'];
	                $this->cultureProductIdArray[] = $ids['id'];
            	}
            }
            unset($compareProductDetails);
        }
    }
    
    public function prepareCompareProductId()
    {
        if (is_array($this->sessionProductId[$this->sessionInstanceKey]) && count($this->sessionProductId[$this->sessionInstanceKey]) > 0) {
            foreach ($this->sessionProductId[$this->sessionInstanceKey] as $productInfo) {
            	$pCalture=explode(',',$productInfo['productCalture']);
            	if(in_array($this->currentCulCode,$pCalture)){
	                $this->productIdArray[] = $productInfo['productId'];
	                $this->cultureProductIdArray[] = $productInfo['cultureProductId'];
            	}
            }
            unset($productInfo);
            unset($sessionProductId);
        }
    }
    
    public function deleteCompareProduct($productId, $cultureProductId)
    {
        if (is_array($this->sessionProductId[$this->sessionInstanceKey]) && count($this->sessionProductId[$this->sessionInstanceKey])) {
            $productKey = $productId.':'.$productId;
			//$productKey = $productId.':'.$cultureProductId;
            if (array_key_exists($productKey, $this->sessionProductId[$this->sessionInstanceKey])) {
            	unset($this->sessionProductId[$this->sessionInstanceKey][$productKey]);
                $this->sessionManager->set('compare-product-id', serialize($this->sessionProductId));
            }
            
            return 'SUCCESS';
        }
    }
    
    public function deleteAllCompareProduct()
    {
        /* if (is_array($this->sessionProductId[$this->sessionInstanceKey]) && count($this->sessionProductId[$this->sessionInstanceKey])) {
            $this->sessionProductId[$this->sessionInstanceKey] = array();
            $this->sessionManager->set('compare-product-id', serialize($this->sessionProductId));
            return 'SUCCESS';
        } */
    	if (is_array($this->sessionProductId[$this->sessionInstanceKey]) && count($this->sessionProductId[$this->sessionInstanceKey]) > 0) {
    		foreach ($this->sessionProductId[$this->sessionInstanceKey] as $productInfo) {
    			$pCalture = explode(',',$productInfo['productCalture']);
    			if(in_array($this->currentCulCode,$pCalture)){
    				$quickQuoteKey = $productInfo['productId'].':'.$productInfo['cultureProductId'];
    				unset($this->sessionProductId[$this->sessionInstanceKey][$quickQuoteKey]);
    				$this->sessionManager->set('compare-product-id', serialize($this->sessionProductId));
    			}
    		}
    		return 'SUCCESS';
    	}
    }

    public function checkProductExistInCompare($productId, $cultureProductId)
    {

        $productKey = $productId.':'.$cultureProductId;
            if (array_key_exists($productKey, $this->sessionProductId[$this->sessionInstanceKey])) {
                return true;
            }else{
                return false;
            }

    }
}
