#ifndef MGUANGYINGPINWIDGET_H
#define MGUANGYINGPINWIDGET_H

#include <QUdpSocket>
#include <QNetworkInterface>
#include <QtSerialPort/QSerialPort>

#include <QtWidgets/QCheckBox>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QFrame>
#include <QtWidgets/QGraphicsView>
#include <QtWidgets/QGroupBox>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QSpacerItem>
#include <QtWidgets/QSpinBox>
#include <QtWidgets/QTextEdit>
#include <QtWidgets/QVBoxLayout>

#define  MACRO_ANSY_PROGRAM(PROGRAM_INDEX) \
    if(groupBox_com->isChecked()) {\
        if(pushButtonMainOpen->text()==tr("Close")) Set_program_buf_and_send(comboBox_SPortName->currentIndex(),PROGRAM_INDEX);\
        if(pushButtonAuxOpen->text()==tr("Close")) Set_program_buf_and_send(comboBox_SPortNameAux->currentIndex(),PROGRAM_INDEX);\
    }\
    if(groupBox_Network->isChecked()) Set_program_buf_and_send_by_udp(PROGRAM_INDEX);

class TA_SerialThread  {
public:
    TA_SerialThread();
    uint8_t portCnt;
    uint8_t portNewCnt;
    QSerialPort *m_arrSerial;//ptr point to an array,maxium port num is 5
    QSerialPortInfo *m_arrNewSerial;//ptr point to an array,maxium port num is 5

    void SearchPort(void);
    void InitPortName(uint8_t portIndex,const QString &portName);
    void InitPortBaudRate(uint8_t portIndex,qint32 baudRate);
    void InitPortDataBits(uint8_t portIndex,QSerialPort::DataBits dataBits);
    void InitPortParity(uint8_t portIndex,QSerialPort::Parity parity);
    void InitPortStopBits(uint8_t portIndex,QSerialPort::StopBits stopBits);
    bool OpenPort(uint8_t portIndex);
    void ClosePort(uint8_t portIndex);
    void SendDataBuf(uint8_t portIndex,const QByteArray &str);
    QByteArray GetDataBuf(uint8_t portIndex);
};

class mGuangYingPinWidget : public QWidget {
    Q_OBJECT
public:
    explicit mGuangYingPinWidget(QWidget *parent = nullptr);
protected:
    void changeEvent(QEvent *) override;
    void transUi();
public slots:
    void MWOnoffPort(void);
    void MWOnoffPortAux(void);
    void MWSendCmd(uint8_t iComIndex,unsigned char cmd,unsigned char *pUcValue,unsigned int iLength,int iCountCmd);
    void MWSendCmdByUdp(unsigned char cmd,unsigned char *pUcValue,unsigned int iLength,int iCountCmd);

    void MWReadData(void);
    void MWReadDataAux(void);
    void MWClearRecv(void);
    void MWRSNumDisplay(void);
    void OnAnsyProgram1(void);
    void OnAnsyProgram2(void);
    void OnAnsyProgram3(void);
    void OnAnsyProgram4(void);
    void OnAnsyProgram5(void);
    void OnAnsyProgram6(void);
    void OnAnsyProgram7(void);
    void OnAnsyProgram8(void);
    void OnAnsyProgram9(void);
    void OnAnsyProgram10(void);
    void OnAnsyProgram11(void);
    void OnAnsyProgram12(void);
    void OnAnsyProgram13(void);
    void OnAnsyProgram14(void);
    void OnAnsyProgram15(void);
    void OnAnsyProgram16(void);
    void OnAnsyProgram17(void);
    void OnAnsyProgram18(void);
    void OnAnsyProgram19(void);
    void OnAnsyProgram20(void);
    void OnClearLog(void);
    void OnAnsyProgramCustom(void);
    void OnAnsyProgramNext(void);
    void OnAnsyProgramPrevious(void);
    void OnAnsyProgramLoop(void);
    void OnBrightnessSet(void);
    void OnScreenOn(void);
    void OnScreenOff(void);
    void OnAnsyProgramCustomByChanged(int icurIndex);
    void OnBrightnessSetByChanged(int icurValue);
    void OnRefresh(void);
private:
    TA_SerialThread *sPortThread;
    volatile uint32_t m_recvNum;
    volatile uint32_t m_sendNum;
    int g_iCountCmd=0;
    int m_port=31296;
    void Set_program_buf_and_send(int iComIndex,int iProgramIndex);
    void Set_program_buf_and_send_by_udp(int iProgramIndex);
    void Set_Brightness_buf_and_send(int iComIndex,int iBrightnessValue);
    void Set_Brightness_buf_and_send_by_udp(int iBrightnessValue);
    void Screen_OnOff_buf_and_send(int iComIndex,int iScreenOnOff);
    void Screen_OnOff_buf_and_send_by_udp(int iScreenOnOff);
    void NextProgram_buf_and_send(int iComIndex);
    void NextProgram_buf_and_send_by_udp();


    void mymidelay(int imlsec);
    QString ByteArrayToHexString(QByteArray &ba);

