#include "stdafx.h"		/*you can comment out this header for non visual c++ compilers*/
#include <windows.h>	/*you can comment out this header for non visual c++ compilers*/
#include "smsapisimple.h" //-- change the location if need be or copy the header file to the project folder	

HINSTANCE g_hSMSAPIdll = NULL;
SMSAPIFUNCTION_SENDSMS sms_sendsms = NULL;
SMSAPIFUNCTION_SENDBATCH sms_sendbatch = NULL;
SMSAPIFUNCTION_GETREPORT sms_getreport = NULL;
SMSAPIFUNCTION_GETINBOX sms_getinbox = NULL;
SMSAPIFUNCTION_GETCREDITS sms_getcredits = NULL;
SMSAPIFUNCTION_INITIALIZE_DEFAULTS sms_initialize_defaults = NULL;

bool SMSAPISimple_Init(const char * lpszAPIFilename)
{
	bool bResult = false;
	g_hSMSAPIdll = LoadLibrary(lpszAPIFilename);
	if (g_hSMSAPIdll)
	{
		sms_sendsms = (SMSAPIFUNCTION_SENDSMS) GetProcAddress(g_hSMSAPIdll,"smsapi_sendsms");
		sms_sendbatch = (SMSAPIFUNCTION_SENDBATCH) GetProcAddress(g_hSMSAPIdll,"smsapi_sendbatch");
		sms_getreport = (SMSAPIFUNCTION_GETREPORT) GetProcAddress(g_hSMSAPIdll,"smsapi_getreport");
		sms_getinbox = (SMSAPIFUNCTION_GETREPORT) GetProcAddress(g_hSMSAPIdll,"smsapi_getinbox");
		sms_getcredits = (SMSAPIFUNCTION_GETCREDITS) GetProcAddress(g_hSMSAPIdll,"smsapi_getcredits");
		sms_initialize_defaults = (SMSAPIFUNCTION_INITIALIZE_DEFAULTS) GetProcAddress(g_hSMSAPIdll,"smsapi_initialize_defaults");		

		if (sms_sendsms && 
			sms_sendbatch &&
			sms_getreport &&
			sms_getinbox &&
			sms_getcredits && sms_initialize_defaults)
			bResult = true;
	}

	return bResult;
}

bool SMSAPISimple_CleanUp(void)
{
	bool bResult = false;
	if (FreeLibrary(g_hSMSAPIdll))
	{
		g_hSMSAPIdll = 0;
		bResult = true;
	}
	return bResult; 
}

