<?php
/** 
 * @package   	VikAppointments
 * @subpackage 	com_vikappointments
 * @author    	Matteo Galletti - e4j
 * @copyright 	Copyright (C) 2018 e4j - Extensionsforjoomla.com. All Rights Reserved.
 * @license  	http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link 		https://extensionsforjoomla.com
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted Area');

class VikSmsApi
{	
	private $order_info;

	private $params;

	private $log = '';

	public static function getAdminParameters()
	{
		return array(	
			'apikey' => array(
				'label' => 'API Key',
				'type'  => 'text',
			),
				
			'apisecret' => array(
				'label' => 'API Secret',
				'type'  => 'text',
			),
			
			'sender' => array(
				'label' => 'Sender Name//max 11 characters',
				'type'  => 'text',
			),
			
			'sandbox' => array(
				'label' => 'Sandbox',
				'type'  => 'select',
				'options' => array(
					1 => 'Yes',
					0 => 'No',
				),
			),
		);
	}
	
	public function __construct($order, $params = array())
	{
		$this->order_info = $order;
		$this->params 	  = $params;
	}
	
	public function sendMessage($phone_number, $msg_text)
	{
		$phone_number = $this->sanitizePhoneNumber($phone_number);
		
		$request = array(
			"apikey"    => $this->params['apikey'],
			"apisecret" => $this->params['apisecret'],
			"from"      => $this->params['sender'],
			"to"        => $phone_number,
			"msg"       => $msg_text,
			"sandbox"   => (int) $this->params['sandbox'],
		);
		
		$json = json_encode($request);

		$curl_opts = array(
			CURLOPT_POST => true,
			CURLOPT_POSTFIELDS => $json,
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_HTTPHEADER => array(
					'Accept: application/json',
					'Content-Type: application/json;charset=UTF-8',
					'Content-Length: ' . strlen($json),
			),
			CURLOPT_VERBOSE => true,
		);
	 
		$ch = curl_init('https://secure.sms.fakeprovider.com/send');
		curl_setopt_array($ch, $curl_opts);
		$response = curl_exec($ch);

		return json_decode($response);
	}
	
	protected function sanitizePhoneNumber($phone_number)
	{
		$str = '';
		for ($i = 0; $i < strlen($phone_number); $i++)
		{
			if (($phone_number[$i] >= '0' && $phone_number[$i] <= '9') || $phone_number[$i] == '+')
			{
				$str .= $phone_number[$i]; // copy only numbers and plus character
			}
		}
		
		$default_prefix = '+1'; // US, Canada phone prefix
		
		if ($phone_number[0] != '+')
		{ 
			// $phone_number doesn't contain the phone prefix 
			$str = $default_prefix . $str;
		}
		
		return $str;
	}
	
	public function validateResponse($response_obj)
	{
		if ($response_obj->httpcode == 200)
		{
			return true;
		}

		$this->log .= $response_obj->errmsg;
		return false;
	}
	
	public function estimate($phone_number, $msg_text)
	{
		$phone_number = $this->sanitizePhoneNumber($phone_number);
		
		$request = array(
			"apikey"    => $this->params['apikey'],
			"apisecret" => $this->params['apisecret'],
			"from"      => $this->params['sender'],
			"to"        => $phone_number,
			"msg"       => $msg_text,
			"sandbox"   => (int) $this->params['sandbox'],
		);
		
		$json = json_encode($request);

		$curl_opts = array(
			CURLOPT_POST => true,
			CURLOPT_POSTFIELDS => $json,
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_HTTPHEADER => array(
				'Accept: application/json',
				'Content-Type: application/json;charset=UTF-8',
				'Content-Length: ' . strlen($json),
			),
			CURLOPT_VERBOSE => true,
		);
	 
		$ch = curl_init('https://secure.sms.fakeprovider.com/estimate');
		curl_setopt_array($ch, $curl_opts);
		$response = curl_exec($ch);
		$response = json_decode($response);

		$fixedResponse = new stdClass;
		$fixedResponse->errCode 	= $response->httpcode; 
		$fixedResponse->userCredit  = 0.0;
		if ($fixedResponse->errCode == 200)
		{
			$fixedResponse->errCode 	= 0;
			$fixedResponse->userCredit  = $response->credit;
		}

		return $fixedResponse;
	}
	
	public function getLog()
	{
		return $this->log;
	}
}