<?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;
use OfficeBrain\Bundle\ShippingEstimatorBundle\Helper\ShippingEstimatorHelper as ShippingEstimatorHelper;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

/**
 * Mobiel API Shipping Estimator
 *
 * @author Employee Id: 410175
 *
 * Desciption: This is a ShippingEstimator Controller
 *
 * @param NULL
 *
 * @return json Response
 *
 * @throws NULL
 *
 * @Route("/api/v1")
 */
class ApiShippingEstimatorController extends ShippingEstimatorController
{
	
	/**
	 *
	 * @author Employee Id: 4471
	 *
	 * To set member variables default value
	 *
	 * @return NULL
	 */
	public function __construct()
	{
		parent::__construct();
		$this->responseFormat = null;
	}
	
	/**
	 *
	 * @author Employee Id: 4471
	 *
	 * To unset member variables
	 *
	 * @return NULL
	 */
	public function __destruct()
	{
		parent::__destruct();
		unset($this->responseFormat);
	}
	
	public function initAction()
	{
		parent::initAction();
		$this->request = $this->getRequest();
		$this->responseFormat = $this->request->get('_format', null) ;
		$this->translator=$this->get('translator');
	}
	
	/*
	 * @author Employee Id: 4471
	 *
	 * This is used get shipping rate
	 *
	 *
	 * @Route("/{_locale}/get-shipping-country-list",
	 * name="officebrain_shippingestimatorbundle_calculateshippingrateestimator_api" )
	 * 
	 * @ApiDoc(
     *      resource=true,
     *      section="Shipping Estimator",
     *      description="",
     *      statusCodes={
     *              1="Returned when success",
     *              403="Returned when the user is not authorized to see this link",
     *              0="Returned when form validation is failed",
     *      },   
     *  requirements={
     *  	{"name"="_locale", "default"="en_us", "dataType"="string", "required"=true},
     *  	{"name"="access_token", "dataType"="string", "required"=false, "description"="valid access token"},     
     *  }
     * )
	 * 
	 * @Template()
	 * @Method("GET")
	 * /
	public function loadShippingCountryAction()
	{
		$this->initAction();
		$newCountry = array();
		$counter = 0;
		
		foreach ($this->country as $key=>$val){
			$newCountry[$counter]['key'] = $key;
			$newCountry[$counter]['value'] = $val;
			$counter++;
		}
		return array('status'=>'success','status_code'=>1,'data'=>$newCountry);
	}
	
	/**
	 * @author Employee Id: 410175
	 *
	 * This is used get shipping types
	 *
	 * @Route("/{_locale}/get-shipping-carrier",
	 * name="officebrain_shippingestimatorbundle_getshippingcarrier_api" )
	 *
	 * @ApiDoc(
	 *      resource=true,
	 *      section="Shipping Estimator",
	 *      description="",
	 *      statusCodes={
	 *              1="Returned when success",
	 *              403="Returned when the user is not authorized to see this link",
	 *              0="Returned when form validation is failed",
	 *      },
	 *  requirements={
	 *  	{"name"="_locale", "default"="en_us", "dataType"="string", "required"=true},
	 *  	{"name"="access_token", "dataType"="string", "required"=false, "description"="valid access token"},
	 *  }
	 * )
	 *
	 * @Template()
	 * @Method("GET")
	 * /
	public function getShippingCarrierAction()
	{
		$this->initAction();
		$newType = array();
		
		$types = $this->seviceType;
		foreach ($this->seviceType as $key=>$val){
			$newType[] = array('key'=>$key,'value'=>$val);
		}
		return array('status'=>'success','status_code'=>1,'data'=>$newType);
	}
	
	/**
     * @author Employee Id: 410175
     * Description : get shipping rate
     * @param 1 : No paramater
     * @throws Exception: Null
	 * @Route("/{_locale}/calculate-shipping-rate-estimator",
     * name="officebrain_shippingestimatorbundle_calculateshippingrateestimator_submit_api" )
     * 
     * @ApiDoc(
     *      resource=true,
     *      section="Shipping Estimator",
     *      description="",
     *      statusCodes={
     *              1="Returned when success",
     *              403="Returned when the user is not authorized to see this link",
     *              0="Returned when form validation is failed",
     *      }, 
     *       input = {
     *      	"class" = "OfficeBrain\Bundle\ShippingEstimatorBundle\Form\ApiShippingEstimatiorType"
     *      },      
     *  requirements={
     *  	{"name"="_locale", "default"="en_us", "dataType"="string", "required"=true},
     *  	{"name"="access_token", "dataType"="string", "required"=false, "description"="valid access token"},     
     *  	{"name"="product_id", "dataType"="integer", "required"=true}
     *  }
     * )
     * 
     * @Template()
     * 
     * @Method("POST")
     * /
	
	public function submitCalculateShippingRateEstimatorAction()
	{
			$this->initAction();		
			$id=$this->request->get('product_id',null);
			$type=$this->request->get('type');
			
			if($this->responseFormat == "json"){
				$this->form = $this->createForm(new ShippingEstimatiorType($this->country,$this->shippingEstinCalSeviceType,$this->isResi), null, array('csrf_protection' => false));
			}else{
				$this->form = $this->createForm(new ShippingEstimatiorType($this->country,$this->shippingEstinCalSeviceType,$this->isResi));
			}
	        
			if($id == null || $id<=0){
				return array('status'=>'error','status_code'=>0,'message'=>$this->translator->trans('shipping_estimator.form.product_detail_not_found'));	
			}
			
	        $this->form->handleRequest($this->request);
	    	if ($this->form->isValid()) 
	    	{
	    		$rateObj= new ShippingEstimatorHelper($this->fedexShippingEstimatorManager,$this->upsShippingEstimatorManager);
	    		//call fedex and/or ups
	    		$rateData = $rateObj->getShippingEstimatorRateForMobileAPI($this->form->getData(),$id,$this->projectSetting);
	    		if ($rateData!=false){
	    			$this->responseArray  = $rateData;
	    		}else{	    			
	    			return array('status'=>'error','status_code'=>0,'message'=>$this->translator->trans('shipping_estimator.form.product_detail_missing'));	    			
				}
		   	}else{
	   			
		   		$ErrorsMsg = '';
	   			foreach ($this->get('validator')->validate($this->form) as $error){
	   				$ErrorsMsg = $error->getMessage();break;
	   			}        		
        		return array('status'=>'error','status_code'=>0,'message'=>$ErrorsMsg);
		    }

		    if( $this->responseFormat == 'json')
    		{
    			return array('status'=>'success','status_code'=>1,'data'=>$this->responseArray);
    		}	
    		return new JsonResponse($this->responseArray);
	}	
	*/
}
