#ifndef TCPPROTOCOL_H
#define TCPPROTOCOL_H

#include "GlobalDefine.h"
#include <QByteArray>
#include <QDateTime>

#define TCP_PROTOCOL_HEADER (quint32)0xDEADCAFE

class CTCPZTStatus
{
public:
    quint32 mZT1Status; //
    quint32 mZT2Status;
    quint32 mNbPass;
    quint32 mNbTrigs;

    QDateTime mZTStartDateTime;
    QDateTime mActualDateTime;

    qint32 mPGTreshold;

    bool mModeMaintenanceON;
    bool mCalibModeON;

    bool mFNTKActive;
    bool mFNAnalysisActive;
    bool mPGTKActive;
    bool mPGAnalysisActive;
    bool mPP1TKActive;
    bool mPP1AnalysisActive;
    bool mPP2TKActive;
    bool mPP2AnalysisActive;
    bool mZT1TKActive;
    bool mZT1AnalysisActive;
    bool mZT2TKActive;
    bool mZT2AnalysisActive;

    bool mUSBKeyConnected;
};

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

class CTCPProtocol
{
    public:

    typedef enum
    {
        TCP_MSG_HEARTBEAT_REQUEST,
        TCP_MSG_HEARTBEAT_RESPONSE,
        TCP_MSG_STATION_NAME_REQUEST,
        TCP_MSG_STATION_NAME_RESPONSE,
        TCP_MSG_ZT_STATUS_REQUEST,
        TCP_MSG_ZT_STATUS_RESPONSE,
        TCP_MSG_ZTLOG_DOWNLOAD_REQUEST,
        TCP_MSG_ZTLOG_DOWNLOAD_RESPONSE,

        TCP_MSG_TRAINLOGS_DOWNLOAD_REQUEST,
        TCP_MSG_TRAINSLOGS_DOWNLOAD_ACK,
        TCP_MSG_TRAINLOG_FILE_DATA,
        TCP_MSG_TRAINLOG_FILE_DATA_ACK,
        TCP_MSG_TRAINLOGS_DOWNLOAD_FINISHED,
        TCP_MSG_DELETE_ZTLOG_REQUEST,
        TCP_MSG_DELETE_ZTLOG_ACK,
        TCP_MSG_SET_ZT_FUNCTIONS,
        TCP_MSG_SET_ZT_FUNCTIONS_ACK,

        TCP_MSG_UNKNOWN
    }eTCPMessage;

    typedef enum
    {
        TCP_PROTOCOL_GET_HEADER_STATE,
        TCP_PROTOCOL_GET_MSG_STATE,
        TCP_PROTOCOL_GET_DATASIZE_STATE,
        TCP_PROTOCOL_GET_DATA_STATE
    }eTCPProtocolState;

    typedef enum
        {
            TCP_PROTOCOL_NACK,
            TCP_PROTOCOL_ACK,
            TCP_PROTOCOL_NACK_ABORT,
            TCP_PROTOCOL_INVALID_ACK
        }eTCPProtocolAcknowledge;

    CTCPProtocol();

    virtual void TCPTxRequest(QByteArray request) = 0;

    unsigned int AnalyzeNewData(QByteArray data);

    QByteArray GetHeartbeatPacket();
    QByteArray GetStationNamePacket(QString StationName);
    QByteArray GetZTStatusPacket(CTCPZTStatus *Status);
    QByteArray GetZTLogPacket(QString *ZTLog);
    QByteArray GetTrainLogsDownloadAckPacket(qint32 FileCount);
    QByteArray *GetTrainLogDataPacket(const QByteArray &FileData);  //MUST BE DELETED BY CALLER
    QByteArray GetTrainLogsDownloadFinishedPacket();
    QByteArray GetDeleteZTLogsAckPacket(qint32 Acknowledge);
    QByteArray GetSetZTFunctionsConfigAckPacket(qint32 Ack);


    virtual void TCPHeartbeatRequest() = 0;
    virtual void TCPStationNameRequest() = 0;
    virtual void TCPStatusRequest() = 0;
    virtual void TCPZTLogDownloadRequest() = 0;
    virtual void TCPTrainLogsDownloadRequest() = 0;
    virtual void TCPTrainLogFileDataAck(eTCPProtocolAcknowledge Acknowledge) = 0;
    virtual void TCPDeleteZTLogRequest() = 0;
    virtual void TCPSetZTFunctionsConfigRequest(QByteArray &Data) = 0;





private:
    eTCPProtocolState mProtocolRxState;
    eTCPMessage mCurTransactionMsg;
    qint64 mCurTransactionDataSize;
    QByteArray mCurTransactionData;

    void ResetRxStateMachine();
    int DecodeTCPMessage();

};
class CTCPZTFunctionsStatus
{
public:
    bool mFNTKActive;
    bool mFNAnalysisActive;
    bool mPGTKActive;
    bool mPGAnalysisActive;
    bool mPP1TKActive;
    bool mPP1AnalysisActive;
    bool mPP2TKActive;
    bool mPP2AnalysisActive;
    bool mZT1TKActive;
    bool mZT1AnalysisActive;
    bool mZT2TKActive;
    bool mZT2AnalysisActive;
};
QDataStream &operator>>(QDataStream &in, CTCPZTFunctionsStatus &dest);
QDataStream &operator<<(QDataStream &out, const CTCPZTFunctionsStatus &source);


#endif // TCPPROTOCOL_H
