#include <windows.h>
#include <iostream>

#include "..\..\..\..\pilot_nt.h"
#include "..\..\..\..\deprtmnt.h"
#include "..\..\..\..\cardtype.h"
#include "..\..\..\..\errors.h"

#ifndef WINCE
  #define DLL_NAME "pilot_nt.dll"
#else
  #define DLL_NAME L"pilot_nt.dll"
#endif

extern HMODULE LoadPilotNt(const char* name, const char** nameOfTheUncknownFunction);
extern void    FreePilotNt(HMODULE dll);

#ifndef WINCE
int main (int argc, char** argv)
#else
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd)
#endif
{
	const char *nameOfTheUncknownFunction = NULL;
	HMODULE dll = LoadPilotNt(DLL_NAME, &nameOfTheUncknownFunction);
	if (!dll)
	{
		if (!nameOfTheUncknownFunction)
		{
			std::cerr << "pilot_nt.dll not found";
			return -1;
		}
		else
		{
			std::cerr << "function " << nameOfTheUncknownFunction << " in " << DLL_NAME << " not found";
			return -2;
		}
	}

	CONTEXT_PTR ctx = NULL;
	try
	{
           auth_answer14 argument;
           DWORD amount = 10000;
           DWORD amountOther = 50000;
           DWORD amountFull = amount + amountOther;
           DWORD result;

           memset(&argument, 0, sizeof(argument));
           argument.ans.TType = OP_PUR_CASHBACK;
           argument.ans.Amount = amountFull;
           //allocate context
           CONTEXT_PTR ctx = ctxAlloc();
           ctxSetInt(ctx, PAR_AMOUNT_OTHER, amountOther);
           result = card_authorize15(NULL, &argument, NULL, ctx, 0);
           std::cout << "Operation completed with code '" << result << "'!" << std::endl;
           if (argument.ans.Check)
           {
             ::CharToOem(argument.ans.Check, argument.ans.Check);
             std::cout << argument.ans.Check << std::endl;
             ::GlobalFree(argument.ans.Check);
           }
	}
	catch (...)
	{
	}

	ctxFree(ctx);
	FreePilotNt(dll);

	return 0;
}
