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

use FedEx\ShipService;
use FedEx\ShipService\ComplexType;
use FedEx\ShipService\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 ShipFedexExtended extends ShippingEstimatorAbstract
{
	public function __construct($entityManager, $request, $session, $productDetailService)
	{
		parent::__construct($entityManager, $request, $session, $productDetailService);
	}
	
	public function __destruct()
	{
		parent::__destruct();
	}

	public function setTotalInsuredValue($issuranceAmount,$issuranceCurrency)
	{
		$money = new ComplexType\Money();
		$money
		->setAmount($issuranceAmount)
		->setCurrency($issuranceCurrency);
		$this->requestedShipment->setTotalInsuredValue($money);
	}
	
	public function setShippingLabelSpecification($imageType,$labelFormat,$stockType)
	{
		//RequestedShipment/ShippingLabelSpecification
		$labelSpecification = new ComplexType\LabelSpecification();
		$labelSpecification
		->setImageType($imageType)
		->setLabelFormatType($labelFormat)
		->setLabelStockType($stockType);
		$this->requestedShipment->setLabelSpecification($labelSpecification);
	}
	
	public function getLabel($shippinEstiConfi)
	{
		$filename='shiplabel'.'_'.date('mdYhis').'.'.strtolower($this->response->CompletedShipmentDetail->CompletedPackageDetails->Label->ImageType);
		$ObjFedex = new FedexHelper('ship',$shippinEstiConfi);
		$file=$ObjFedex->allParameter['label_img_path'].$filename;
		$fp = fopen($file, 'wb');
		fwrite($fp, ($this->response->CompletedShipmentDetail->CompletedPackageDetails->Label->Parts->Image));
		fclose($fp);
		return '<a href="../../'.$file.'">'.$filename.'</a> was generated.';
	}
	
	public function getBarcode()
	{
		$filename='shiplabel.'.strtolower($this->response->CompletedShipmentDetail->CompletedPackageDetails->Label->ImageType);
		$file=$this->allParameter['barcode_img_path'].$filename;
		$fp = fopen($file, 'wb');
		fwrite($fp, ($this->response->CompletedShipmentDetail->CompletedPackageDetails->OperationalDetail->Barcodes->BinaryBarcodes->Value));
		fclose($fp);
		return '<a href="../'.$file.'">'.$filename.'</a> was generated.';
	}
	
	public function callRequest($shippingEstiDetail,$shippinEstiConfi)
	{
		//$this->initAction();
		$this->accountNumber ='510087844';
		$issuranceAmount = 0;
		$issuranceCurrency = 'USD';
		$this->createProcessShipmentRequest = new ComplexType\ProcessShipmentRequest();
		$this->ObjFedex = new FedexHelper('ship',$shippinEstiConfi);
		
		//WebAuthenticationDetail
		$webAuthenticationDetail=$this->ObjFedex->setAuthenticationDetail();
		$this->createProcessShipmentRequest->setWebAuthenticationDetail($webAuthenticationDetail);
		//ClientDetail
		$clientDetail=$this->ObjFedex->setClientDetail();
		$this->createProcessShipmentRequest -> setClientDetail($clientDetail);
		//TransactionDetail
		$transactionDetail=$this->ObjFedex->setTransaction();
		$this->createProcessShipmentRequest->setTransactionDetail($transactionDetail);
		//Version
		$version=$this->ObjFedex->setVersionDetail();
		$this->createProcessShipmentRequest->setVersion($version);
		//$this->ObjFedex->setShipmentDetail($shippingEstiDetail);
		$this->requestedShipment=$this->ObjFedex->setShipmentDetail($shippingEstiDetail);
		if($shippinEstiConfi['is_issurance_allow']['status'] == '1')
		{
			$this->setTotalInsuredValue($shippinEstiConfi['is_issurance_allow']['option']['amount']['status'],$shippinEstiConfi['is_issurance_allow']['option']['currency']['status']);
		}
		$this->setShippingLabelSpecification($shippinEstiConfi['label_doc_image_type']['status'],$shippinEstiConfi['label_format_type']['status'],$shippinEstiConfi['label_stock_type']['status']);
		$this->createProcessShipmentRequest->setRequestedShipment($this->requestedShipment);
		$this->reply = new ShipService\Request();
		$this->response = $this->reply->getProcessShipmentReply($this->createProcessShipmentRequest);
		$result=$this->getLabel($shippinEstiConfi);
		return $result; 
	   
	}
}
?>