<?php
namespace OfficeBrain\Bundle\QuickQuoteBundle\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 QuickQuoteAbstract
{
    protected $entityManager;
    protected $sessionManager;
    protected $request;
    protected $productDetailManager;
    protected $projectSetting;
    protected $instanceId;
    protected $instanceType;
    protected $userId;
    protected $cultureId;
    protected $cultureCode;
    protected $configurationRule;
    protected $inlineViewProductMaxLimit;
    protected $createdUid;
    protected $upatedUid;
    protected $totalProduct;
    protected $sessionProductId;
    protected $sessionInstanceKey;
    protected $quickQuoteProducts;
	protected $emailServiceManager;
	protected $twigManager;
    
    /**
     * @author Employee Id: 4203
     * OB eCommerce Product - Requestinfo
     * Initialize the required variable on class load
     */
    public function __construct($entityManager, $request, $sessionManager, $productDetailManager, $emailServiceManager, $twigManager)
    {
        $this->entityManager = $entityManager;
        $this->request = $request;
        $this->sessionManager = $sessionManager;
        $this->productDetailManager = $productDetailManager;
		$this->emailServiceManager = $emailServiceManager;
		$this->twigManager = $twigManager;

        $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->quickQuoteConfig = $this->projectSetting['configuration_rule']['transaction']['quick_quote'];
        $this->quoteProductMaxLimit = $this->quickQuoteConfig['inline_view']['option']['no_of_product'];
        $this->createdUid = $this->userId;
        $this->upatedUid =$this->userId;
        $this->totalProduct = null;

        $this->wishListProducts = array();
        $this->sessionProductId = array();
        $this->sessionInstanceKey = $this->instanceId.'-'.$this->cultureId;
        
        if ($this->sessionManager->has('quick-quote-id')) {
            $this->sessionProductId = unserialize($this->sessionManager->get('quick-quote-id'));
        }
        
        if (!array_key_exists($this->sessionInstanceKey, $this->sessionProductId)) {
            $this->sessionProductId[$this->sessionInstanceKey] = array();
        }
        
        $this->entityManager->getRepository('OfficeBrainQuoteBundle:PreAddToQuote')->setProjectSetting($this->projectSetting);
        $this->continueShoppingUrlSessionKey = 'quick-quote-continue-shopping-url-'.$this->projectSetting['culture_code'];
    }
    
    public function __destruct()
    {
        unset($this->entityManager);
        unset($this->request);
        unset($this->projectSetting);
        unset($this->instanceId);
        unset($this->instanceType);
        unset($this->userId);
        unset($this->cultureId);
        unset($this->cultureCode);
        unset($this->quickQuoteConfig);
        unset($this->quoteProductMaxLimit);
        unset($this->createdUid);
        unset($this->upatedUid);
		unset($this->emailServiceManager);
		unset($this->twigManager);
    }
    
	public function sendMailQuickQuote($formData){
		
		$toEmail = $formData->getToEmail();
		$fromEmail = $formData->getFromEmail();
		$message = $formData->getMessage();
		$senderName = $formData->getFromName();
		$toName = $formData->gettoName();
		$recordSet = $this->getQuickQuote();
		
        $totalProduct = $this->getTotalCount();
		
		$quickQuoteHTML = $this->twigManager->render ( 'OfficeBrainQuickQuoteBundle:QuickQuote:emailQuickQuote.html.twig',
				array( "items"=>$recordSet,
                        "totalRecoards" => $totalProduct,
						"toName" => $toName,
						"message" => $message
                )
		);
		
		$templateParams = array(
				'mail_template' => $quickQuoteHTML,
				'message' => $message,
		);
		$eventKey = 'send_quick_quote_mail';
		return $message = $this->emailServiceManager->sendMail($eventKey, $toEmail, $templateParams, $culture=null, $fromEmail);
	}
	
    public function getProductDetailBySlug($productSlug, $culture)
    {
        return $this->productDetailManager->getProductDetailBySlug($productSlug, $culture);
    }

    public function getProductDetailById($productId, $culture)
    {
        return $this->productDetailManager->getProductDetailById($productId, $culture);
    }

    public function getQuickQuote()
    {
    }
    public function getQuickQuoteList()
    {
    }
    public function deleteQuickQuote($productId, $cultureProductId)
    {
    }
    public function deleteAllQuickQuote()
    {
    }
    public function setQuickQuoteInQueue()
    {
    }
    public function isMaxCountLimitQuickQuote()
    {
    }
    public function processSendEmailToFriend()
    {
    }
    public function processSaveAsPdf()
    {
    }
    
    public function prepareQuickQuoteDetail()
    {
    }
    public function prepareQuickQuoteId()
    {
    }
    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->quickQuoteConfig['guest_user']['option']['continue_shopping_default_url'];
    		}
    		else {
    			return $this->quickQuoteConfig['registered_user']['option']['continue_shopping_default_url'];
    		}
    	}
    }
    
    public function setContinueShoppingUrl($refererUrlRouteName, $refererUrl)
    {
    	if ($refererUrl != '' && $refererUrlRouteName != 'office_brain_quickquotebundle_quickquote_quickquote') {
    		$this->sessionManager->set($this->continueShoppingUrlSessionKey, $refererUrl);
    	} else {
    		$this->sessionManager->remove($this->continueShoppingUrlSessionKey);
    	}
    }
}
