#ifndef CLIENTTHREAD_H
#define CLIENTTHREAD_H

#include <QThread>
#include <QTcpSocket>
#include <QDateTime>
#include <QList>

class PData
{
public:
    explicit PData(quint16 chk=0, QByteArray data=nullptr){
        Data = data;
        Chk = chk;
    }
    quint16 check(){
        return Chk;
    }
    QByteArray data(){
        return Data;
    }
private:
    QByteArray Data;
    quint16 Chk;
};

class AppPData
{
public:
    explicit AppPData(QString id="", QString chksum="", int majro=0, int minro=0, uint prolen=0, uint time=0){
        PId = id;
        Checksum = chksum;
        MajroVersion = majro;
        MinroVersion = minro;
        ProgramLength = prolen;
        UpTime = time;
        DataList.clear();
    }
    void SetPId(QString id){
        PId = id;
    }
    void ClearData(){
        DataList.clear();
    }
    void AppendData(PData data){
        DataList.append(data);
    }
    QString AppId(){
        return PId;
    }
    QString chksum(){
        return Checksum;
    }
    int Size(){
        return DataList.length();
    }
    int majroVersion(){
        return MajroVersion;
    }
    int minroVersion(){
        return MinroVersion;
    }
    uint programLength(){
        return ProgramLength;
    }
    uint upTime(){
        return UpTime;
    }
    PData DataAt(int idx){
        if(idx<DataList.length())
            return DataList.at(idx);
        return PData(0,nullptr);
    }
private:
    QString PId;
    int MajroVersion, MinroVersion;
    uint ProgramLength, UpTime;
    QString Checksum;
    QList<PData> DataList;
};

class ClientThread : public QThread
{
    Q_OBJECT
public:
    explicit ClientThread(qintptr socketDescriptor, QObject *parent = nullptr);
    ~ClientThread() override;
    void run() override;
    void close_self(QTcpSocket *so);
    bool readData(QTcpSocket *so);
    bool chkversion(QByteArray body, QTcpSocket *so);
    bool getversion(QByteArray body, QTcpSocket *so);
    void setAppPDataList(QList<AppPData> list);

signals:
    void commlog(QByteArray data, bool flag);
    void datalog(QString log);
public slots:

private:
    qintptr SocketDescriptor;
    bool keepcomm;
    QList<AppPData> AppPDataList;
};

#endif // CLIENTTHREAD_H
