#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 call_pilot_command(Gate_Call_Sb_Kernel function, char* command)
{
  InArg in;
  OutArg out;
  memset(&in, 0, sizeof(in));
  in.Reserved = command;
  DWORD error = function(KO_CALL_PILOT_COMMAND, &in, &out);
  throw_if_error(error, "call_pilot_command");
  throw_if_error(out.ErrorCode, "call_pilot_command");
}

void perform_sale(Gate_Call_Sb_Kernel function, DWORD amount, bool* hasMonitoringMessage)
{
  InArg in;
  OutArg out;
  struct_in_cl4 InDetail;
  struct_out_cl4 OutDetail;
  memset(&in, 0, sizeof(in));
  memset(&InDetail, 0, sizeof(InDetail));
  in.in_struct = &InDetail;
  memset(&out, 0, sizeof(out));
  memset(&OutDetail, 0, sizeof(OutDetail));
  out.out_struct = &OutDetail;

  InDetail.Amount = amount;

  DWORD error = function(KO_SALE,&in,&out);
  throw_if_error(error, "perform_sale");
  throw_if_error(out.ErrorCode, "perform_sale");
  if (hasMonitoringMessage)
    *hasMonitoringMessage = (out.Flags&F_HAVE_MONMSG) == F_HAVE_MONMSG;
}

std::string read_monitoring_message(Gate_Call_Sb_Kernel function, bool& isUrgentMessage)
{
  InArg in;
  OutArg out;
  tag_struct_in22 InDetail;
  tag_struct_out22 OutDetail;
  memset(&in, 0, sizeof(in));
  memset(&InDetail, 0, sizeof(InDetail));
  in.in_struct = &InDetail;
  memset(&out, 0, sizeof(out));
  memset(&OutDetail, 0, sizeof(OutDetail));
  out.out_struct = &OutDetail;
  isUrgentMessage = false;

  InDetail.bErase = true;

  DWORD error = function(KO_GET_MONITORING_STATE,&in,&out);
  throw_if_error(error, "read_monitoring_message");
  if (out.ErrorCode == ERR_PIL_NO_FILES)
    return std::string();
  throw_if_error(out.ErrorCode, "read_monitoring_message");

  isUrgentMessage = OutDetail.mesState == 1;
  return *OutDetail.msg ? std::string(OutDetail.msg) : std::string();
}

#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);

    std::string hash;
    const DWORD STO_RUBLEY = 10000;
    bool hasMonitoringMessage = false;

    call_pilot_command(gate_Call_Sb_Kernel, " 47 2"); // 1. ask psdb state

    perform_sale(gate_Call_Sb_Kernel, STO_RUBLEY, &hasMonitoringMessage); //2. perform some operation and catch message mark
    if (hasMonitoringMessage) { //3. if message mask found
      bool isUrgentMessage = false;
      std::string msg = read_monitoring_message(gate_Call_Sb_Kernel, isUrgentMessage); //4. read mesage from disk
      if (isUrgentMessage)
        std::cout << "Is urgent monitoring message!" << std::endl;
      std::cout << "Monitoring message: " << msg.c_str() << std::endl;
    }

    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;
}
