#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 *nameOfTheUnknownFunction = NULL;
	HMODULE dll = LoadPilotNt(DLL_NAME, &nameOfTheUnknownFunction);
	if (!dll)
	{
		if (!nameOfTheUnknownFunction)
		{
			std::cerr << "pilot_nt.dll not found";
			return -1;
		}
		else
		{
			std::cerr << "function " << nameOfTheUnknownFunction << " in " << DLL_NAME << " not found";
			return -2;
		}
	}

	try
	{
		auth_answer14 argument;
		DWORD amount = 40000;
		DWORD error;

		memset(&argument, 0, sizeof(argument));
        argument.ans.TType = OP_PURCHASE;
		argument.ans.Amount = amount;

		error = card_authorize14(NULL, &argument, NULL);
		std::cout << "Operation completed with code '" << error << "'!" << std::endl;

        StatusRequestQuery query;
        query.size = sizeof(StatusRequestQuery);
        query.RequestId = argument.RequestID;
        //Status request without cheque
        StatusRequestResult result;
        error = GetOperationStatusByRequestId(&query, NULL);
        //Status request with cheque
        result.size = sizeof(StatusRequestResult);
        result.Cheque = NULL;
        error = GetOperationStatusByRequestId(&query, &result);
        if (result.Cheque)
        {
          // print cheque
          GlobalFree(result.Cheque);
        }

	}
	catch (...)
	{
	}

	FreePilotNt(dll);

	return 0;
}
