#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  hex_2_ascii(const BYTE *source,BYTE *destn, WORD sz)
{
  BYTE  a, c;
  WORD i,j;

  for(i=0,j=0;sz--;){
    c = source[j];
    a = (BYTE)(c>>4);
    if (a <= 9)
      destn[i++] = (BYTE)(a + '0');
    else
      destn[i++] = (BYTE)(a + '7');

    c = source[j++];
    a = (BYTE)(c&0x0f);
    if (a <= 9)
      destn[i++] = (BYTE)(a + '0');
    else
    destn[i++] = (BYTE)(a + '7');
  }
  destn[i] = '\0';
}

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 read_card_hash(Gate_Call_Sb_Kernel function, std::string& hash)
{
  InArg in;
  OutArg out;
  struct_in_cl5 m_in;
  struct_out_cl5 m_out;
  struct_in_reserved5  m_res;
  struct_out_reserved5 out_res;

  memset(&in,0,sizeof(in));
  memset(&m_in,0,sizeof(m_in));
  memset(&m_res,0,sizeof(m_res));
  in.in_struct = &m_in;
  in.Reserved = &m_res;
  m_res.size = sizeof(m_res);

  memset(&out,0,sizeof(out));
  memset(&m_out,0,sizeof(m_out));
  memset(&out_res,0,sizeof(out_res));
  out.out_struct = &m_out;
  out.Reserved = &out_res;
  out_res.size = sizeof(out_res);
  std::cout << "Let's read hash!" << std::endl;
  DWORD error = function(5002,&in,&out);
  throw_if_error(error, "read_card_hash");
  throw_if_error(out.ErrorCode, "read_card_hash");


  hash.resize(40);
  hex_2_ascii(out_res.Hash, (BYTE*)&hash[0], sizeof(out_res.Hash));

  std::cout << "Hash: '" << hash.c_str() << "'"<< std::endl;

}

void pay_with_hash(Gate_Call_Sb_Kernel function, DWORD amount, const std::string& hash)
{
  InArg in;
  OutArg out;
  struct_in_cl4 m_in;
  struct_out_cl4 m_out;
  struct_in_reserved4  inExtra;
  struct_out_reserved4 outExtra;

  memset(&in,0,sizeof(in));
  memset(&m_in,0,sizeof(m_in));
  memset(&inExtra,0,sizeof(inExtra));
  in.in_struct = &m_in;
  in.Reserved = &inExtra;
  inExtra.size = sizeof(inExtra);

  memset(&out,0,sizeof(out));
  memset(&m_out,0,sizeof(m_out));
  memset(&outExtra,0,sizeof(outExtra));
  out.out_struct = &m_out;
  out.Reserved = &outExtra;
  outExtra.size = sizeof(outExtra);

  std::cout << "Let's pay with hash '" << hash.c_str() <<  "'!" << std::endl;
  inExtra.Department = DEPARTMENT_NOT_SPECIFIED;
  m_in.Amount = amount;
  strcpy_s(m_in.Track2, hash.c_str());
  DWORD error = function(4000,&in,&out);

  throw_if_error(error, "pay_with_hash");
  throw_if_error(out.ErrorCode, "pay_with_hash");
}

#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;
    read_card_hash(gate_Call_Sb_Kernel, hash);
    pay_with_hash(gate_Call_Sb_Kernel, STO_RUBLEY, hash);
    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;
}