int SMSAPISimple_SendSMS(const char * lpszUsername, const char * lpszPassword, const char * lpszMobile, const char * lpszMessage, int serverID)
{
	int nResult = -1;

	SMSAPI_NETWORKCONFIGURATION networkConfiguration;		//-- defines server, port, secure etc.
	SMSAPI_ACCOUNTINFO accountInfo;						//-- your bulksms account info is specified here
	SMSAPI_MESSAGEINFO messageInfo;						//-- the content of the Text SMS message, recipients, sender ID etc.
	SMSAPI_RESPONSEINFO responseInfo;						//-- responses are returned in this structure

	//-- allocate a 1k buffer for the result
	char szResultBuffer[1024];					
	responseInfo.lpszResult = szResultBuffer;
	responseInfo.cbSizeOfResultBuffer = sizeof(szResultBuffer);
	

	if (!sms_initialize_defaults || !sms_sendsms)
		return nResult;
	
	//-- initialize the structures. this nulls the entire structures and sets preferred default values
	sms_initialize_defaults(&networkConfiguration,sizeof(SMSAPI_NETWORKCONFIGURATION)); 
	sms_initialize_defaults(&accountInfo, sizeof(SMSAPI_ACCOUNTINFO));
	sms_initialize_defaults(&messageInfo, sizeof(SMSAPI_MESSAGEINFO));
	sms_initialize_defaults(&responseInfo, sizeof(SMSAPI_RESPONSEINFO));

	//-- the message ID is automatically generated by the bsms_initialize_defaults function. you leave the
	//   generated ID or use your own own, as long as the ID is unique. 	
	
	//-- set detailed specific information here
	networkConfiguration.nIdealPort = 80;					//-- use the standard HTTP internet port
	accountInfo.defaultServerID = (SMSAPI_SERVER) serverID;	//-- connect to the South Africa commerce server
	strcpy(accountInfo.szSMSAPIUsername,lpszUsername);		//-- set the bulksms account username
	strcpy(accountInfo.szSMSAPIPassword,lpszPassword);		//-- set the bulksms account password	
	strcpy(messageInfo.szMessageText,lpszMessage);			//-- set the message text
	messageInfo.lpszCommaSeperatedMobileList = lpszMobile;	//-- set the message recipient list (comma seperated)

	if (sms_sendsms(&networkConfiguration,&accountInfo,&messageInfo,&responseInfo))
	{
		nResult = 0;
		MessageBox(NULL,responseInfo.lpszResult,"",MB_OK);
	}
	else
	{
		nResult = responseInfo.nResult;
		MessageBox(NULL,responseInfo.lpszResult,"",MB_OK);
	}
	        	
	return nResult;
}
int SMSAPISimple_GetReport(const char * lpszUsername, const char * lpszPassword, const int serverID, const unsigned long dwMessageID, char * szReportData, int nSize)
{
	int nResult = -1;

	SMSAPI_NETWORKCONFIGURATION networkConfiguration;		//-- defines server, port, secure etc.
	SMSAPI_ACCOUNTINFO accountInfo;						//-- your bulksms account info is specified here
	SMSAPI_RESPONSEINFO responseInfo;						//-- responses are returned in this structure

	//-- allocate a 1k buffer for the result
	responseInfo.lpszResult = szReportData;
	responseInfo.cbSizeOfResultBuffer = nSize;
	
	//-- fail is no buffer allocated
	if (responseInfo.cbSizeOfResultBuffer < 1)
		return -1;
	
	//-- fail if function pointers have not been initialized
	if (!sms_initialize_defaults || !sms_getreport)
		return -1;
		
	//-- initialize the structures. this nulls the entire structures and sets preferred default values
	sms_initialize_defaults(&networkConfiguration,sizeof(SMSAPI_NETWORKCONFIGURATION)); 
	sms_initialize_defaults(&accountInfo, sizeof(SMSAPI_ACCOUNTINFO));
	sms_initialize_defaults(&responseInfo, sizeof(SMSAPI_RESPONSEINFO));

	//-- set detailed specific information here
	networkConfiguration.nIdealPort = 80;					//-- use the standard HTTP internet port
	accountInfo.defaultServerID = (SMSAPI_SERVER) serverID;	//-- connect to the South Africa commerce server
	strcpy(accountInfo.szSMSAPIUsername,lpszUsername);		//-- set the bulksms account username
	strcpy(accountInfo.szSMSAPIPassword,lpszPassword);		//-- set the bulksms account password	

	if (sms_getreport(&networkConfiguration,&accountInfo,dwMessageID,&responseInfo))
	{
		nResult = 0;
		MessageBox(NULL,responseInfo.lpszResult,"",MB_OK);
	}
	else
	{
		nResult = responseInfo.nResult;
		MessageBox(NULL,responseInfo.lpszResult,"",MB_OK);
	}
	        	
	return nResult;
}

int SMSAPISimple_SendBatch(const char * lpszUsername, const char * lpszPassword, const LPSMSAPI_BATCHITEM lpArray, const int nArrayItemCount, int serverID)
{
	int nResult = -1;

	SMSAPI_NETWORKCONFIGURATION networkConfiguration;		//-- defines server, port, secure etc.
	SMSAPI_ACCOUNTINFO accountInfo;							//-- your bulksms account info is specified here
	SMSAPI_BATCHINFO batchInfo;								//-- the content of the Text SMS message, recipients, sender ID etc.
	SMSAPI_RESPONSEINFO responseInfo;						//-- responses are returned in this structure

	//-- allocate a 1k buffer for the result
	char szResultBuffer[1024];					
	responseInfo.lpszResult = szResultBuffer;
	responseInfo.cbSizeOfResultBuffer = sizeof(szResultBuffer);
	

	if (!sms_initialize_defaults || !sms_sendbatch)
		return nResult;
	
	//-- initialize the structures. this nulls the entire structures and sets preferred default values
	sms_initialize_defaults(&networkConfiguration,sizeof(SMSAPI_NETWORKCONFIGURATION)); 
	sms_initialize_defaults(&accountInfo, sizeof(SMSAPI_ACCOUNTINFO));
	sms_initialize_defaults(&batchInfo, sizeof(SMSAPI_BATCHINFO));
	sms_initialize_defaults(&responseInfo, sizeof(SMSAPI_RESPONSEINFO));

	//-- the message ID is automatically generated by the bsms_initialize_defaults function. you leave the
	//   generated ID or use your own own, as long as the ID is unique. 	
	
	//-- set detailed specific information here
	networkConfiguration.nIdealPort = 80;					//-- use the standard HTTP internet port
	accountInfo.defaultServerID = (SMSAPI_SERVER) serverID;	//-- connect to the South Africa commerce server
	strcpy(accountInfo.szSMSAPIUsername,lpszUsername);		//-- set the bulksms account username
	strcpy(accountInfo.szSMSAPIPassword,lpszPassword);		//-- set the bulksms account password	
	
	batchInfo.lpBatchItemArray = lpArray;
	batchInfo.nBatchItemArrayItemCount = nArrayItemCount;
		
	if (sms_sendbatch(&networkConfiguration,&accountInfo,&batchInfo,&responseInfo))
	{
		nResult = 0;
		MessageBox(NULL,responseInfo.lpszResult,"",MB_OK);
	}
	else
	{
		nResult = responseInfo.nResult;
		MessageBox(NULL,responseInfo.lpszResult,"",MB_OK);
	}
	        	
	return nResult;
}

