/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
	Description:
	Ces classes contiennent l'état de la Zone Tests et les informations nécessaires
	au fonctionnement des machines à états.
*/

/* ************************************************************************** */
/* Revision:
### 20130501 JFM
	Verision d'origine.
	
### YYYYMMDD Description du besoin ou du bug
	Description du changement.
 */

/* ************************************************************************** */

#ifndef ZTDATA_H
#define ZTDATA_H

#include "GlobalDefine.h"
#include <QObject>
#include <QTime>
#include <QDataStream>
#include <QVector>
#include <QHostAddress>

enum eZTLOGType
{
    ZT1_LOG_TYPE,
    ZT2_LOG_TYPE,
    UNKNOWN_LOG_TYPE
};

enum eCDVState
{
	CDV_STATE_OCCUPIED,
	CDV_STATE_FREE,
	CDV_STATE_ITI_CMD,
	CDV_STATE_UNKNOWN
};

enum eSwitchCVDPosition
{
	SWITCH_CDV_NORMAL_POSITION,
	SWITCH_CDV_REVERSE_POSITION,
	SWITCH_CDV_UNKNOWN_POSITION
};

enum eZTITI
{
    ZT_PRIMARY_ITI,   // Itinéraire 10/12 ou en voie normale
    ZT_SECONDARY_ITI,   // Itinéraire 10/22 ou autre changement de voie (quai banalisé)
    ZT_MAX_ITI
};

enum eCDVType
{
	CDV_NORMAL_TYPE,
	CDV_SWITCH_TYPE
};

enum eDetectionID
{
	DETECTION_MAGNETIC_SENSOR_COUNT,
	DETECTION_FN_DETECTION,
	DETECTION_PG_DETECTION,
	DETECTION_PPI_DETECTION,
	DETECTION_PPE_DETECTION,

    DETECTION_ZT2_MAGNETIC_SENSOR_COUNT,
    DETECTION_ZT2_PPI_DETECTION,
    DETECTION_ZT2_PPE_DETECTION,

	DETECTION_PEQ1_DETECTION,
	DETECTION_PEQ2_DETECTION,

	DETECTION_MAX_DETECTION_ID
};

enum eDetectionFunctionID
{
	DETECTION_FCT_FN,
	DETECTION_FCT_PG,
	DETECTION_FCT_PP,
	DETECTION_FCT_PP2,
    DETECTION_FCT_ZT1,
    DETECTION_FCT_ZT2,

	DETECTION_FCT_MAX_FCT
};

enum eDetectionFunctionMask
{
    DETECTION_FCT_FN_MASK = 0x00000001,
    DETECTION_FCT_PG_MASK = 0x00000002,
    DETECTION_FCT_PP_MASK = 0x00000004,
    DETECTION_FCT_PP2_MASK = 0x00000008,
    DETECTION_FCT_ZT1_MASK = 0x00000010,
    DETECTION_FCT_ZT2_MASK = 0x00000020
};

enum ePEQ1Types
{
    PEQ1_UNKNOWN_TYPE,
    PEQ1_FN_PREDETECTION_TYPE,
    PEQ1_PG_PREDETECTION_TYPE,
    PEQ1_PINT_PREDETECTION_TYPE,
    PEQ1_PEXT_PREDETECTION_TYPE,
    PEQ1_MAG_SENSOR_S1_PREDETECTION_TYPE,
    PEQ1_MAG_SENSOR_S2_PREDETECTION_TYPE,
    PEQ1_LAZER_SENSOR_INT_PREDETECTION_TYPE,
    PEQ1_LAZER_SENSOR_EXT_PREDETECTION_TYPE,
    PEQ1_FN_STUCK_LOW_TYPE,

    PEQ1_MAX_TYPE
};

