#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QSerialPort>
#include <QSettings>
#include <QItemDelegate>
#include <QThread>
#include <stdint.h>
#include <QDateTime>
#include "common.h"

namespace Ui {   //新增的
 class form;
}

class Worker;

#define USART_BUF_SIZE  1088
#define UART_COMMUNICATION_ERROR    "communication timeout"

/* 设置tableview某行/列不可编辑. */
class ReadOnlyDelegate: public QItemDelegate
{

public:
    ReadOnlyDelegate(QWidget *parent = nullptr):QItemDelegate(parent)
    {}

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const override;
};

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

signals:
    void triggerWork();

private slots:
    void on_btnConn_clicked();

    void on_btnReset_clicked();

    void on_btnRefresh_clicked();

    void on_configButton_clicked();

    void on_defaultButton_clicked();

    void on_fileButton_clicked();

    void on_updateButton_clicked();

    void on_configSetButton_clicked();

    void on_configGetButton_clicked();

    void on_csgConfigGetButton_clicked();

    void on_csgConfigSetButton_clicked();

    void on_dataRefreshButton_clicked();

    void on_dataRefreshAutoButton_clicked();

    void on_stateRefreshButton_clicked();

    void on_freChBox_currentIndexChanged(int index);

    void on_freRefreshButton_clicked();

    void on_waveColButton_clicked();

    void on_waveRefreshButton_clicked();

    void on_waveCalButton_clicked();

    void on_historyDataButton_clicked();

    void on_stateLogButton_clicked();

    void on_waveButton_clicked();

    void on_manualRatioSetButton_clicked();

    void on_manualAdjBox_activated(int index);

    void on_manualAdjButton_clicked();

    void on_RatioParamGetButton_clicked();

    void on_RatioParamSetButton_clicked();

    void on_ratioSetButton_clicked();

    void on_adjButton_clicked();

    void on_tabWidget_tabBarClicked(int index);
    
    void on_displayMessage(QString text);
protected:
     void closeEvent(QCloseEvent* ev) override;

private:
    QString toHexadecimal(const QByteArray &byteArray);
    int searchUsablePort();
    void serialErrHandle(QSerialPort::SerialPortError error);
    void serialDisconnect();
    int openSerial();
    int deinitSerial();
    int readData();
    int writeData(char *buf, int len);

    int protoHeaderInit(proto_head_t * header, uint16_t len, uint32_t id, uint8_t cmd_type, uint8_t cmd);

    void buttonEnable(bool flag);

    QString devStateWaveStateGet(uint8_t state);

    QString devStateWlStateGet(uint8_t state);
    void uiDevInfoRefresh();
    void uiDevConfigRefresh();
    void uiDevCsgConfigRefresh();
    void uiDevRealDataRefresh();

    void uiDevStateRefresh();

    void uiPowerFrequencyRefresh();

    void uiManualAdjustRefresh();

    void uiAdjustInfoRefresh();

    void protoContact(char *data);

    void protoConfig(char *data);

    void protoCsgConfig(char *data);

    void protoRealdata(char *data);

    int protoUpdateReply(char *data, int fileType);

    int protoWaveDataUploadReply(char *data);

    int protoPowerFrequency(char *data);

    int protoState(char *data);

    int protoManualAdjust(char *data);

    int protoAutoAdjust(char *data);

    int protoAdjustInfoGet(char *data);

    int protoLog(char * data);

    int protoHistoryData(char *data);

    int paraseProtocols(char *data);
    int snedMsgDeviceInfoGet();
    int sendMsgDeviceInfoSet();
    int sendMsgDeviceReset();
    int sendMsgDeviceCfgDefault();
    int sendMsgDeviceConfigSet();
    int sendMsgDeviceConfigGet();
    int sendMsgDeviceCsgConfigGet();
    int sendMsgDeviceCsgConfigSet();
    int sendMsgDeviceRealDataGet();
    int sendMsgDeviceUpdate(int fileType);
    int sendMsgDeviceWaveDataGet();

    int sendMsgDeviceStateGet();

    int sendMsgDevicePowerFrequencyGet();

    int sendMsgDeviceAdjustInfoGet();

    int sendMsgDeviceAdjustInfoSet();

    int sendMsgDeviceAdjustMode(uint8_t cmd, adjSet *param);

    int sendMsgWaveCol();

    int sendMsgWaveCal();

    int sendMsgLogGet();

    int sendMsgHistoryDataGet();

    Ui::form *ui;   //新增的
    QSerialPort *serialPort;
    QSettings *settings;
    QString filePath;    
    QString adjFilePath;
    QString waveFilePath;
    
    char rxbuf[USART_BUF_SIZE];
    int rxbufLen;

    devInfo clientDevInfo;
    devConfig clientDevConfig;
    csgConfig_t csgConfig;
    realData_t clientRealData;
    devState clientDevState;
    dev_adj_t clientAdjInfo;
    adjSet clientAdjSet;	
    QString energyMode[4];
    int isConnected;	// serial connect state
    int64_t upFileSize;
    uint32_t upFileRecvSize;
    uint32_t upPackIndex;
    int isDataRefresh;
    int16_t powerFre[CFG_ADC_CH_CNT][ADC_COLLECT_CNT];
    uint32_t freIndex;
    uint32_t waveIndex;
    uint16_t waveParam;
    uint8_t is_wave_up;
    uint8_t is_log_up;
    uint32_t logIndex;
    uint8_t is_data_up;
    uint32_t dataIndex;
    int32_t logSum;
    int32_t historyDataSum;
    
    ReadOnlyDelegate* readOnlyDelegate;
    QThread     *m_thread;
    Worker      *worker;
};


#include <QThread>

class Worker : public QObject
{
    Q_OBJECT
public:
    Worker() = default;
    ~Worker() = default;

    enum CntStat
    {
        CNT_RST = 0,
        CNT_START = 1,
    };
    Q_ENUM(CntStat)

    //工作函数，在这个函数中编写业务逻辑
    void doWork();
 
    //终止循环，结束工作函数
    void Stop()
    {
        stop=true;
    }

    void timeCounterStart()
    {
        m_timestamp = QDateTime::currentMSecsSinceEpoch();
        m_timeCounterStat = CNT_START;
    }

    void timeCounterReset()
    {
        m_timeCounterStat = CNT_RST;
    }
signals:
    void changeString(QString text);

private:
    bool stop = false;

private:
    QThread m_pTh;
    qint64 m_timestamp;
    CntStat m_timeCounterStat;
};


#endif // WIDGET_H
