<?php
namespace OfficeBrain\Bundle\ShippingEstimatorBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use OfficeBrain\Bundle\ShippingEstimatorBundle\Form\ShippingEstimatiorType as ShippingEstimatiorType;
use Symfony\Component\HttpFoundation\JsonResponse;

class UPSController extends Controller
{
	/**
	 *
	 * @author Employee Id: 4471
	 *
	 * To set member variables default value
	 *
	 * @return NULL
	 */
	public function __construct()
	{
		$this->userId = null;
		$this->request = null;
		$this->refererUrl = null;
		$this->responseArray = array();
		$this->commonLibrary = null;
	}
	
	/**
	 *
	 * @author Employee Id: 4471
	 *
	 *         To unset member variables
	 *
	 * @return NULL
	 */
	public function __destruct()
	{
		unset($this->userId);
		unset($this->request);
		unset($this->refererUrl);
		unset($this->commonLibrary);
		unset($this->responseArray);
	}
	
	/**
	 *
	 * @author Employee Id: 4471
	 *
	 * Create various objects required action in this class
	 *
	 * @return NULL
	 */
	public function initAction()
	{
		$this->request = $this->getRequest();
		$this->productId = $this->request->get('productId', null);
		$this->cultureProductId = $this->request->get('cultureProductId', null);
		$this->responseArray['product_id'] = $this->productId;
		$this->responseArray['status'] = 'error';
	
		$this->projectSetting = $this->request->get('project_setting');
		$this->cultureId = $this->projectSetting['culture_id'];
		$this->userId = $this->projectSetting['user_id'];
	
		$this->commonLibrary = $this->get('office_brain_common_library');
		$this->shippingEstimatorManager = $this->get('office_brain_shipping_estimator_manager_ups');
		$this->country = array('US'=>'USA','CA'=>'Canada');
	}
	
	/**
     * @author Employee Id: 4471
     * Description : get shipping rate of UPS
     * @param 1 : No paramater
     * @throws Exception: Null
	 * @Route("/{_locale}/calculate-ups-shipping-rate-estimator-submit",
     * name="officebrain_shippingestimatorbundle_calculateupsshippingrateestimator_submit",
     * options={"expose"=true}
     * )
     * @Template()
     * 
     */
	
	public function submitCalculateUPSShippingRateEstimatorAction()
	{
		$this->initAction();
		if ($this->request->getMethod() == 'POST')
		{
			$id=$this->request->get('productId');
	        $this->form = $this->createForm(new ShippingEstimatiorType($this->country));
	        $this->form->handleRequest($this->request);
	    	if ($this->form->isValid()) {
	    		
				$qty = $this->form['qty']->getData();
				$userAdd = $this->form['address']->getData();
				$userCity = $this->form['city']->getData();
				$userState = $this->form['state']->getData();
				$userZipcode = $this->form['zip_code']->getData();
				$userCountry = $this->form['country']->getData();
				$userIsResiden = $this->form['is_resident']->getData();
				$userDetail=array('reciver'=>array('userAdd'=>$userAdd,'userCity'=>$userCity,'userState'=>$userState ,'userZip'=>$userZipcode, 'userCountry' =>$userCountry,'userPhone'=>'','userName'=>'','userIsResident'=>$userIsResiden));
				$this->responseArray ['shipping_data'] =$this->upsRateCalculateAction($id,$qty,$userDetail);
				$this->responseArray ['status'] = 'success';
	    	}
	    	else
	    	{
			
	   			$this->responseArray = $this->commonLibrary->prepareFormJsonError($this->form);
				$this->responseArray ['status'] = 'error';
	    	}
    	}
    	return new JsonResponse($this->responseArray);
	}
	
	/**
	 * @Route("/{_locale}/ups-cb-url",
	 * name="officebrain_shippingestimatorbundle_ups_callbackUrl" ,
	 * options={"expose"=true})
	 * @Template()
	 */

	public function upsCallBackURLAction()
	{
		echo "call back url"; die;
	}

	/**
	 * @Route("/{_locale}/ups-rate-calculate",
	 * name="officebrain_shippingestimatorbundle_ups_ratecalculation" ,
	 * options={"expose"=true})
	 * @Template()
	 */
	
