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

#include "..\..\..\..\deprtmnt.h"
#include "..\..\..\..\errors.h"
#include "..\..\..\..\gate.h"
#include "..\..\..\..\sbkernel.h"

#ifndef WINCE
  #define CALL_SB_KERNEL_NAME "_call_sb_kernel"
  #define DDL_NAME "gate.dll"
#else
  #define CALL_SB_KERNEL_NAME L"call_sb_kernel"
  #define DDL_NAME L"gate.dll"
#endif

  struct Error
  {
    DWORD Line;
    DWORD Code;
    std::string Text;

    Error(DWORD line, DWORD code, const char* text)
      : Line(line), Code(code), Text(text)
    {}
  };

#define throw_if_error(code, text) if (code) throw Error(__LINE__, code, text)

void initialize(Gate_Call_Sb_Kernel function)
{
  InArg in;
  OutArg out;
  struct_in0 InDetail;
  struct_out0 OutDetail;
  memset(&in, 0, sizeof(in));
  memset(&InDetail, 0, sizeof(InDetail));
  in.in_struct = &InDetail;
  InDetail.AppVersion = KERNEL_MINIMAL_VERSION;
  memset(&out, 0, sizeof(out));
  memset(&OutDetail, 0, sizeof(OutDetail));
  out.out_struct = &OutDetail;

  DWORD error = function(KO_INITIALIZATION, &in, &out);
  throw_if_error(error, "initialization");
  throw_if_error(out.ErrorCode, "initialization");
  std::cout << ((OutDetail.LibVersion/65536)&0xFF) << "." <<((OutDetail.LibVersion/256)&0xFF) << "." << (OutDetail.LibVersion&0xFF) << std::endl;
}

bool de_initialize(Gate_Call_Sb_Kernel function)
{
  InArg in;
  OutArg out;
  memset(&in,0,sizeof(in));
  memset(&out,0,sizeof(out));

  if(function && !function(1, &in, &out))
      return true;

  return false;
}

void complete_with_cheque(Gate_Call_Sb_Kernel function, DWORD requestId, char* cheque)
{
  InArg in;
  OutArg out;
  struct_in_reserved6 extraIn;
  memset(&in, 0, sizeof(in));
  memset(&extraIn, 0, sizeof(extraIn));
  extraIn.size = sizeof(extraIn);
  in.Reserved = &extraIn;
  extraIn.dwQueryRequestID = requestId;
  extraIn.MerchantCheque = cheque;
  DWORD error = function(KO_ADD_MERCHANT_CHEQUE, &in, &out);
  throw_if_error(error, "complete_with_cheque");
  throw_if_error(out.ErrorCode, "complete_with_cheque");
}

void complete_without_cheque(Gate_Call_Sb_Kernel function, DWORD requestId)
{
  InArg in;
  OutArg out;
  struct_in_reserved6 extraIn;
  memset(&in, 0, sizeof(in));
  memset(&extraIn, 0, sizeof(extraIn));
  extraIn.size = sizeof(extraIn);
  in.Reserved = &extraIn;
  extraIn.dwQueryRequestID = requestId;
  DWORD error = function(KO_ADD_MERCHANT_CHEQUE, &in, &out);
  throw_if_error(error, "complete_without_cheque");
  throw_if_error(out.ErrorCode, "complete_without_cheque");
}

void upload_cheques_to_host(Gate_Call_Sb_Kernel function)
{
  InArg in;
  OutArg out;
  memset(&in, 0, sizeof(in));
  DWORD error = function(KO_ADD_MERCHANT_CHEQUE, &in, &out);
  throw_if_error(error, "complete_without_cheque");
  throw_if_error(out.ErrorCode, "complete_without_cheque");
}

void perform_sale(Gate_Call_Sb_Kernel function, DWORD amount, DWORD& requestId)
{
  InArg in;
  OutArg out;
  struct_in_cl4 InDetail;
  struct_out_cl4 OutDetail;
  struct_out_reserved4 extraOut;
  memset(&in, 0, sizeof(in));
  memset(&InDetail, 0, sizeof(InDetail));
  in.in_struct = &InDetail;
  memset(&out, 0, sizeof(out));
  memset(&OutDetail, 0, sizeof(OutDetail));
  memset(&extraOut, 0, sizeof(extraOut));
  extraOut.size = sizeof(extraOut);
  out.out_struct = &OutDetail;
  out.Reserved = &extraOut;

  InDetail.Amount = amount;

  DWORD error = function(KO_SALE,&in,&out);
  requestId = extraOut.RequestID;
  throw_if_error(error, "perform_sale");
  throw_if_error(out.ErrorCode, "perform_sale");
}

#ifndef WINCE
int main (int argc, char** argv)
#else
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd)
#endif
{
  HMODULE dll = LoadLibrary(DDL_NAME);
  if (!dll) 
  {
    std::cerr << "gate.dll not found";
    return -1;
  }

  try {
    Gate_Call_Sb_Kernel gate_Call_Sb_Kernel = (Gate_Call_Sb_Kernel)GetProcAddress(dll, CALL_SB_KERNEL_NAME);
    if (!gate_Call_Sb_Kernel)
    {
      std::cerr << "function _call_sb_kernel in gate.dll not found";
      return -2;
    }

    initialize(gate_Call_Sb_Kernel);

    const DWORD STO_RUBLEY = 10000;
    DWORD requestId = 0;

    perform_sale(gate_Call_Sb_Kernel, STO_RUBLEY, requestId);
    complete_with_cheque(gate_Call_Sb_Kernel, requestId, "cheque data");
    requestId = 0;
    perform_sale(gate_Call_Sb_Kernel, STO_RUBLEY, requestId);
    complete_without_cheque(gate_Call_Sb_Kernel, requestId);

    upload_cheques_to_host(gate_Call_Sb_Kernel);

    de_initialize(gate_Call_Sb_Kernel);
  }
  catch(const Error& error)
  {
    std::cerr << "Error '" << error.Code << ":" << error.Text.c_str() << "' on line " << error.Line << std::endl;
  }

  FreeLibrary(dll);
  return 0;
}
