<?php
namespace OfficeBrain\Bundle\OrderBundle\ApiClient;

use OfficeBrain\Bundle\ApiClientBundle\ApiClient\AbstractApiClient;

class ApiClient extends AbstractApiClient
{
	protected $apiCallUrl;
	protected $result_set;
	public function __construct()
	{
// 		$this->apiCallUrl='http://test.officebeacon.com/AakronAPI/api/products?CustomerNo='.$this->responseArray['aakronCustomer'].'&OrderNo=&CustomerPO='.$this->responseArray['aakronCustomerPO'];
		$this->apiCallUrl='http://test.officebeacon.com/AakronAPI/api/products?';
		$this->encodeURL = false;
		$this->result_set=array();
	}
	public function doProcess()
	{
		switch ($this->apiCallType) {
			case 'order-status':
				break;
			case 'order-report':
				$this->apiCallUrl='http://test.officebeacon.com/AakronAPI/api/invoicedetails?';
				break;
			default:
				break;
		}
		$this->callApiOrderStatus();
	}
	public function init($apiCallType){
		$this->apiCallType = trim($apiCallType);
	}
	public function callApiOrderStatus()
	{
		$this->prepareOrderStatusApiUrlParameter();
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $this->apiCallUrl);
		curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		curl_setopt($ch, CURLOPT_USERPWD, "officebeacon:test123");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 4);
		curl_setopt($ch, CURLOPT_TIMEOUT, 1000);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
		$xml = curl_exec($ch);
		if($xml!="No Data Found")
		{
			$xml_utf8 = preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $xml);
			$this->result_set=json_decode(json_encode(simplexml_load_string($xml_utf8)));
		}
		else
			$this->result_set="NORECORDFOUND";
		
	}
	public function getCallParameters(){
		return $this->callParameters;
	}
	
	public function setCallParameters($callParameters){
		
		$this->callParameters = $callParameters;
	}
	private function prepareOrderStatusApiUrlParameter(){
		$apiParameterArray = array();
			foreach ($this->callParameters as $parameterName=>$parameterValue) {
				if($this->encodeURL){
					$parameterValue= urlencode($parameterValue);
				}
				@$apiParameterArray[] = $parameterName.'='.$parameterValue;
			}
		$this->apiCallUrl = $this->apiCallUrl.implode('&', $apiParameterArray);
	
		unset($apiParameterArray);
	}
	public function getResultArray()
	{
		return $this->result_set;
	}
	
	
}
