<?php 

function infobip() {


}

function YI_whatsapp($template,$no,$param,$header=null){
	if($header){
		if($header=='IMAGE'){
			withimgheader($template,$no,$param);
		}elseif($header=='VIDEO'){
			withvideoheader($template,$no,$param);
		}
	}else{
		noheader($template,$no,$param);
	}
}
	
function noheader($template,$no,$param){
	$data = array();		
	//$data['scenarioKey']="77E3760DB647A79B03F245C55FE7D3A2";
	
	$data['scenarioKey']=$_SESSION['infobipscenario'];
	$data['destinations'][0]['to']['phoneNumber']=$no;
	$data['whatsApp']['templateName']=$template;
	$data['whatsApp']['templateData']=$param;
	$data['whatsApp']['language']='en';	
	//echo json_encode($data); 
	sendWhatsapp($data);	
}

function imageheader($template,$no,$param,$media,$mediatype,$btn1,$btn2,$key,$scenario){
	$data = array();		
	//$data['scenarioKey']="77E3760DB647A79B03F245C55FE7D3A2";
	
	//$data['scenarioKey']=$_SESSION['infobipscenario'];
	$data['scenarioKey']=$scenario;
	
	$data['destinations'][0]['to']['phoneNumber']=$no;
	$data['whatsApp']['templateName']=$template;
	
	if($param)
	{
		$data['whatsApp']['mediaTemplateData']["body"]["placeholders"]=$param;
	}
	else{
		$data['whatsApp']['mediaTemplateData']["body"]["placeholders"]=array();
	}
	
	if($mediatype == 'MEDIA'){
		$data['whatsApp']['mediaTemplateData']["header"]["imageUrl"]=base_url('uploads/broadcast/'.$media);
	}elseif($mediatype == 'VIDEO'){
		$data['whatsApp']['mediaTemplateData']["header"]["videoUrl"]=base_url('uploads/broadcast/'.$media);
	}
	elseif($mediatype == 'DOCUMENT'){
		$data['whatsApp']['mediaTemplateData']["header"]["fileUrl"]=base_url('uploads/broadcast/'.$media);
	}
	
	
	if($param)
	{
	$data['whatsApp']['mediaTemplateData']["body"]["placeholders"]=$param;
	}
	
	if($btn1!=null && $btn1!='' ){
		$data['whatsApp']['mediaTemplateData']['buttons'][0]["urlPlaceholder"]=$btn1;
	}
	elseif($btn2!=null && $btn2!='' && $btn1==null && $btn1==''){
		$data['whatsApp']['mediaTemplateData']['buttons'][0]["urlPlaceholder"]=$btn2;
	}
	elseif($btn2!=null && $btn1!=null && $btn2!='' && $btn1!=''){
		$data['whatsApp']['mediaTemplateData']['buttons'][1]["urlPlaceholder"]=$btn2;
	}
	
	$data['whatsApp']['language']='en';

	json_encode($data); 
	sendWhatsapp($data,$key);	
}

function videoheader($template,$no,$param,$media,$mediatype,$btn1,$btn2,$key,$scenario){
	$data = array();		
	
	//$data['scenarioKey']="77E3760DB647A79B03F245C55FE7D3A2";
	
	//$data['scenarioKey']=$_SESSION['infobipscenario'];
	
	$data['scenarioKey']=$scenario;
	$data['destinations'][0]['to']['phoneNumber']=$no;
	$data['whatsApp']['templateName']=$template;
	
	
	
	if($mediatype == 'MEDIA'){
		$data['whatsApp']['mediaTemplateData']["header"]["imageUrl"]=base_url('uploads/broadcast/'.$media);
	}elseif($mediatype == 'VIDEO'){
		$data['whatsApp']['mediaTemplateData']["header"]["videoUrl"]=base_url('uploads/broadcast/'.$media);
	}
	elseif($mediatype == 'DOCUMENT'){
		$data['whatsApp']['mediaTemplateData']["header"]["fileUrl"]=base_url('uploads/broadcast/'.$media);
	}
	
	if($param)
	{
		$data['whatsApp']['mediaTemplateData']["body"]["placeholders"]=$param;
	}
	else{
		$data['whatsApp']['mediaTemplateData']["body"]["placeholders"]=array();
	}
	
	
	if($btn1!=null && $btn1!='' ){
		$data['whatsApp']['mediaTemplateData']['buttons'][0]["urlPlaceholder"]=$btn1;
	}
	elseif($btn2!=null && $btn2!='' && $btn1==null && $btn1==''){
		$data['whatsApp']['mediaTemplateData']['buttons'][0]["urlPlaceholder"]=$btn2;
	}
	elseif($btn2!=null && $btn1!=null && $btn2!='' && $btn1!=''){
		$data['whatsApp']['mediaTemplateData']['buttons'][1]["urlPlaceholder"]=$btn2;
	}
	
	$data['whatsApp']['language']='en';

	json_encode($data); 
	sendWhatsapp($data,$key);	
}

function sendWhatsapp($params,$key)
	{
		$CI = get_instance();
		$url = 'https://yr3plg.api.infobip.com/omni/1/advanced/';
		//$headerkey="App 2e99c35d9d74cd2245c4b3faf8170b09-5d183eae-74cc-48f0-9038-c27eeb79cf9d";
		
		//$headerkey = $_SESSION['infobipheader'];	
		$headerkey = $key;	
	
		$curlOptions = array( CURLOPT_RETURNTRANSFER => TRUE);
		$curlOptions[CURLOPT_CUSTOMREQUEST] = 'POST';
		$curlOptions[CURLOPT_URL] = $url;
		$requestHeaders = array(
				'Authorization:'.$headerkey,
				'Content-Type: application/json',
				'Accept: application/json'
		);
		
		$curlOptions[CURLOPT_POSTFIELDS] = json_encode($params);
		$curlOptions[CURLOPT_HTTPHEADER] = $requestHeaders;
		$curl = curl_init();
		curl_setopt_array($curl, $curlOptions);
		$response = curl_exec($curl);
		curl_close($curl);
		
		//echo"<br/><br/><br/>"; print_r($response);
		
		$rsp = json_decode($response, true);
		
		$contact = preg_replace("/[^0-9]/","",$params['destinations'][0]['to']['phoneNumber']);
		$contact = substr($contact,2);
		
		
		if(@$rsp['messages'][0]['status']['name'] == 'PENDING_ENROUTE'){
			$CI->Helper_model->updatemessageresponse($contact,1,$response);
			return 1;
		}
		else{
			$CI->Helper_model->updatemessageresponse($contact,3,$response);
			return 0;
		}
		
	}
	

?>