// #define PRUEBAS_SIN_VCL

//---------------------------------------------------------------------------

#ifndef LM2000H
#define LM2000H
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
/** Estructura que define a una tarjeta */
typedef struct
{
  int FLG;			// Enabling flag
  int TYP;			// Card Type
  int CDA;			// Card Code
  int PIN;			// PIN
  int GIS;			// Expiration day
  int MES;			// Expiration month
  int ANS;			// Expiration year
  int ORS;			// Expiration hour
  int MIS;			// Expiration minute
  int SES;			// Expiration second
  int REV;			// Revision
  int CRD;			// Credit
} TCard;

/** Estructura que define el modo de lectura/escritura */
typedef struct
{
  int FMC;	//	Card memory flag
  int MAC;	//
  int LFN;	//	Operation logic
  int NCR;	//	Cards in memory
  int CMN;	//	Minimum code
  int CMX;	//	Maximum code
  int NBN;	//	Number of bands
  int NMP;	//	Number of messages
  int NFA;	//	Number of periods
  int RMS;	//	Lines in messages
  int MCA;	//	Motive mode
  int MCR;	//	Credit mode
} RMem;

/** Program Operation Parameters */
typedef struct
{
	int BKN;			// Network block
	int BKP;			// Printer block
	int PRN;			// Print mode
	int MCL;			// Mode for requesting information from P.C.
	int MCK;			// Card control mode
	int ENF;			// Enabling of keyboard functions
	int SNV;			// Anomaly memorization mode
	int ENI;			// Number of intrusions per apparatus block
} POP;

/** Configuracion de fecha/hora */
typedef struct
{
	int GIA;			// Day
	int MEA;			// Month
	int ANA;			// Year
	int ORA;			// Hour
	int MIA;			// Minute
	int SEA;			// Second
} RClock;

/** Estructura TSD */
typedef struct
{
	int CHN;
	int GSL;
	int MMS;
	int ASL;
	int HSL;
	int MSL;
	int SSL;
} TSD;

/** Caracteres en la Banda */
typedef struct
{
 int GAP;  // Number of empty characters preceding prefix
 int LEP;  // Length of prefix in characters
 int GAC;  // Number of empty characters preceding code
 int LEC;  // Length of code in characters
 int GAR;  // Number of empty characters preceding revision
 int LER;  // Number of characters containing number of revision
 int GDI;  // Number of empty characters preceding validity start date
 int GDF;  // Number of empty characters preceding validity end date
} CAB;

/** LM2000
    Clase que encapsula las funciones necesarias para comunicarse con el LM2000
*/
#ifdef PRUEBAS_SIN_VCL
class PACKAGE TLm2000 
#else
class PACKAGE TLm2000 : public TComponent
#endif
{
private:
       int __fastcall GeneraChecksum( char *cadena );
       char * __fastcall ObtenerRespuesta( char *rcv );
       char * __fastcall EnviarComando( char *CMD, int ID, char *rcv );
       char * __fastcall ObtenerParametro( char *cadena, int narg, char *prm );
       bool __fastcall ProcesaTarjeta( int ID, char *msg );

        /** Manejador que apunta al Puerto donde dialogamos */
        HANDLE LM_handle;
        /** Indica si estamos en comunicación actualmente */
        bool LM_opened;

        void __fastcall ProcesaTransito( char *FGT, char *TPT, char *CDT, char *O, char *DTT, char *CTT, char *ETT );

#ifndef PRUEBAS_SIN_VCL
        typedef bool __fastcall (__closure *Tprintfv)( AnsiString msg );
        typedef bool __fastcall (__closure *TBuscaTarjeta)( int ID, int FGA, int TPA, int CDA, int *TPR, int *PNR, char *message );
        typedef void __fastcall (__closure *TOnProcesaTransito)( char *FGT, char *TPT, char *CDT, char *O, char *DTT, char *CTT, char *ETT );
        Tprintfv                printfv;
        TBuscaTarjeta           FOnBuscaTarjeta;
        TOnProcesaTransito      FOnProcesaTransito;
#else
        void __fastcall printfv( AnsiString msg );
#endif

protected:
public:
#ifdef PRUEBAS_SIN_VCL
       __fastcall TLm2000(void);
#else
       __fastcall TLm2000(TComponent* Owner);
#endif
       __fastcall  ~TLm2000(void);

       void __fastcall CierraPuerto(void);
       int __fastcall AbrePuerto(LPTSTR lpszPortName);

       /** El 'polling' debe llamar continuamente a esta función para conocer
           el estado de los dispositivos 'ID' */
       char * __fastcall SimpleInterrogation( int ID, char *rcv );

       // LECTURA DE MEMORIA
       bool   __fastcall ReadCardMemory( int ID, TCard *CRD );
       void   __fastcall ReadEntireCardMemory( int ID );
       // ESCRITURA DE MEMORIA
       int    __fastcall EnterCardMemory( int ID, TCard *CRD );
       int    __fastcall DeleteCardMemory( int ID, int TIP, int COD );
       int    __fastcall DeleteAllCardMemory( int ID, int TIP );
       // CONFIGURACION LM2000
       bool   __fastcall ConfigureMemory( int ID, RMem *mem );
       RMem * __fastcall ReadMemoryCFG( int ID, RMem *mem );
       bool   __fastcall ProgrammingPeriods( int ID, int NFS, int PBN, char *StarTime, char *EndTime, int GIF );
       bool   __fastcall ResetClock( int ID, RClock *clock );
       int    __fastcall ReadingPrefixes( int ID, int NPR );
       bool   __fastcall ReadPersonalMessage( int ID );
       void   __fastcall ReadAllPersonalMessage( int ID );
       void   __fastcall SetTime( int ID, TSD *tsd );
       char * __fastcall ReadSystemMessages( int ID, int IDmsg, char *rcv );
       bool   __fastcall ProgramOperationParameters( int ID, POP *pop );
       bool   __fastcall SetCharsOnBand( int ID, CAB *cab );
       bool   __fastcall GetCharsOnBand( int ID, CAB *cab );
       bool   __fastcall PassageOpening( int ID, int Passage );

       /* Llamadas que deben ser sobrecargadas CALLBACK*/
//       void __fastcall ProcesaTransito( char *FGT, char *TPT, char *CDT, char *O, char *DTT, char *CTT, char *ETT );
//       bool __fastcall BuscaTarjeta( int FGA, int TPA, int CDA, int *TPR, int *PNR, char *message );
#ifndef PRUEBAS_SIN_VCL
__published:
       __property Tprintfv              OnVerboseMessage = {read=printfv, write=printfv };
       __property TOnProcesaTransito    OnProcesaTransito = {read=FOnProcesaTransito, write=FOnProcesaTransito };
       __property TBuscaTarjeta         OnBuscaTarjeta = {read=FOnBuscaTarjeta, write=FOnBuscaTarjeta };
#endif
};
//---------------------------------------------------------------------------
#endif
 