<?php
namespace OfficeBrain\Bundle\QuoteBundle\Manager\Extended;
use OfficeBrain\Bundle\QuoteBundle\Manager\AbstractManager\QuoteAbstract;
class QuoteRegisteredExtended extends QuoteAbstract
{
	/**
	 * @author Employee Id: 4203
	 * OB eCommerce Product - Quote
	 * Initialize the required variable on class load
	 */
	public function __construct($entityManager, $request, $session, $productDetailManager,$userDetailManager, $emailManager, $twigManager,$paginatorManager, $coreManager,$contactBookManager, $routerManager, $translatorManager, $productApi,$userContactBookManager, $serviceRuleApi)
	{
		parent::__construct($entityManager, $request, $session,  $productDetailManager,$userDetailManager, $emailManager, $twigManager,$paginatorManager, $coreManager,$contactBookManager, $routerManager, $translatorManager, $productApi, $userContactBookManager, $serviceRuleApi);
	}

	public function __destruct()
	{
		parent::__destruct();
	}

	/**
	 * @author Employee Id: 4217
	 * OB eCommerce Product - Quote
	 * Function created to override the add To Quote product functionality defined in abstract class
	 */
	public function setQuote($productId, $cultureProductId)
	{
		$isQuoteExists = $this->quoteRepositoryManager->isQuoteExists($productId, $cultureProductId);
		if ($isQuoteExists) {
			return 'ERROR_PRODUCT_ALREADY_EXISTS';
		} else {
			$this->quoteRepositoryManager->addToQuote($productId, $cultureProductId);
		}
		return 'SUCCESS';

	}

	public function getQuoteDetailById()
	{
		return $this->quoteRepositoryManager->getQuoteDetailById($this->request->get('quote-id'));
	}


	/**
	 * @author Employee Id: 4217
	 * OB eCommerce Product - Quote
	 * To get all register user Quote list
	 * @param null
	 * return array object of  Quote
	 */
	public function getQuoteAdminList($page,$recordsPerPage,$requestStatus,$searchParametersArray,$sortingParameterArray,$searchKeyWordsArray)
	{
		$adminUser = $this->userManager->findIsAdmin();
		$flag = 'user';
		if ($adminUser) {
			$flag = 'admin';
		}
		return $this->quoteRepositoryManager->getQuoteAdminList($page,$recordsPerPage,$requestStatus,$searchParametersArray,$sortingParameterArray,$searchKeyWordsArray, $flag);

	}


	/**
	 * @author Employee Id: 4217
	 * OB eCommerce Product - Quote
	 * To get all register user Quote list
	 * @param null
	 * return array object of  Quote
	 */
	public function getAllTabCount()
	{
		$tab =array();
		$adminUser = $this->userManager->findIsAdmin();
		$flag = 'user';
		if ($adminUser) {
			$flag = 'admin';
		}
		$tab['draft'] = $this->quoteRepositoryManager->getQuoteCountList('draft', $flag);
		$tab['sent'] = $this->quoteRepositoryManager->getQuoteCountList('sent', $flag);
		$tab['approved'] = $this->quoteRepositoryManager->getQuoteCountList('approved', $flag);
		$tab['rejected'] = $this->quoteRepositoryManager->getQuoteCountList('rejected', $flag);
		$tab['change'] = $this->quoteRepositoryManager->getQuoteCountList('change', $flag);
		$tab['history'] = $this->quoteRepositoryManager->getQuoteCountList('history', $flag);
		return $tab;
	}


	/**
	 * @author Employee Id: 4217
	 * OB eCommerce Product - Quote
	 * To get all register user Quote list
	 * @param null
	 * return array object of  Quote
	 */
	public function getQuoteList($page,$recordsPerPage,$requestStatus,$searchParametersArray,$sortingParameterArray,$searchKeyWordsArray)
	{
		return $this->entityManager->getRepository('OfficeBrainQuoteBundle:QuoteMaster')->getQuoteList($page,$recordsPerPage,$requestStatus,$searchParametersArray,$sortingParameterArray,$searchKeyWordsArray);

	}

	/**
	 * @author Employee Id: 4532
	 * OB eCommerce Product - Quote
	 * To get Address From ContactBook/AddressBook with searching term
	 */
	public function getUserShippingAddresByEmailOrName()
	{
		$shippingFrom = $this->request->get('shipping_from');
		$term = $this->request->get('term');
		$searchIn = array('email'=>$term,'name'=>$term);

		if($shippingFrom == 'shipping_book'){
			$Address = $this->getUserShippingAddressFromShippingBook($searchIn,false);
		}else if($shippingFrom == 'contact_book'){
			$Address = $this->getUserShippingAddressFromContactBook($searchIn,false);
		}
		return $Address;
	}

	/**
	 * @author Employee Id: 4532
	 * OB eCommerce Product - Quote
	 * To get All the shipping Address from Address Book of user
	 */
	public function getUserShippingAddressFromShippingBook($searchIn=array(), $isAllField = true)
	{
		$userId = $this->projectSetting['user_id'];
		return $this->userContactBookManager->getAllAddressBook($userId, $this->shippingAddressType, $searchIn, $isAllField);
	}

