<?php

class MailConfigApi {
	public function __construct() {

		$this->ApiUserId 		= "80c6d2b487b59184237bd51ae2ef0ea6";
		$this->ApiSecret 		= "f3e7de62c8dc82bb327f999cf88e0f45";


		// schedule 			= daily => 1
		// 						= days_of_week = [0,4]	// 0 = Sunday, 4 = Thursday
		// 						= days_of_month	= [19] 	// 19th day of the month
		// 						= days_of_year 	= [1,91,181,271] = 1st day of year, 91st day of year, 181th day of year, 271st day of year


	}


	public function GetCustomerProducts($ClientId, $CustomerId) {
		$Args 					= array('client_id'		=> $ClientId,
										'customer_id'	=> $CustomerId
										);

		$Result 				= $this->CallApi('MailConfig', 'GetCustomerProducts', $Args, 'get');
		// echo "Result = " . print_r($Result, true) . "\n";
		return $Result;
	}



	public function GetCustomerProductConfigs($CustomerId, $ProductId) {
		$Args 					= array('customer_id'	=> $CustomerId,
										'product_id'	=> $ProductId
											);

		$Result 				= $this->CallApi('MailConfig', 'GetCustomerProductConfigs', $Args, 'get');
		// echo "Result = " . print_r($Result, true) . "\n";
		return $Result;
	}



	public function GetCustomerProductConfig($PortalConfigId) {
		$Args 					= array('us2_product_portal_config_id'	=> $PortalConfigId
											);

		$Result 				= $this->CallApi('MailConfig', 'GetCustomerProductConfig', $Args, 'get');
		// echo "Result = " . print_r($Result, true) . "\n";
		return $Result;
	}



	public function GetCustomerProductConfigHistory($PortalConfigId) {
		$Args 					= array('us2_product_portal_config_id'	=> $PortalConfigId
											);

		$Result 				= $this->CallApi('MailConfig', 'GetCustomerProductConfigHistory', $Args, 'get');
		// echo "Result = " . print_r($Result, true) . "\n";
		return $Result;
	}



	public function GetCustomerProductPoolConfigs($PoolId) {
		$Args 					= array(
										'us2_product_portal_pool_id' => $PoolId
											);

		$Result 				= $this->CallApi('MailConfig', 'GetCustomerProductPoolConfigs', $Args, 'get');
		// echo "Result = " . print_r($Result, true) . "\n";
		return $Result;
	}

	public function GetClientProductLayouts($ClientId, $ProductId) {
		$Args 					= array(
										'client_id'		=> $ClientId,
										'product_id'	=> $ProductId,
											);

		$Result 				= $this->CallApi('MailConfig', 'GetClientProductLayouts', $Args, 'get');
		// echo "Result = " . print_r($Result, true) . "\n";
		return $Result;

	}

	public function GetClientProductDials($ClientId, $ProductId) {
		$Args 					= array(
										'client_id'		=> $ClientId,
										'product_id'	=> $ProductId,
											);

		$Result 				= $this->CallApi('MailConfig', 'GetClientProductDials', $Args, 'get');
		// echo "Result = " . print_r($Result, true) . "\n";
		return $Result;

	}


	public function GetClientProductAssetMap($ClientId, $ProductId) {
		$Args 					= array(
										'client_id'		=> $ClientId,
										'product_id'	=> $ProductId,
											);

		$Result 				= $this->CallApi('MailConfig', 'GetClientProductAssetMap', $Args, 'get');
		// echo "Result = " . print_r($Result, true) . "\n";
		return $Result;

	}



	public function SetCustomerProductConfig($ProductPortalConfigId, $Us2ProductPortalPoolId, $CustomerId, $ProductId, $Format, $Description, $Settings, $IsDefaultYn, $StartDate, $EndDate, $Priority, $IsOnlineYn, $SfGuardUserId=0, $AmmsAuthUserId=0) {
		$Args 					= array(
										'us2_product_portal_config_id'	=> $ProductPortalConfigId,
										'customer_id'	=> $CustomerId,
										'product_id'	=> $ProductId,
										'format'		=> $Format,
										'description' 	=> $Description,
										'settings'		=> $Settings,
										'is_default_yn'	=> $IsDefaultYn,
										'is_online_yn'	=> $IsOnlineYn,
										'start_date'	=> $StartDate,
										'end_date'		=> $EndDate,
										'priority'		=> $Priority,
										'us2_product_portal_pool_id' => $Us2ProductPortalPoolId,
										'sf_guard_user_id'	=> $SfGuardUserId,
										'ammsauth_user_id'	=> $AmmsAuthUserId
											);

		$Result 				= $this->CallApi('MailConfig', 'SetCustomerProductConfig', $Args, 'get');
		// echo "Result = " . print_r($Result, true) . "\n";
		return $Result;
	}



	public function DeleteCustomerProductConfig($ProductPortalConfigId, $SfGuardUserId=0, $AmmsAuthUserId=0) {
		$Args 					= array(
										'us2_product_portal_config_id'	=> $ProductPortalConfigId,
										'sf_guard_user_id'	=> $SfGuardUserId,
										'ammsauth_user_id'	=> $AmmsAuthUserId

											);

		$Result 				= $this->CallApi('MailConfig', 'DeleteCustomerProductConfig', $Args, 'get');
		// echo "Result = " . print_r($Result, true) . "\n";
		return $Result;
	}
	