enum eTrainTypeID
{
	TRAIN_TYPE_UNKNOWN,
	TRAIN_TYPE_MR63_MR73,
	TRAIN_TYPE_MPM10,
    TRAIN_TYPE_MR63,
    TRAIN_TYPE_MR73,
    TRAIN_TYPE_MAINTENANCE_VEHICLE,


	TRAIN_TYPE_MAX_ID
};

enum eZT1FlagsID
{
	ZT1_INT_PG_OFFSET_FLAG_ID,
	ZT1_EXT_PG_OFFSET_FLAG_ID,
	ZT1_PG_TRESHOLD_VALUE_FLAG_ID,
	ZT1_PG_CALIB_ON_FLAG_ID,
    ZT1_PEQ_TYPE_FLAG_ID,


	ZT1_MAX_FLAG_ID
};

enum eZTTypeID
{
    ZT1_TYPE_ID,
    ZT2_TYPE_ID,
    ZTGLOBAL_TYPE_ID,

    ZT_MAX_TYPE_ID
};

class CZTData
{
public:
	CZTData();



	unsigned int mCIZT2;

	static const char *ErrorString[];
    static const char *ShortErrorString[];
	static const char* GetErrorString(unsigned int ErrorID);
	static const char* GetShortErrorString(unsigned int ErrorID);

	static const char *TrainTypeString[];
	static const char *GetTrainTypeString(unsigned int TrainType);

    static const char *GetModbusTrainTypeString(unsigned int TrainType);

};

//This class contains data from the ZT1 analysis thread
//that is periodically communicated to the main thread
class CZT1ThreadData
{

public:
	~CZT1ThreadData();
	qint64 mTimeStamp;	//nanosecs
	QDateTime mDateTime;
	quint32 mS1;
	quint32 mS2;
	quint32 mPInt;
	quint32 mPExt;
	quint32 mFN;
	quint32 mPG;

	quint32 mBogie;
	quint32 mRank;
	quint32 mS1Count;
	quint32 mS2Count;
	quint32 mFNCount;
	qint32 mPGExtValue;
	qint32 mPGIntValue;
	qreal mTrainSpeed;
	quint32 mTrainType;

	void operator=(CZT1ThreadData& a);
};

class CZT1FlagsData
{
public:
	qint32 mIntPGOffset;
	qint32 mExtPGOffset;
	qint32 mPGTresholdValue;
	qint32 mPGCalibrationON;
	qint32 mAnalogTracePresent;
    qint32 mIsProblematicPassage;   //This flag will be set if problems have been detected but not sent to PCC due to PURE comitee rules.
    qint32 mPEQ1Type;   //Not saved in passage files...
    qint32 mTrainCompo1;
    qint32 mTrainCompo2;
    qint32 mTrainCompo3;
    qint32 mModbusTrainType;
    qint32 mZT1ITI;
    qint32 mZTDetectionsConfig;
    qint32 mUnusedFlag14;
    qint32 mUnusedFlag15;
    qint32 mUnusedFlag16;
    qint32 mUnusedFlag17;
    qint32 mUnusedFlag18;
    qint32 mUnusedFlag19;
    qint32 mUnusedFlag20;
};
QDataStream &operator<<(QDataStream &out, const CZT1FlagsData &source);
QDataStream &operator>>(QDataStream &in, CZT1FlagsData &dest);

class CZT2FlagsData
{
public:
    qint32 mIsProblematicPassage;   //This flag will be set if problems have been detected but not sent to PCC due to PURE comitee rules.
    qint32 mTrainCompo1;
    qint32 mTrainCompo2;
    qint32 mTrainCompo3;
    qint32 mModbusTrainType;
    qint32 mZTDetectionsConfig;
    qint32 mUnusedFlag7;
    qint32 mUnusedFlag8;
    qint32 mUnusedFlag9;
    qint32 mUnusedFlag10;
};
QDataStream &operator<<(QDataStream &out, const CZT2FlagsData &source);
QDataStream &operator>>(QDataStream &in, CZT2FlagsData &dest);


