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

use FedEx\TrackService;
use FedEx\TrackService\ComplexType;
use FedEx\TrackService\SimpleType;
use OfficeBrain\Bundle\ShippingEstimatorBundle\Manager\AbstractManager\ShippingEstimatorAbstract;
use OfficeBrain\Bundle\ShippingEstimatorBundle\Manager\Service\ShippingEstimatorService;
use OfficeBrain\Bundle\ShippingEstimatorBundle\Helper\FedexHelper;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;

Class TrackFedexExtended extends ShippingEstimatorAbstract
{
	public function __construct($entityManager, $request, $session, $productDetailService)
	{
		parent::__construct($entityManager, $request, $session, $productDetailService);
	}
	
	public function __destruct()
	{
		parent::__destruct();
	}

	public function setCarrierCode()
	{
		$this->trackShipmentRequest->setCarrierCode(new SimpleType\CarrierCodeType('FDXG'));
	}
	
	public function setTrackingCode()
	{
		//$this->trackShipmentRequest->setTrackingNumberUniqueIdentifier('EZ1000000001');
	}
	
	public function setPackageIdentifier($trackingNumber)
	{
		$packageIdentifier= new ComplexType\TrackPackageIdentifier();
		$packageIdentifier
		->setType(SimpleType\TrackIdentifierType::_TRACKING_NUMBER_OR_DOORTAG)
		->setValue($trackingNumber); //123456789012 for used test mode.122816215025810
		$this->trackShipmentRequest->setPackageIdentifier($packageIdentifier);
	}
	
	public function callRequest($trackingNumber,$shippinEstiConfi,$callFrom)
	{
		$this->trackShipmentRequest = new ComplexType\TrackRequest();
		$ObjFedex = new FedexHelper('track',$shippinEstiConfi);
		
		//WebAuthenticationDetail
		$webAuthenticationDetail=$ObjFedex->setAuthenticationDetail();
		$this->trackShipmentRequest->setWebAuthenticationDetail($webAuthenticationDetail);
		
		//ClientDetail
		$clientDetail=$ObjFedex->setClientDetail();
		$this->trackShipmentRequest -> setClientDetail($clientDetail);
		
		//TransactionDetail
		$transactionDetail=$ObjFedex->setTransaction();
		$this->trackShipmentRequest->setTransactionDetail($transactionDetail);
		
		//Version
		$version=$ObjFedex->setVersionDetail();
		$this->trackShipmentRequest->setVersion($version);
		
		//$this->trackShipmentRequest->setCarrierCode();
		
		$this->trackShipmentRequest->setCarrierCode(new SimpleType\CarrierCodeType('FDXG'));
		 
		$this->setTrackingCode();
		
		$this->trackShipmentRequest->setIncludeDetailedScans(true);
		//$this->trackShipmentRequest->setPagingToken(true);
		 
		$this->setPackageIdentifier($trackingNumber);

		
		//$this->reply = new TrackService\Request('../src/OfficeBrain/Bundle/ShippingEstimatorBundle/wsdl/TrackService_v9.wsdl'); // live 
		$this->reply = new TrackService\Request('../src/OfficeBrain/Bundle/ShippingEstimatorBundle/wsdl/TrackService_v5.wsdl');
		$this->response = $this->reply->getTrackReply($this->trackShipmentRequest);
		
		
		/*echo "<pre>";
		print_r($this->reply);
		print_r($this->trackShipmentRequest->toArray());
		print_r($this->response);
		exit();*/
		
		
		return $this->response;
	}
}