	public function GetCustomerProductPool($Us2ProductPortalPoolId) {
		$Args 					= array('us2_product_portal_pool_id'		=> $Us2ProductPortalPoolId
										);

		$Result 				= $this->CallApi('MailConfig', 'GetCustomerProductPool', $Args, 'get');
		// echo "Result = " . print_r($Result, true) . "\n";
		return $Result;
	}
	


	public function GetCustomerProductPools($CustomerId) {
		$Args 					= array('customer_id'		=> $CustomerId
										);

		$Result 				= $this->CallApi('MailConfig', 'GetCustomerProductPools', $Args, 'get');
		// echo "Result = " . print_r($Result, true) . "\n";
		return $Result;
	}
	


	public function SetCustomerProductPool($ClientId, $CustomerId, $Name, $PoolId) {
		$Args 					= array('customer_id'		=> $CustomerId,
										'client_id'			=> $ClientId,
										'name' 				=> $Name,
										'us2_product_portal_pool_id' => $PoolId
										);

		$Result 				= $this->CallApi('MailConfig', 'SetCustomerProductPool', $Args, 'get');
		// echo "Result = " . print_r($Result, true) . "\n";
		return $Result;
	}
	


	public function DeleteCustomerProductPool($CustomerId, $Us2ProductPortalPoolId) {
		$Args 					= array('customer_id' 		=> $CustomerId,
										 'us2_product_portal_pool_id'	=> $Us2ProductPortalPoolId
										);

		$Result 				= $this->CallApi('MailConfig', 'DeleteCustomerProductPool', $Args, 'get');
		// echo "Result = " . print_r($Result, true) . "\n";
		return $Result;
	}
	


	public function GetCustomerProductPoolDetails($Us2ProductPortalPoolId) {
		$Args 					= array('us2_product_portal_pool_id'	=> $Us2ProductPortalPoolId
										);

		$Result 				= $this->CallApi('MailConfig', 'GetCustomerProductPoolDetails', $Args, 'get');
		// echo "Result = " . print_r($Result, true) . "\n";
		return $Result;
	}




	private function CallApi($api_class='MailConfig', $api='', $args=array(), $method) 
	{
	
		$baseurl 				= "http://ammstech.org/{$api_class}/";


		$args['api_time'] 		= date("Y-m-d H:i:s");		// We add this to frustrate someone watching the API
															// - if they assume that the signature is just md5('[]' + ApiSecret) 
															//   when there are no actual parameters
															//   this value is ignored by the receiving API
		$DataJson 				= json_encode($args);
		$Signature 				= md5($DataJson . $this->ApiSecret);

	


		$url					= $baseurl.$api;
		// $method			= $this->methods[$api];
		// $params				= array(
		// 	'payload'		=> json_encode($args),
		// 	'api_user_id'		=> $this->ApiUserId,
		// 	'signature'		=> $Signature
		// );
		$params					= array(
			'payload'			=> json_encode($args),
			'account'			=> $this->ApiUserId,
			'signature'			=> $Signature
		);

		// echo "params = <pre>" . print_r($params, true) . "</pre><br />";

		// echo "CallApi() <br />url = {$url}<br />method = {$method}<br />\n";

		$query_string		= !empty($params) ? http_build_query($params,'','&') : "";
		$content_type		= 'application/x-www-form-urlencoded';

		$ch					= curl_init();

		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
		curl_setopt($ch, CURLOPT_TIMEOUT, 6000);

		if ($method == "post" || $method == "put") {
			$length				= count($params);
			$query_string		= !empty($params) ? http_build_query($params,'','&') : "";
			// echo "post/put: query_string = {$query_string}\n";
			// echo "post/put: url = {$url}\n";
			curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: '.$content_type));
			curl_setopt($ch, CURLOPT_HEADER, FALSE);
			curl_setopt($ch, CURLOPT_POST, $length);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
			curl_setopt($ch, CURLOPT_URL, $url);
		} else {
			if (!empty($params)) {
				$url				= $url . "?".http_build_query($params,'','&');
				// echo "get() url = $url\n";
			} else {
				$url				= $url;
			}

			curl_setopt($ch, CURLOPT_URL, $url);
		}

		if ($method == "put" || $method == "delete") {
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
		}
		// echo " final url={$url}<br />";

		curl_setopt($ch,CURLOPT_ENCODING , "gzip");
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
		$response = curl_exec($ch);

		// echo "response = " . print_r($response, true) . "<br />";

		if (!$response) {
			// echo "info about the event.\n" . print_r(curl_getinfo($ch), true) . "\n";
			$msg		= "Error Number ".curl_errno($ch)." Error Text:".curl_error($ch);
			$response	= json_encode(array("status"=>"error","message"=>"Curl Error -".$msg,"data"=>false));

			curl_close($ch);
			return $response;
		} else {
			curl_close($ch);
			return $response;
		}

	}
}