<?php
namespace  OfficeBrain\Bundle\ShippingEstimatorBundle\Helper;
use OfficeBrain\Bundle\ShippingEstimatorBundle\Manager\AbstractManager\ShippingEstimatorAbstract;

class ShippingEstimatorHelper extends ShippingEstimatorAbstract
{
	protected $fedexManager;
	protected $upsManager;
	protected $loomisExpress;
	
	public function __construct($fedexManager,$upsManager,$loomisExpress,$coreManager){
		$this->fedexManager=$fedexManager;
		$this->upsManager=$upsManager;
		$this->loomisExpressManager= $loomisExpress;
		$this->coreManager=$coreManager;
	}
	
	public function __destruct()
	{
		unset($this->fedexManager);
		unset($this->upsManager);
		unset($this->loomisExpressManager);
		unset($this->coreManager);
	}
	
	public function initAction()
	{
		//$this->country = array('CA'=>'Canada','US'=>'USA');
	}
	
	public function getShippingEstimatorRate($data,$productId,$projectSetting,$callFrom)
	{
		$this->initAction();
		$this->userId = $projectSetting['user_id'];
		$result = array();
		$resultFedex = array();
		$resultUPS = array();
		$resultLoomis = array();
		$qty = $data['qty'];
		$userAdd = '';
		
		if(isset($data['city']))
		{
			$userCity = $this->coreManager->getCityName($data['city']);
		} 
		else 
		{
			$userCity = '';
		}
		if(isset($data['state']))
		{
			$userState = $this->coreManager->getStateCode($data['state']);
		}
		else
		{
			$userState = '';
		}

		$userZipcode = $data['zip_code'];
		$userCountry = $data['country']; 
		$shippingTypeArray = $data['shipping_type'];
		if(isset($data['is_resident']))
		{
			$userIsResiden= $data['is_resident'];
		}
		else
		{
			$userIsResiden = 0;
		}
		
		$userDetail=array('reciver'=>array('userAdd'=>$userAdd,'userCity'=>$userCity,'userState'=>$userState ,'userZip'=>$userZipcode, 'userCountry' =>$userCountry,'userPhone'=>'','userName'=>'','userIsResident'=>$userIsResiden));
		
		
		if(is_array($shippingTypeArray))
		{
			if($shippingTypeArray[0]=='fedex')
			{
				$shippingManager = $this->fedexManager;
			}
			elseif ($shippingTypeArray[0]=='ups')
			{
				$shippingManager = $this->upsManager;
			}
			elseif ($shippingTypeArray[0]=='loomisExpress')
			{
				$shippingManager = $this->loomisExpressManager;
			}
			$productDetail = $shippingManager->getProductDetailForShipping($productId,$qty);
		}
		else
		{
			if($shippingTypeArray == 'fedex')
			{
				$shippingManager = $this->fedexManager;
				$productDetail = $shippingManager->getProductDetailForShipping($productId,$qty);
				$shippingTypeArray= array('0'=>$shippingTypeArray);
			}
			elseif ($shippingTypeArray=='ups') {
				$shippingManager = $this->upsManager;
				$productDetail = $shippingManager->getProductDetailForShipping($productId,$qty);
				$shippingTypeArray= array('0'=>$shippingTypeArray);
			}
			elseif ($shippingTypeArray=='loomisExpress') {
				$shippingManager = $this->loomisExpressManager;
				$productDetail = $shippingManager->getProductDetailForShipping($productId,$qty);
				$shippingTypeArray= array('0'=>$shippingTypeArray);
			}
		}
		if($productDetail['status']== 'error')
		{
			$response['status'] = 'error';
			$response['error'] = $productDetail['error'];
		}
		else 
		{
			$productDetail=$productDetail['data'];
			$shipperDetail = array('shipper'=>array('shipperAdd'=>' ','shipperCity'=>$productDetail['city'],'shipperState'=>$productDetail['state'] ,'shipperZip'=>$productDetail['zipcode'], 'shipperCountry' =>$productDetail['country'],'shipperPhone'=>' ','shipperCompanyName'=>' ','shipperEmail'=>' ','shipperName'=>' '));
			$shippingEstiDetail=array_merge($productDetail,$userDetail,$shipperDetail) ;
			
			foreach($shippingTypeArray as $key=>$shippingMethod)
			{
				if($shippingMethod == 'fedex')
				{
					if($this->userId!='0')
						$shippinEstiConfifedex = $projectSetting['configuration_rule']['transaction']['shipping_estimator']['option']['registered_user']['option']['fedex']['option']['fedex_rate']['option'];
					else
						$shippinEstiConfifedex = $projectSetting['configuration_rule']['transaction']['shipping_estimator']['option']['guest_user']['option']['fedex']['option']['fedex_rate']['option'];
						
					$resultFedex = $this->fedexManager->callRequest($shippingEstiDetail,$shippinEstiConfifedex,$callFrom);
				}
				if($shippingMethod == 'ups')
				{
					if($this->userId!='0')
						$shippinEstiConfiUPS = $projectSetting['configuration_rule']['transaction']['shipping_estimator']['option']['registered_user']['option']['ups']['option']['ups_rate']['option'];
					else
						$shippinEstiConfiUPS = $projectSetting['configuration_rule']['transaction']['shipping_estimator']['option']['guest_user']['option']['ups']['option']['ups_rate']['option'];
						
					$resultUPS = $this->upsManager->callRequest($shippingEstiDetail,$shippinEstiConfiUPS,$callFrom);
				}
				
				if($shippingMethod == 'loomisExpress')
				{
					if($this->userId!='0')
						$shippinEstiConfiUPS = $projectSetting['configuration_rule']['transaction']['shipping_estimator']['option']['registered_user']['option']['loomis_express']['option']['loomis_express_rate']['option'];
					else
						$shippinEstiConfiUPS = $projectSetting['configuration_rule']['transaction']['shipping_estimator']['option']['guest_user']['option']['loomis_express']['option']['loomis_express_rate']['option'];
					
					$resultLoomis = $this->loomisExpressManager->callRequest($shippingEstiDetail,$shippinEstiConfiUPS,$callFrom);
				}
			}
			$result = array_merge($resultFedex,$resultUPS,$resultLoomis);
			
			$response['status'] = 'success';
			$response['productDetail']=$productDetail;
			$response['data'] = $result;
		}
		return $response;
	}
	