	//public function upsRateCalculateAction($id,$qty,$userDetail)
	public function upsRateCalculateAction()
	{
		$this->initAction();
		if($this->projectSetting['configuration_rule']['transaction']['shipping_estimator']['option']['registered_user']['option']['ups']['option']['ups_rate']['status']==1)
		{
			$this->shippinEstiConfi = $this->projectSetting['configuration_rule']['transaction']['shipping_estimator']['option']['registered_user']['option']['ups']['option']['ups_rate']['option'];
			//$this->qty = $qty;
			$this->qty = 3;
			$id=332931;
			$this->qty = $this->request->get('qty', 3);
			$this->userAdd= $this->request->get('address', array('1750-1798 N 9th St'));
			$this->userCity= $this->request->get('city', 'Kansas City');
			$this->userState = $this->request->get('state', 'KS');
			$this->userZipcode = $this->request->get('pincode', '66101');
			$this->userCountry = $this->request->get('country','US');
			$this->userPhone = $this->request->get('phone','620-123-4567');
			$this->userName = $this->request->get('contact_name','XYZ');
			$this->userIsResiden = $this->request->get('is_resident','0');
			$this->weight = $this->request->get('weight', 0);
				
			$this->productId = $this->request->get('product_id',$id);
			$this->productSlug = $this->request->get('product_slug');
				
			$userDetail=array('reciver'=>array('userAdd'=>$this->userAdd,'userCity'=>$this->userCity,'userState'=>$this->userState ,'userZip'=>$this->userZipcode, 'userCountry' =>$this->userCountry,'userPhone'=>$this->userPhone,'userName'=>$this->userName,'userIsResident'=>$this->userIsResiden));
		
			$productDetail = $this->shippingEstimatorManager->getProductDetailForShipping($this->productId,$this->qty);
			unset($productDetail['shipper']);
			$shipperDetail = array('shipper'=>array('shipperAdd'=>array(''),'shipperCity'=>'','shipperState'=>'Bergen' ,'shipperZip'=>'5835', 'shipperCountry' =>'NO','shipperPhone'=>'','shipperCompanyName'=>'','shipperEmail'=>'','shipperName'=>''));
		
			$shippingEstiDetail=array_merge($productDetail,$userDetail,$shipperDetail) ;
				
			$this->responseArray = $this->shippingEstimatorManager->callRequest($shippingEstiDetail,$this->shippinEstiConfi,'shippingEstimator');
			print_r($this->responseArray); exit();
			
			return $this->responseArray;
		}
	}
	
	
	/**
	 * @Route("/{_locale}/ups-create-shipment",
	 * name="officebrain_shippingestimatorbundle_ups_createshipment" ,
	 * options={"expose"=true})
	 * @Template()
	 */
	
	public function upsCreateShipmentAction()
	{
		$this->initAction();
		if($this->projectSetting['configuration_rule']['transaction']['shipping_estimator']['option']['registered_user']['option']['ups']['option']['ups_ship']['status']==1)
		{
			$this->qty = $this->request->get('qty', 3);
			
			$this->userAdd= $this->request->get('address', '1750-1798 N 9th St');
			$this->userCity= $this->request->get('city', 'Kansas City');
			$this->userState = $this->request->get('state', 'KS');
			$this->userZipcode = $this->request->get('pincode', '66101');
			$this->userCountry = $this->request->get('country','US');
			$this->userPhone = $this->request->get('phone','620-123-4567');
			$this->userName = $this->request->get('contact_name','XYZ');
			$this->userIsResiden = $this->request->get('isResiden','1');
			$this->weight = $this->request->get('weight', 0);
			$this->serviceType = $this->request->get('serviceType', 'FIRST_OVERNIGHT');
			
			$this->productId = $this->request->get('product_id','203');
			$this->productSlug = $this->request->get('product_slug');
			
			
			$userDetail=array('reciver'=>array('userAdd'=>$this->userAdd,'userCity'=>$this->userCity,'userState'=>$this->userState ,'userZip'=>$this->userZipcode, 'userCountry' =>$this->userCountry,'userPhone'=>$this->userPhone,'userName'=>$this->userName,'userIsResident'=>$this->userIsResiden),'serviceType'=> $this->serviceType);
			$productDetail = $this->shippingEstimatorManager->getProductDetailForShipping($this->productId,$this->qty);
			unset($productDetail['shipper']);
			$shipperDetail = array('shipper'=>array('shipperAdd'=>'909 Ferndale Ave','shipperCity'=>'Herndon','shipperState'=>'VA' ,'shipperZip'=>'20170', 'shipperCountry' =>'US','shipperPhone'=>'123-123-1234','shipperCompanyName'=>'Company Name','shipperEmail'=>'test@xyz.com','shipperName'=>'XYZ Name'));
			
			$shippingEstiDetail=array_merge($productDetail,$userDetail,$shipperDetail) ;
			
			$this->shippinEstiConfi = $this->projectSetting['configuration_rule']['transaction']['shipping_estimator']['option']['registered_user']['option']['ups']['option']['ups_ship']['option'];
			$shipmentLabel = $this->shippingEstimatorManager->callRequest($shippingEstiDetail,$this->shippinEstiConfi,'shippingDetail');
			echo $shipmentLabel;
			exit();
		}
		else
		{
			echo "Permission deny contact admin for setting ";
		}
	}

	/**
	 * @Route("/{_locale}/ups-track-shipment",
	 * name="officebrain_shippingestimatorbundle_ups_trackshipment" ,
	 * options={"expose"=true})
	 * @Template()
	 */
	
	public function trackShipmentAction()
	{
		$this->initAction();
		$trackingNumber = '1Z12345E1512345676'; //1Z12345E1512345676
		
		if($this->projectSetting['configuration_rule']['transaction']['shipping_estimator']['option']['registered_user']['option']['ups']['option']['ups_track']['status']==1)
		{
			$this->shippinEstiConfi = $this->projectSetting['configuration_rule']['transaction']['shipping_estimator']['option']['registered_user']['option']['ups']['option']['ups_track']['option'];
			$response = $this->shippingEstimatorManager->callRequest($trackingNumber,$this->shippinEstiConfi,'shippingDetail');
			
			if($response['TRACKRESPONSE']['RESPONSE']['RESPONSESTATUSDESCRIPTION']=='Success')
			{
				echo "<pre>";
				print_r($response);
				exit();
			}
			else
			{
				echo $response['TRACKRESPONSE']['RESPONSE']['ERROR']['ERRORDESCRIPTION'];
				exit();
			}
		}
		else {
			echo "contact admin for more setting !!";
		}
	}
}
