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

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Translation\Translator;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\JsonResponse;

abstract class CompareProductAbstract
{
    protected $entityManager;
    protected $session;
    protected $request;
    protected $productDetailService;
    protected $projectSetting;
    protected $instanceId;
    protected $instanceType;
    protected $userId;
    protected $cultureId;
    protected $cultureCode;
    protected $compareProductConfig;
    protected $comapreProductMaxLimit;
    protected $createdUid;
    protected $upatedUid;
    protected $totalProduct;
    protected $sessionProductId;
    protected $sessionInstanceKey;
    protected $emailManager;
    protected $commonLibraryManager;
    protected $twig;
    
    /**
     * @author Employee Id: 4203
     * OB eCommerce Product - Requestinfo
     * Initialize the required variable on class load
     */
    public function __construct($entityManager, $request, $sessionManager, $productDetailManager, $emailManager, $twig, $commonLibraryManager, $twigExtension)
    {
        $this->entityManager = $entityManager;
        $this->request = $request;
        $this->sessionManager = $sessionManager;
        $this->productDetailManager = $productDetailManager;
        $this->emailManager = $emailManager;
        $this->commonLibraryManager = $commonLibraryManager;
        $this->twig = $twig;
        $this->twigExtension = $twigExtension;
        
        $this->projectSetting = $this->request->get('project_setting');
        $this->instanceId = $this->projectSetting['instance_id'];
        $this->instanceType = $this->projectSetting['instance_type'];
        $this->userId = $this->projectSetting['user_id'];
        $this->cultureId = $this->projectSetting['culture_id'];
        $this->cultureCode = $this->projectSetting['culture_code'];
        $this->compareProductConfig = $this->projectSetting['configuration_rule']['transaction']['compare_product'];
        $this->comapreProductMaxLimit = $this->compareProductConfig['inline_view']['option']['no_of_product'];
        $this->createdUid = $this->userId;
        $this->upatedUid = $this->userId;
        $this->totalProduct = null;
        
        $this->sessionProductId = array();
        $this->sessionInstanceKey = $this->instanceId.'-'.$this->cultureId;
        
        if ($this->sessionManager->has('compare-product-id')) {
            $this->sessionProductId = unserialize($this->sessionManager->get('compare-product-id'));
        }
         
        if (!array_key_exists($this->sessionInstanceKey, $this->sessionProductId)) {
            $this->sessionProductId[$this->sessionInstanceKey] = array();
        }
        
        $this->compareProductRepositoryManager = $this->entityManager->getRepository('OfficeBrainCompareProductBundle:CompareProduct');
        $this->compareProductRepositoryManager->setProjectSetting($this->projectSetting);
        $this->continueShoppingUrlSessionKey = 'compare-product-continue-shopping-url-'.$this->projectSetting['culture_code'];
       
    }
    
    public function __destruct()
    {
        unset($this->entityManager);
        unset($this->request);
        unset($this->sessionManager);
        unset($this->productDetailManager);
        unset($this->projectSetting);
        unset($this->instanceId);
        unset($this->instanceType);
        unset($this->userId);
        unset($this->cultureId);
        unset($this->cultureCode);
        unset($this->compareProductConfig);
        unset($this->comapreProductMaxLimit);
        unset($this->createdUid);
        unset($this->upatedUid);
        unset($this->totalProduct);
        unset($this->sessionProductId);
        unset($this->sessionInstanceKey);
        unset($this->compareProductRepositoryManager);
        unset($this->commonLibraryManager);
        unset($this->emailManager);
        unset($this->twig);
    }
    
    public function getProductDetailBySlug($productSlug, $culture)
    {
        return $this->productDetailManager->getProductDetailBySlug($productSlug,  $this->cultureCode);
    }

    public function getProductDetailById($productId, $culture)
    {
        return $this->productDetailManager->getProductDetailById($productId, $this->cultureCode);
    }
    
    public function setCompareProduct($productId, $cultureProductId)
    {
    }
    public function getCompareProduct()
    {
    }
    public function getCompareProductList()
    {
    }
    public function deleteCompareProduct($productId, $cultureProductId)
    {
    }
    public function deleteAllCompareProduct()
    {
    }
    public function setCompareProductInQueue()
    {
    }
    public function isMaxCountLimitCompareProduct()
    {
    }
    public function processSendEmailToFriend()
    {
    }
    public function processSaveAsPdf()
    {
    }
    
    public function prepareCompareProductDetail()
    {
    }
    public function prepareCompareProductId()
    {
    }
    public function getGropPricing($products)
    {
    }
    
    public function getContinueShoppingUrl()
    {
    	if ($this->sessionManager->has($this->continueShoppingUrlSessionKey)) {
    		$redirectUrl = $this->sessionManager->get($this->continueShoppingUrlSessionKey);
    		$this->sessionManager->remove($this->continueShoppingUrlSessionKey);
    		return $redirectUrl;
    	}
    	else {
    		if($this->userId) {
    			return $this->compareProductConfig['guest_user']['option']['continue_shopping_default_url'];
    		}
    		else {
    			return $this->compareProductConfig['registered_user']['option']['continue_shopping_default_url'];
    		}
    	}
    }
    
    public function setContinueShoppingUrl($refererUrlRouteName, $refererUrl)
    {
    	if ($refererUrl != '' && $refererUrlRouteName != 'office_brain_compareproductbundle_compareproduct_compareproduct') {
    		$this->sessionManager->set($this->continueShoppingUrlSessionKey, $refererUrl);
    	} else {
    		$this->sessionManager->remove($this->continueShoppingUrlSessionKey);
    	}
    }
    
    public function getProductPriceByQty($productId, $cultureId, $qty)
    {
    	$recordSet = $this->getProductDetailById($productId, $cultureId);
    	$regularVariationsCharges = $recordSet[$productId]['pricing'];
    	
    	if(count($this->twigExtension) > 0) {
    		return $this->twigExtension->priceFilter($this->commonLibraryManager->getProductPriceByQty($regularVariationsCharges, $qty));
    	}
    	else {
    		return $this->commonLibraryManager->getProductPriceByQty($regularVariationsCharges, $qty);
    	}
    }
}