    QGroupBox *groupBox_com;
    QHBoxLayout *horizontalLayout_29;
    QVBoxLayout *verticalLayout_2;
    QGroupBox *groupBoxMain;
    QVBoxLayout *verticalLayout_6;
    QHBoxLayout *horizontalLayout_6;
    QLabel *label_PortName;
    QComboBox *comboBox_SPortName;
    QHBoxLayout *horizontalLayout_9;
    QLabel *label_BaudRate;
    QComboBox *comboBox_SPortBaud;
    QHBoxLayout *horizontalLayout_15;
    QLabel *label_DataBit;
    QComboBox *comboBox_SPortDataBit;
    QHBoxLayout *horizontalLayout_17;
    QLabel *label_OEBit;
    QComboBox *comboBox_SPortOEBit;
    QHBoxLayout *horizontalLayout_18;
    QLabel *label_StopBit;
    QComboBox *comboBox_SPortStopBit;
    QHBoxLayout *horizontalLayout_22;
    QLabel *label_ComStatus;
    QGraphicsView *graphicsView_ComStatus;
    QHBoxLayout *horizontalLayout_4;
    QPushButton *pushButtonMainOpen;
    QVBoxLayout *verticalLayout_3;
    QGroupBox *groupBoxAux;
    QVBoxLayout *verticalLayout_8;
    QHBoxLayout *horizontalLayout_7;
    QLabel *label_PortNameAux;
    QComboBox *comboBox_SPortNameAux;
    QHBoxLayout *horizontalLayout_24;
    QLabel *label_BaudRateAux;
    QComboBox *comboBox_SPortBaudAux;
    QHBoxLayout *horizontalLayout_25;
    QLabel *label_DataBitAux;
    QComboBox *comboBox_SPortDataBitAux;
    QHBoxLayout *horizontalLayout_26;
    QLabel *label_OEBitAux;
    QComboBox *comboBox_SPortOEBitAux;
    QHBoxLayout *horizontalLayout_27;
    QLabel *label_StopBitAux;
    QComboBox *comboBox_SPortStopBitAux;
    QHBoxLayout *horizontalLayout_28;
    QLabel *label_ComStatusAux;
    QGraphicsView *graphicsView_ComStatusAux;
    QHBoxLayout *horizontalLayout_5;
    QPushButton *pushButtonAuxOpen;
    QPushButton *pushButtonRefresh;
    QGroupBox *groupBox_Network;
    QSpacerItem *horizontalSpacer;
    QVBoxLayout *verticalLayout_9;
    QGroupBox *groupBoxParam;
    QVBoxLayout *verticalLayout_10;
    QHBoxLayout *horizontalLayout_20;
    QLabel *label_ResendTimes;
    QLineEdit *lineEdit_ResendTimes;
    QHBoxLayout *horizontalLayout_23;
    QLabel *label_DelayMill;
    QLineEdit *lineEdit_DelayMill;
    QSpacerItem *verticalSpacer;
    QSpacerItem *horizontalSpacer_2;
    QVBoxLayout *verticalLayout_4;
    QHBoxLayout *horizontalLayout_14;
    QHBoxLayout *horizontalLayout_16;
    QVBoxLayout *verticalLayout_5;
    QHBoxLayout *horizontalLayout_13;
    QPushButton *pushButton1;
    QPushButton *pushButton2;
    QPushButton *pushButton3;
    QPushButton *pushButton4;
    QPushButton *pushButton5;
    QHBoxLayout *horizontalLayout_12;
    QPushButton *pushButton6;
    QPushButton *pushButton7;
    QPushButton *pushButton8;
    QPushButton *pushButton9;
    QPushButton *pushButton10;
    QHBoxLayout *horizontalLayout_11;
    QPushButton *pushButton11;
    QPushButton *pushButton12;
    QPushButton *pushButton13;
    QPushButton *pushButton14;
    QPushButton *pushButton15;
    QHBoxLayout *horizontalLayout_10;
    QPushButton *pushButton16;
    QPushButton *pushButton17;
    QPushButton *pushButton18;
    QPushButton *pushButton19;
    QPushButton *pushButton20;
    QVBoxLayout *verticalLayout_7;
    QHBoxLayout *horizontalLayout_19;
    QLabel *label_ProgramNum;
    QSpinBox *spinBox_ProgramIndex;
    QPushButton *pushButtonSend;
    QPushButton *pushButtonPrevious;
    QPushButton *pushButtonNext;
    QPushButton *pushButtonLoop;
    QSpacerItem *horizontalSpacer_7;
    QHBoxLayout *horizontalLayout_21;
    QLabel *label_BrightNess;
    QSpinBox *spinBox_BrightnessValue;
    QPushButton *pushButtonBrightnessSet;
    QPushButton *pushButtonScreenOff;
    QPushButton *pushButtonScreenOn;
    QSpacerItem *horizontalSpacer_18;
    QSpacerItem *verticalSpacer_2;
    QHBoxLayout *horizontalLayout_2;
    QSpacerItem *horizontalSpacer_3;
    QPushButton *pushButtonClearLog;
    QCheckBox *checkBoxDebug;
    QHBoxLayout *horizontalLayout_3;
    QHBoxLayout *horizontalLayout_8;
    QTextEdit *textEditReadBuf;
};

#endif // MGUANGYINGPINWIDGET_H
