#ifndef CTLWIDGET_H
#define CTLWIDGET_H

#include "serialworker.h"
#include "combobox.h"
#include "samplewidget.h"
#include <QtWidgets/QWidget>
#include <QPushButton>
#include <QComboBox>
#include <QMutex>
#include <QTextEdit>
#include <QCheckBox>


class CtlWidget : public QWidget
{
    Q_OBJECT
public:
     CtlWidget(QWidget *parent = 0);
    ~CtlWidget();

    QStringList msgs;
    QPushButton *m_pushbutton;
    QPushButton *m_plotbutton;
    QPushButton *m_clearbutton;
    QPushButton* m_openbutton;
    QTextEdit *m_sendEdit;
    QTextEdit *m_recvEdit;
    QPushButton* m_rstatebutton;
    QPushButton* m_execubutton;
    QPushButton* m_runstatebutton;

protected:
private slots:
    void show_sample_parameter_set_window();
    void set_fit_coff_to_target();

public slots:
    void read_xlsx_data();
    void enable_load_data();
    void disable_load_button();
    void on_receive(QByteArray tmpdata);
    void send_text_data();
    void show_port_success_msgbox();
    void show_port_failed_msgbox();

    void show_write_success_msgbox();
    void show_write_failed_msgbox();
    void show_open_port_failed_msgbox();
    void show_close_port_success_msgbox();

    void deal_mutex_box1_logic(int index);
    void deal_mutex_box2_logic(int index);
    void deal_mutex_box3_logic(int index);
    void deal_mutex_box4_logic(int index);
    void deal_mutex_box5_logic(int index);
    void deal_mutex_box6_logic(int index);

    void save_data_to_xlsx();

    void read_run_mode_to_checkbox();
    void read_run_state_to_checkbox();

    void execute_send_plan();
    void set_mode_checkbox_slot(unsigned char mode_state);
    void set_run_state_combox_slot(unsigned char run_state);

    void change_checkbox1_state(int state);
    void change_checkbox2_state(int state);
    void change_checkbox3_state(int state);
    void change_checkbox4_state(int state);

    void read_fit_coff_from_target();

    void change_box_index_stage1();
    void change_box_index_stage2();
    void change_box_index_stage3();
    void change_box_index_stage4();
    void change_box_index_stage5();
    void change_box_index_stage6();
    void set_stage1_edit_box_slot(unsigned char state, float until, float riserate, uint16_t time, unsigned char airchoose);
    void set_stage2_edit_box_slot(unsigned char state, float until, float riserate, uint16_t time, unsigned char airchoose);
    void set_stage3_edit_box_slot(unsigned char state, float until, float riserate, uint16_t time, unsigned char airchoose);
    void set_stage4_edit_box_slot(unsigned char state, float until, float riserate, uint16_t time, unsigned char airchoose);
    void set_stage5_edit_box_slot(unsigned char state, float until, float riserate, uint16_t time, unsigned char airchoose);
    void set_stage6_edit_box_slot(unsigned char state, float until, float riserate, uint16_t time, unsigned char airchoose);
    void write_box_edit_to_stage1();
    void write_box_edit_to_stage2();
    void write_box_edit_to_stage3();
    void write_box_edit_to_stage4();
    void write_box_edit_to_stage5();
    void write_box_edit_to_stage6();


private:
    SerialWorker *m_serialWorker;
    QThread *m_serialThread;
    ComboBox *m_comboBox1;
    QComboBox *m_comboBox4;

    QComboBox *m_runstateBox;

    QComboBox *m_stage1Box;
    QComboBox *m_stage2Box;
    QComboBox *m_stage3Box;
    QComboBox *m_stage4Box;
    QComboBox *m_stage5Box;
    QComboBox *m_stage6Box;

    QCheckBox  *m_checkBox1;
    QCheckBox  *m_checkBox2;
    QCheckBox  *m_checkBox3;
    QCheckBox  *m_checkBox4;

    QLineEdit *m_untilEdit;
    QLineEdit *m_riseEdit;
    QLineEdit *m_timeEdit;
    QComboBox *m_airchBox;
    QPushButton *m_readStageBtn;
    QPushButton *m_writeStageBtn;
    QPushButton *m_saveStageBtn;

    QLineEdit *m_until2Edit;
    QLineEdit *m_rise2Edit;
    QLineEdit *m_time2Edit;
    QComboBox *m_airch2Box;
    QPushButton *m_readStage2Btn;
    QPushButton *m_writeStage2Btn;
    QPushButton *m_saveStage2Btn;

    QLineEdit *m_until3Edit;
    QLineEdit *m_rise3Edit;
    QLineEdit *m_time3Edit;
    QComboBox *m_airch3Box;
    QPushButton *m_readStage3Btn;
    QPushButton *m_writeStage3Btn;
    QPushButton *m_saveStage3Btn;

    QLineEdit *m_until4Edit;
    QLineEdit *m_rise4Edit;
    QLineEdit *m_time4Edit;
    QComboBox *m_airch4Box;
    QPushButton *m_readStage4Btn;
    QPushButton *m_writeStage4Btn;
    QPushButton *m_saveStage4Btn;

    QLineEdit *m_until5Edit;
    QLineEdit *m_rise5Edit;
    QLineEdit *m_time5Edit;
    QComboBox *m_airch5Box;
    QPushButton *m_readStage5Btn;
    QPushButton *m_writeStage5Btn;
    QPushButton *m_saveStage5Btn;

    QLineEdit *m_until6Edit;
    QLineEdit *m_rise6Edit;
    QLineEdit *m_time6Edit;
    QComboBox *m_airch6Box;
    QPushButton *m_readStage6Btn;
    QPushButton *m_writeStage6Btn;
    QPushButton *m_saveStage6Btn;


    QMutex mutex;

    SampleWidget *sampleWidget;

    char m_checkbox1_flag;
    char m_checkbox2_flag;
    char m_checkbox3_flag;
    char m_checkbox4_flag;

    uint16_t ModbusCRC16(QByteArray senddata);
    void send_read_command(unsigned char addr, unsigned char len);


    union{
        float f;
        unsigned char x[4];
    }coff_a;
    union{
        float f;
        unsigned char x[4];
    }coff_b;
    union{
        float f;
        unsigned char x[4];
    }coff_c;

    union{
        float f;
        unsigned char x[4];
    }stage1_untilw;
    union{
        float f;
        unsigned char x[4];
    }stage1_riseratew;

    union{
        float f;
        unsigned char x[4];
    }stage2_untilw;
    union{
        float f;
        unsigned char x[4];
    }stage2_riseratew;

    union{
        float f;
        unsigned char x[4];
    }stage3_untilw;
    union{
        float f;
        unsigned char x[4];
    }stage3_riseratew;

    union{
        float f;
        unsigned char x[4];
    }stage4_untilw;
    union{
        float f;
        unsigned char x[4];
    }stage4_riseratew;

    union{
        float f;
        unsigned char x[4];
    }stage5_untilw;
    union{
        float f;
        unsigned char x[4];
    }stage5_riseratew;

    union{
        float f;
        unsigned char x[4];
    }stage6_untilw;
    union{
        float f;
        unsigned char x[4];
    }stage6_riseratew;

signals:
    void enable_load_button();
    void sent_text_array(QByteArray tmp);
    void refresh_com_name(QString);
    void change_send_flag_to_port();
    void change_test_start_flag();

    void show_read_sample_param(QString, double);

};

#endif // CTLWIDGET_H