class CZT1LogData
{
public:
	CZT1LogData(CZT1ThreadData *ThreadData = 0);
	~CZT1LogData();

	QDateTime mDateTime;
	qint64 mTimestamp;
	quint32 mCIZT1;
	quint32 mCDVApproach_ZT1;
	quint32 mCDVARM_ZT1;
	qint32 mAnalogData;		//The analog data must be written or read to/from file manually (not part of the QStream << operator overloading)

	CZT1ThreadData *mZT1ThreadData;
};

class CZT1Log
{
public:
	QVector<CZT1LogData*> mZT1LogData;
	CZT1FlagsData mZT1Flags;
};

QDataStream &operator<<(QDataStream &out, const CZT1LogData &source);
QDataStream &operator>>(QDataStream &in, CZT1LogData &dest);

class CZT2ThreadData
{
public:
	qint64 mTimeStamp;
	quint32 mS1;
	quint32 mPPInt;
	quint32 mPPExt;

    quint32 mBogie;
    quint32 mRank;
    quint32 mS1Count;
};

class CZTDetectionData
{
public:

    CZTDetectionData();
    qint64 mTimeStamp;
//	QDateTime mDateTime;
	quint32 mDetectionID;
	quint32 mRank;
//	quint32 mBogie;

   void operator=(const CZTDetectionData &in);

};
QDataStream &operator<<(QDataStream &out, const CZTDetectionData &source);
QDataStream &operator>>(QDataStream &in, CZTDetectionData &dest);




class CZT2LogData
{
public:
    CZT2LogData(CZT2ThreadData *ThreadData = 0);
    ~CZT2LogData();

    QDateTime mDateTime;
    qint64 mTimestamp;
    quint32 mCIZT2;
    quint32 mCDVARM_ZT2;
	quint32 mCDVApproach_ZT2;

    CZT2ThreadData *mZT2ThreadData;
};
QDataStream &operator<<(QDataStream &out, const CZT2LogData &source);
QDataStream &operator>>(QDataStream &in, CZT2LogData &dest);

class CZT2Log
{
public:
    QVector<CZT2LogData *> mZT2LogData;
    CZT2FlagsData mZT2Flags;
};

class CZTDetectionConfig
{
public:
	bool AnalysisActive;
	bool TKActive;
};

class CZTDetectionFunctionConfig
{
public:
	CZTDetectionFunctionConfig();
	CZTDetectionConfig mZTDetectionConfig[DETECTION_FCT_MAX_FCT];

	void operator=(CZTDetectionFunctionConfig& a);
    qint32 GetDetectionsConfigMask();

};

QDataStream &operator<<(QDataStream &out, const CZTDetectionFunctionConfig &source);
QDataStream &operator>>(QDataStream &in, CZTDetectionFunctionConfig &dest);

class CZTSettingsData
{
    public:
    CZTSettingsData();

    CZTDetectionFunctionConfig *mDetectionFunctionSettings;
	bool mAutoExportZT1CSV;
	bool mAutoExportZT2CSV;
	int mPGTreshold;
    int mMaxLogCount;
    bool mKeepMPM10Logs;
    bool mKeepAllZT1Logs;
    bool mKeepAllZT2Logs;
    bool mUseNetworkTime;
    bool mUseModbusCC;
    QHostAddress mModbusCCHostAddress;
    QHostAddress mSEIModbusHostAddress;



};

class CZTPGCalibrationData
{
	public:
	qint64 mTimestamp;
	int mTrainNbr;
	qint32 mPGExtValue;
	qint32 mPGIntValue;
};

class CZTPassageInfo
{
public:
    QVector<CZTDetectionData*> *mDetections;
    quint16 mNbPassages;
    quint16 mNbDeclenchements;
    qint16 mTrainCompo1;
    qint16 mTrainCompo2;
    qint16 mTrainCompo3;
    qint16 mTrainType;
    QDateTime mPassageDateTime;

};

#endif // ZTDATA_H
