#ifndef DISCRETETKTRANSPORT_H
#define DISCRETETKTRANSPORT_H

#include "TKTransportInterface.h"
#include "OutputModule.h"
#include "Station.h"
#include <QTimer>
#include "TKGenerator.h"

#define TK_GENERATOR_SHOW_ZERO_TIMEOUT 1000 //the Low state time between two TKs
#define TK_GENERATOR_AUTOMATIC_SHOW_TK_TIMEOUT 2000 //the amount of time the TK must be emited in auto acquitment mode
#define TK_GENERATOR_MAX_ALARMS 4
#define TK_GENERATOR_MAX_PEQ 1
#define TK_GENERATOR_MAX_V00X 1


class CDiscreteTKTransport : public CTKTransportInterface
{
    Q_OBJECT
public:

    enum eTKGeneratorStaes
    {
        TK_ZT1_GENERATOR_SHOW_TK_STATE,
        TK_ZT1_GENERATOR_WAIT_FOR_AN1_STATE,
        TK_ZT1_GENERATOR_SHOW_ZERO_STATE,

        TK_ZT1_GENERATOR_AUTO_SHOW_TK_AUTO_STATE,
        TK_ZT1_GENERATOR_AUTO_WAIT_FOR_TIMEOUT_STATE,
        TK_ZT1_GENERATOR_AUTO_SHOW_ZERO_STATE,

        TK_ZT1_GENERATOR_FINISH_STATE,

        TK_GENERATOR_ZT1_STANDBY_STATE,

        TK_ZT2_GENERATOR_SHOW_TK_STATE,
        TK_ZT2_GENERATOR_WAIT_FOR_AN2_STATE,
        TK_ZT2_GENERATOR_SHOW_ZERO_STATE,

        TK_ZT2_GENERATOR_AUTO_SHOW_TK_AUTO_STATE,
        TK_ZT2_GENERATOR_AUTO_WAIT_FOR_TIMEOUT_STATE,
        TK_ZT2_GENERATOR_AUTO_SHOW_ZERO_STATE,

        TK_ZT2_GENERATOR_FINISH_STATE,

        TK_GENERATOR_ZT2_STANDBY_STATE
    };

    CDiscreteTKTransport();
    ~CDiscreteTKTransport();


    int SendTKToCC(CZTDetectionData TKData);
    int ClearTK(int ZT);

    void BindPointers(GenericOutputMasks_t *OutputMasks, COutputModule *OutputModule);
    unsigned int CancelAllTK();
    unsigned int CancelMaintenanceCurrentTK();
    unsigned int ExitMaintenance();
    unsigned int EnterMaintenance();
    unsigned int SetInputStates(bool AN1State, bool ZT1CDVState, bool AN2State, bool ZT2CDVState);
     unsigned int BeginTKEmission();
     virtual unsigned int ClearTKForNewTrain();

    bool IsTKProcessing();
    bool IsZT1TKProcessing();
    bool IsZT2TKProcessing();
    int AddNewZT1Detection(CZTDetectionData Detection);
    int AddNewZT2Detection(CZTDetectionData Detection);

private:
    GenericOutputMasks_t *mOutputMasks;

    COutputModule *mOutputModule;

    QList<CZTDetectionData> mZT1TKDataList,mZT2TKDataList;
    unsigned int mZT1TKGeneratorState,mZT2TKGeneratorState;
    int mZT1CurTKIndex,mZT2CurTKIndex;
    QTimer *mZT1TKStateMachineTimer;
    QTimer *mZT2TKStateMachineTimer;
    QElapsedTimer mZT1RefTimer,mZT2RefTimer;

    bool mAN1State, mZT1CDVState,mAN2State,mZT2CDVState;
    bool mMaintenanceMode, mForceZT1Clear, mForceZT2Clear;

    quint32 mClearZT1AlarmMask, mClearZT2AlarmMask;

    quint32 IntegerToBCDZT1(quint32 Value);
    quint32 IntegerToBCDZT2(quint32 Value);
    quint32 IntegerToBCDFN(quint32 Value);

    //void LogTK(quint32 Value);
   // void LogTK(CZTDetectionData DetectionData);


public slots:
    void ExecZT1SM();
    void ExecZT2SM();

};

#endif // DISCRETETKTRANSPORT_H