int SMSAPISimple_GetInbox(const char * lpszUsername, const char * lpszPassword, const int serverID, const unsigned long dwMessageID, char * szReportData, int nSize)
{
	int nResult = -1;

	SMSAPI_NETWORKCONFIGURATION networkConfiguration;		//-- defines server, port, secure etc.
	SMSAPI_ACCOUNTINFO accountInfo;						//-- your bulksms account info is specified here
	SMSAPI_RESPONSEINFO responseInfo;						//-- responses are returned in this structure

	//-- allocate a 1k buffer for the result
	responseInfo.lpszResult = szReportData;
	responseInfo.cbSizeOfResultBuffer = nSize;
	
	//-- fail is no buffer allocated
	if (responseInfo.cbSizeOfResultBuffer < 1)
		return -1;
	
	//-- fail if function pointers have not been initialized
	if (!sms_initialize_defaults || !sms_getreport)
		return -1;
		
	//-- initialize the structures. this nulls the entire structures and sets preferred default values
	sms_initialize_defaults(&networkConfiguration,sizeof(SMSAPI_NETWORKCONFIGURATION)); 
	sms_initialize_defaults(&accountInfo, sizeof(SMSAPI_ACCOUNTINFO));
	sms_initialize_defaults(&responseInfo, sizeof(SMSAPI_RESPONSEINFO));

	//-- set detailed specific information here
	networkConfiguration.nIdealPort = 80;					//-- use the standard HTTP internet port
	accountInfo.defaultServerID = (SMSAPI_SERVER) serverID;	//-- connect to the South Africa commerce server
	strcpy(accountInfo.szSMSAPIUsername,lpszUsername);		//-- set the bulksms account username
	strcpy(accountInfo.szSMSAPIPassword,lpszPassword);		//-- set the bulksms account password	

	if (sms_getinbox(&networkConfiguration,&accountInfo,dwMessageID,&responseInfo))
	{
		nResult = 0;
		MessageBox(NULL,responseInfo.lpszResult,"",MB_OK);
	}
	else
	{
		nResult = responseInfo.nResult;
		MessageBox(NULL,responseInfo.lpszResult,"",MB_OK);
	}
	        	
	return nResult;
}

int SMSAPISimple_GetCredits(const char * lpszUsername, const char * lpszPassword, const int serverID, char * szReportData, int nSize)
{
	int nResult = -1;

	SMSAPI_NETWORKCONFIGURATION networkConfiguration;		//-- defines server, port, secure etc.
	SMSAPI_ACCOUNTINFO accountInfo;						//-- your bulksms account info is specified here
	SMSAPI_RESPONSEINFO responseInfo;						//-- responses are returned in this structure

	//-- allocate a 1k buffer for the result
	responseInfo.lpszResult = szReportData;
	responseInfo.cbSizeOfResultBuffer = nSize;
	
	//-- fail is no buffer allocated
	if (responseInfo.cbSizeOfResultBuffer < 1)
		return -1;
	
	//-- fail if function pointers have not been initialized
	if (!sms_initialize_defaults || !sms_getreport)
		return -1;
		
	//-- initialize the structures. this nulls the entire structures and sets preferred default values
	sms_initialize_defaults(&networkConfiguration,sizeof(SMSAPI_NETWORKCONFIGURATION)); 
	sms_initialize_defaults(&accountInfo, sizeof(SMSAPI_ACCOUNTINFO));
	sms_initialize_defaults(&responseInfo, sizeof(SMSAPI_RESPONSEINFO));

	//-- set detailed specific information here
	networkConfiguration.nIdealPort = 80;					//-- use the standard HTTP internet port
	accountInfo.defaultServerID = (SMSAPI_SERVER) serverID;	//-- connect to the South Africa commerce server
	strcpy(accountInfo.szSMSAPIUsername,lpszUsername);		//-- set the bulksms account username
	strcpy(accountInfo.szSMSAPIPassword,lpszPassword);		//-- set the bulksms account password	

	if (sms_getcredits(&networkConfiguration,&accountInfo,&responseInfo))
	{
		nResult = 0;
		MessageBox(NULL,responseInfo.lpszResult,"",MB_OK);
	}
	else
	{
		nResult = responseInfo.nResult;
		MessageBox(NULL,responseInfo.lpszResult,"",MB_OK);
	}
	        	
	return nResult;
}