<?php
namespace OfficeBrain\CustomBundle\OrderBundle\Manager\Extended;
use OfficeBrain\Bundle\OrderBundle\Manager\AbstractManager\OrderAbstract;
use OfficeBrain\Bundle\OrderBundle\Manager\Extended\OrderGuestExtended as BaseOrderGuestExtended;

class OrderGuestExtended extends BaseOrderGuestExtended
{
	/**
	 * @author Employee Id: 5497
	 * OB eCommerce Product - Order
	 * Initialize the required variable on class load
	 */
	public function __construct($entityManager, $request, $sessionManager, $productDetailManager, $userDetailManager, $emailManager, $twigManager,$userContactBookManager, $commonLibraryManager, $coreManager,$productApi, $serviceRuleApi,$paginatorManager,$xmlPath,$jmsSerializer, $twigExtension, $promoCoupon, $couponUsers){
		parent::__construct($entityManager, $request, $sessionManager, $productDetailManager, $userDetailManager, $emailManager, $twigManager,$userContactBookManager, $commonLibraryManager, $coreManager,$productApi, $serviceRuleApi,$paginatorManager,$xmlPath,$jmsSerializer, $twigExtension);
		$this->currentCulCode = substr($this->cultureCode, strpos($this->cultureCode, "_") + 1);

		$this->orderMasterRepositoryManager->setCoreManager($this->coreManager);
	}
	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;
	}

	/**
	 * @author Employee Id: 4248
	 * OB eCommerce Product - Order
	 * To set the record into the order tables
	 */
	public function addOrderDetail()
	{   
		$order_no = $this->orderMasterRepositoryManager->getLastInsertedOrder();
		$status=array();
		$data=$this->sessionManager->get('paymentDetail');
		if($data>0){
			$contactBookId=$data['billing_id'];
			$billingAddress = $this->userContactBookManager->getSingleContactBook($contactBookId);
			$userRepository=$this->entityManager->getRepository('OfficeBrainUserBundle:User');
			$userdetails=$userRepository->getGuestByEmail($billingAddress->getemail());
			$this->userId=0;
			if($userdetails!=NULL){
				$this->userId = $userdetails->getId();
			}
			if($this->userId>0){
				$cartData = $this->getCartList();
				$this->totalProduct = $this->getTotalCount();
				$userObj = $this->getUserDetail($this->userId, 'register');
				if ($this->totalProduct > 0)
				{
					
					$productDetailArray = $this->cart;
					if($userObj->getUserType()=="distributor"){
						$this->productDataArray['pref_dist']=$this->siteOwnerId;
					}else{
						$this->productDataArray['pref_dist']=$userObj->getLocatedDistributorId();
					}
					$this->productDataArray['customerNumber']=$data['customer_number'];
					$this->productDataArray['poNumber']=$data['po_number'];
					$this->productDataArray['transactionId']=$data['transaction_id'];
					$this->productDataArray['paymentAmount']=$data['amount'];
					$this->productDataArray['tax_total']=$data['tax_total'];
					$this->productDataArray['paymentmethod']=$data['method'];
					$this->productDataArray['guest_shipping_id']=$data['guest_shipping_id'];
					$this->productDataArray['productData']=$cartData;
					$this->productDataArray['order_no']=$order_no;
					$this->orderMasterRepositoryManager->setBillingAddress($billingAddress);
					$this->orderMasterRepositoryManager->setErpId($this->request->get('erp_id',NULL));
					
					$status= $this->orderMasterRepositoryManager->setGuestOrderData($productDetailArray,$this->productDataArray,$this->userId);
				}
				if(isset($status['data']) && count($status['data']) > 0 && $status['data']->getId()>0){
					
					//here you add email code for order email
					$toEmail = $billingAddress->getemail();
					$data['order_no']=$order_no;
					$data['order'] = $this->isOrderExists($status['data']->getId());
					$data['data'] = $this->getProductList($status['data']->getId());
					$data['sizeAllowed']= $this->checkSizeAllow($data['data'][0]['category']);
					$data['instance_type']= $this->instanceType;
					$data['confi'] = $this->configurationRule['order_tax'];
					$data['user'] = $userObj;
					$data['productData']=$cartData;
					
					/* Retrive the productDetailConfiguration */
					if(isset($data['order']))
					{
						$orderItemLines = $data['order']->getOrderItemLine();
						foreach($orderItemLines as $orderItemLineIndex => $orderItemLineData)
						{
							$encodedProductDetailConfiguration = $orderItemLineData->getProductDetailConfiguration();
							$decodedProductDetailConfiguration = base64_decode($encodedProductDetailConfiguration);
							$serizliaedProductDetailConfiguration = $decodedProductDetailConfiguration;
							$unSerizliaedProductDetailConfiguration = unserialize($serizliaedProductDetailConfiguration);
							$data['order']->getOrderItemLine()[$orderItemLineIndex]->setProductDetailConfiguration($unSerizliaedProductDetailConfiguration);
						}
					}
					$orderDetailHTML = $this->twigManager->render ( 'OfficeBrainOrderBundle:Order:orderEmail.html.twig', $data);
					$templateParams = array('order_mail_template' => $orderDetailHTML);
					$eventKey = 'order_email';
					$message = $this->emailManager->sendMail($eventKey, $toEmail, $templateParams);//, $culture=null, $fromEmail);
		
					$this->sessionManager->remove('paymentDetail');
					$this->deleteAllCartList();
					$status['order_id']=$data['order']->getId();
					return $status;
				}else{
					return $status['error']='error';
				}
			}
		}else{
			 return $status['error']='error';
			}
	}


}