	/**
	 * @author Employee Id: 4532
	 * OB eCommerce Product - Quote
	 * To get All the shipping Address from ContactBook of user
	 */
	public function getUserShippingAddressFromContactBook($searchIn=array(), $isAllField= true)
	{
		$userId = $this->projectSetting['user_id'];
		return $this->userContactBookManager->getAllContactBook($userId, $this->shippingAddressType, $searchIn, $isAllField);
	}

	/**
	 * @author Employee Id: 4532
	 * OB eCommerce Product - Quote
	 * To get Address From ContactBook/AddressBook By Id
	 */
	public function getUserShippingAddresById()
	{
		$contactBookId = $this->request->get('contact_id',null);
		$counter = $this->request->get('counter',0);
		$Address = null; $otherData = null;
		if($contactBookId != null && $contactBookId > 0){
			$Address = $this->userContactBookManager->getSingleContactBook($contactBookId);

			$otherData['cityName'] = $this->coreManager->getCityName($Address->getCityId());
			$otherData['stateName'] = $this->coreManager->getStateName($Address->getStateId());
			$otherData['countryName'] = $this->coreManager->getCountryName($Address->getCountryId());
			$otherData['counter'] = $counter;
		}

		return array('address_data'=>$Address,'other_data'=>$otherData);
	}


	/**
	 * @author Employee Id: 4532
	 * OB eCommerce Product - Quote
	 * To get
	 */
	public function getBlankColorQuantityValue($productAttributes)
	{
		$productAttributes['config']['shipping_type'] = $this->request->get('shipping_type');
		$colorId = $productAttributes['attribute_value_id'];
		$qty = array();
		if($productAttributes['config']['allow_size'] && isset($productAttributes['attributes'][$productAttributes['config']['size_attribute_id']])){
			foreach ( $productAttributes['attributes'][$productAttributes['config']['size_attribute_id']]['values'] as $sizeId=>$size){
				$qty['quantity'][] = 0;
				$qty['size'][] = $sizeId;
			}
		}else{
			$qty['quantity'][] = 0;
		}

		return array('config'=>$productAttributes['config'],'Qty'=> $qty, 'colorId' => $colorId,'product_attributes'=>$productAttributes['attributes']);

	}
	
	private function checkInArray($item , $array){
	    return preg_match('/'.$item.'/i' , json_encode($array));
	}
	
	public function checkForAttribute($colorAttr){
		$attrIds = $this->productDetailManager->getAttributeIds();
		$data = array();
		if($this->checkInArray($attrIds["trim_color_id"], $colorAttr)){
			$data['is_trim_color'] = 1; 
			$data['trim_color_id'] = $attrIds["trim_color_id"];
		}else{
			$data['is_trim_color'] = 0; 
			$data['trim_color_id'] = $attrIds["trim_color_id"];
		}
			
		if($this->checkInArray($attrIds["ink_color_id"], $colorAttr)){
			$data['is_ink_color'] = 1; 
			$data['ink_color_id'] = $attrIds["ink_color_id"];
			
		}else{
			$data['is_ink_color'] = 0; 
			$data['ink_color_id'] = $attrIds["ink_color_id"];
		}
			
		if($this->checkInArray($attrIds["barrel_color_id"], $colorAttr)){
			$data['is_barrel_color'] = 1; 
			$data['barrel_color_id'] = $attrIds["barrel_color_id"];
		}else{
			$data['is_barrel_color'] = 0; 
			$data['barrel_color_id'] = $attrIds["barrel_color_id"];
		}
		if($this->checkInArray($attrIds["std_color_id"], $colorAttr)){
			$data['is_standard_color'] = 1; 
			$data['std_color_id'] = $attrIds["std_color_id"];
		}else{
			$data['is_standard_color'] = 0; 
			$data['std_color_id'] = $attrIds["std_color_id"];
		}
		$data['color_attr_id'] = $attrIds['color_attr_id'];
		//$data['std_color_id'] = $attrIds['std_color_id'];
		return $data;
	}
	
	public function mergeAttributeArray($colrInfo,$attribute){
    	$result = array();
		$fullColorArray = array();
		$inkColorArray = array();
		$trimColorArray = array();
		$barrelColorArray = array();
		
		foreach($attribute as $key=>$val){
			if($val['id'] == $colrInfo['trim_color_id']){
				$trimColorArray = $val['values'];
			}
			if($val['id'] == $colrInfo['ink_color_id']){
				$inkColorArray = $val['values'];
			}
			if($val['id'] == $colrInfo['barrel_color_id']){
				$barrelColorArray = $val['values'];
			}
			if($val['id'] == 10){
				$fullColorArray = $val['values'];
			}
		}
		foreach ($fullColorArray as $k=>$v){
			$v2 = preg_quote($v, '/');
			if(preg_grep( "/^".$v2."$/i" , $trimColorArray )) {	
				$result['trim_color_val'][$k] = $v;  
			}
			if(preg_grep( "/^".$v2."$/i" , $inkColorArray )) {
				$result['ink_color_val'][$k] = $v;  
			}
			if(preg_grep( "/^".$v2."$/i" , $barrelColorArray )) {	
				$result['barrel_color_val'][$k] = $v;  
			}
		}
		
		return $result;
		
    }
	
	
}