#include "../ozApi/ozApi.h"
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;

// dll description
HINSTANCE hDLL = NULL;

// This function unload ozApi.dll
void UnLoadOzekiApi()
{
	FreeLibrary((HMODULE)hDLL);
}

// This function load ozApi.dll
ozApi* LoadOzekiApi()
{
	HINSTANCE hDLL = NULL;
	SmsConnection conn;

	// Load ozApi.dll
	hDLL = LoadLibrary("ozApi.dll");

	if (hDLL != NULL) 
	{
		conn = (SmsConnection)GetProcAddress((HMODULE)hDLL, "CreateSmsConnection");
	    if (conn != NULL) return conn();
		UnLoadOzekiApi();
	}

	return NULL;
}


int main(int argc, char** argv)
{
	// Load ozApi dll and connect to it.
	ozApi* myConn = LoadOzekiApi();
	if (myConn == NULL)
	{
		// Error while connect to dll.
		cout << "Unable to load ozApi.dll" << endl;
		system("pause");
		return 0;
	}
	else
	{
		// Dll is loaded.
		cout << "Loaded" << endl;
	}

	// Connect to Ozeki NG SMS Gateway 
	// on localhost (127.0.0.1) at port 9500.
	myConn->open("127.0.0.1", 9500);

	// Login with default username and password
	// (admin / abc123)
	if(myConn->login("admin", "abc123"))
	{
		// Send a test message to 0011111
		myConn->sendMessage("0011111", "Test Message.");
	}
	else 
	{
		// If username/password is bad
		cout << "Bad usename or password" << endl;
	}


	// Closing connection.
	cout << "Closing connection..." << endl;
	myConn->close();

	// Unload ozApi.dll
	UnLoadOzekiApi();
	cout << "Done" << endl;
		
    return 0;
}