	public function getShippingEstimatorRateForMobileAPI($data,$productId,$projectSetting)
	{
		$this->initAction();
		$result ='';
		$qty = $data['qty'];
		$userAdd = '';
		$userCity = '';
		$userState = '';
		$userZipcode = $data['zip_code'];
		$userCountry = $data['country']; 
		$shippingTypeArray = $data['shipping_type'];
		$userIsResiden = $data['is_resident'];
		
		$userDetail=array('reciver'=>array('userAdd'=>$userAdd,'userCity'=>$userCity,'userState'=>$userState ,'userZip'=>$userZipcode, 'userCountry' =>$userCountry,'userPhone'=>'','userName'=>'','userIsResident'=>$userIsResiden));
		
		
		if(is_array($shippingTypeArray))
		{
			if($shippingTypeArray[0]=='fedex')
			{
				$shippingManager = $this->fedexManager;
			}
			elseif ($shippingTypeArray[0]=='ups')
			{
				$shippingManager = $this->upsManager;
			}
			$productDetail = $shippingManager->getProductDetailForShipping($productId,$qty);
		}
		else
		{
			$shippingManager = $this->fedexManager;
			$productDetail = $shippingManager->getProductDetailForShipping($productId,$qty);
		}
		if($productDetail['status']=='error')
		{
			$response['status'] = 'error';
			$response['error'] = $productDetail['error'];
		}
		else 
		{
			$productDetail=$productDetail['data'];
			$shipperDetail = array('shipper'=>array('shipperAdd'=>' ','shipperCity'=>' ','shipperState'=>$productDetail['state'] ,'shipperZip'=>$productDetail['zipcode'], 'shipperCountry' =>$productDetail['country'],'shipperPhone'=>' ','shipperCompanyName'=>' ','shipperEmail'=>' ','shipperName'=>' '));
			$shippingEstiDetail=array_merge($productDetail,$userDetail,$shipperDetail) ;
			
			foreach($shippingTypeArray as $key=>$shippingMethod)
			{
				if($shippingMethod == 'fedex')
				{
					if($this->userId!='0')
						$shippinEstiConfiFedex = $projectSetting['configuration_rule']['transaction']['shipping_estimator']['option']['registered_user']['option']['fedex']['option']['fedex_rate']['option'];
					else 
						$shippinEstiConfiFedex = $projectSetting['configuration_rule']['transaction']['shipping_estimator']['option']['guest_user']['option']['fedex']['option']['fedex_rate']['option'];
					$result[$shippingMethod] = $this->fedexManager->callRequestMobileApi($shippingEstiDetail,$shippinEstiConfiFedex);
				}
				if($shippingMethod == 'ups')
				{
					if($this->userId!='0')
						$shippinEstiConfiUps = $projectSetting['configuration_rule']['transaction']['shipping_estimator']['option']['registered_user']['option']['ups']['option']['ups_rate']['option'];
					else 
						$shippinEstiConfiUps = $projectSetting['configuration_rule']['transaction']['shipping_estimator']['option']['guest_user']['option']['ups']['option']['ups_rate']['option'];
					$result[$shippingMethod] = $this->upsManager->callRequestMobileApi($shippingEstiDetail,$shippinEstiConfiUps);
				}
			}
			$response['status'] = 'success';
			$response['data'] = $result;
		}
		return $response;
	}
}