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

use OfficeBrain\Bundle\QuickQuoteBundle\Manager\AbstractManager\QuickQuoteAbstract;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class QuickQuoteRegisteredExtended extends QuickQuoteAbstract
{
	protected $preQuoteRepositoryManager;
	
    public function __construct($entityManager, $request, $sessionManager, $productDetailManager, $emailServiceManager, $twigManager)
    {
       parent::__construct($entityManager, $request, $sessionManager, $productDetailManager, $emailServiceManager, $twigManager);
       $this->preQuoteRepositoryManager = $this->entityManager->getRepository('OfficeBrainQuoteBundle:PreAddToQuote');
       $this->currentCulCode = substr($this->cultureCode, strpos($this->cultureCode, "_") + 1);
    }
    
    public function __destruct()
    {
        parent::__destruct();
        unset($this->currentCulCode);
    }
    
    public function setQuickQuote()
    {
    	$productId = $this->request->get('product_id');
    	$productSlug = $this->request->get('product_slug');
    	$cultureProductId = $this->request->get('culture_product_id');
        $this->totalProduct = $this->getTotalCount();
        
        $pDetail = $this->productDetailManager->getProductDetailById($productId,$this->cultureCode);
        $pstatus = $pDetail[$productId]['currency_for_other_country'];
        $productCountryDetail = implode(",", array_keys($pstatus));
        
        //if ($this->totalProduct >= $this->quoteProductMaxLimit) {
        //    return 'ERROR_MAX_LIMIT_REACHED';
       // } else {
            $isQuoteExists = $this->preQuoteRepositoryManager->isQuoteExists($productId, $cultureProductId,$this->currentCulCode);
        	if ($isQuoteExists) {
        		return 'ERROR_PRODUCT_ALREADY_EXISTS';
        	} else {
        		$this->preQuoteRepositoryManager->addToQuote($productId, $cultureProductId, $productSlug, $productCountryDetail);
        	}
        	return 'SUCCESS';
      //  }
    }

    public function getTotalCount()
    {
		$this->totalProduct = $this->preQuoteRepositoryManager->getTotalCountUserWise($this->currentCulCode);
		return $this->totalProduct;
    }

    public function getQuickQuote()
    {
    	$this->quickQuotes = null;
        $this->totalProduct = $this->getTotalCount();

        if ($this->totalProduct) {
        	$this->processPrepareQuickQuoteId();
            $this->processPrepareQuickQuoteDetail();
            return $this->quickQuotes;
        } else {
            return null;
        }
    }

    public function processPrepareQuickQuoteDetail()
    {
        $this->quickQuotes = array();
         
        if (is_array($this->productIdArray) && count($this->productIdArray)) {
            $productIds = json_encode($this->productIdArray);
            $this->quickQuotes = $this->getProductDetailById(json_encode($this->productIdArray), $this->cultureCode);
        }
        unset($productIds);
    }
    
    public function processPrepareQuickQuoteId()
    {
    	$this->productIdArray = array();
    	$this->cultureProductIdArray = array();
        $recordSet = $this->preQuoteRepositoryManager->getQuickQuote($this->currentCulCode);
        if (is_array($recordSet) && count($recordSet)) {
            foreach ($recordSet as $row) {
                $this->productIdArray[] = $row['productId'];
                $this->cultureProductIdArray[] = $row['productCultureId'];
            }
            unset($recordSet);
        }
    }
	
    public function getMaxNoOfQuickQuote($maxNo)
    {
    	$this->totalProduct = $this->getTotalCount();
    	
    	if ($this->totalProduct) {
    		$this->getMaxNoOfQuickQuoteId($maxNo);
    		$this->processPrepareQuickQuoteDetail();
    		return $this->quickQuotes;
    	} else {
    		return null;
    	}
    }
    
    public function getMaxNoOfQuickQuoteId($maxNo)
    {
    	$this->productIdArray = array();
    	$this->cultureProductIdArray = array();
    	$recordSet = $this->preQuoteRepositoryManager->getMaxNoOfProduct($maxNo,$this->currentCulCode);
    	if (is_array($recordSet) && count($recordSet)) {
    		foreach ($recordSet as $row) {
    			$this->productIdArray[] = $row['productId'];
    			$this->cultureProductIdArray[] = $row['productCultureId'];
    		}
    		unset($recordSet);
    	}
    }
    
    
    public function deleteQuickQuote($productId, $cultureProductId)
    {
        $isDeleted = $this->preQuoteRepositoryManager->deleteQuickQuote($productId, $cultureProductId);
        
        if ($isDeleted) {
            $this->totalProduct = null;
            return 'SUCCESS';
        }
        return 'ERROR_NOT_FOUND';
    }
    
    public function deleteAllQuickQuote()
    {
        $isDeleted = $this->preQuoteRepositoryManager->deleteAllQuickQuote($this->currentCulCode);

        if ($isDeleted) {
            $this->totalProduct = null;
            return 'SUCCESS';
        }
        return 'ERROR_NOT_FOUND';
    }
}
