#include <QtDebug>

#include "experimentsettingform.h"
#include "ui_experimentsettingform.h"
#include "protocol.h"
#include "dataparser.h"
#include "serialport.h"

ExperimentSettingForm::ExperimentSettingForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ExperimentSettingForm)
{
    ui->setupUi(this);
    uiReset();

    connect(ui->checkBox_phase_2,&QCheckBox::clicked,
            this,&ExperimentSettingForm::slotPhase2StateChanged);
    connect(ui->checkBox_phase_3,&QCheckBox::clicked,
            this,&ExperimentSettingForm::slotPhase3StateChanged);
    connect(ui->checkBox_phase_4,&QCheckBox::clicked,
            this,&ExperimentSettingForm::slotPhase4StateChanged);

    connect(ui->pushButton_connect_to_device,&QPushButton::clicked,
            this,&ExperimentSettingForm::slotConnectToDevice);
    connect(ui->pushButton_cancel,&QPushButton::clicked,
            this,&ExperimentSettingForm::slotCancel);

    connect(this,&ExperimentSettingForm::sigConnectToDevice,
            SerialPort::instance(),&SerialPort::slotSendData);
}

ExperimentSettingForm::~ExperimentSettingForm()
{
    delete ui;
}

void ExperimentSettingForm::uiReset()
{
    //init data and status.
    ui->checkBox_phase_1->setChecked(true);
    ui->checkBox_phase_1->setEnabled(false);

    ui->phase_1_cutoff_temp->setText("0");
    ui->phase_2_cutoff_temp->setText("0");
    ui->phase_3_cutoff_temp->setText("0");
    ui->phase_4_cutoff_temp->setText("0");

    ui->phase_1_scan_rate->setText("0");
    ui->phase_2_scan_rate->setText("0");
    ui->phase_3_scan_rate->setText("0");
    ui->phase_4_scan_rate->setText("0");

    ui->phase_1_constant_temp->setText("0");
    ui->phase_2_constant_temp->setText("0");
    ui->phase_3_constant_temp->setText("0");
    ui->phase_4_constant_temp->setText("0");

    ui->phase_2_cutoff_temp->setEnabled(false);
    ui->phase_3_cutoff_temp->setEnabled(false);
    ui->phase_4_cutoff_temp->setEnabled(false);

    ui->phase_2_scan_rate->setEnabled(false);
    ui->phase_3_scan_rate->setEnabled(false);
    ui->phase_4_scan_rate->setEnabled(false);

    ui->phase_2_constant_temp->setEnabled(false);
    ui->phase_3_constant_temp->setEnabled(false);
    ui->phase_4_constant_temp->setEnabled(false);

    ui->comboBox_phase_2_atmosphere->setEnabled(false);
    ui->comboBox_phase_3_atmosphere->setEnabled(false);
    ui->comboBox_phase_4_atmosphere->setEnabled(false);

    ui->radioButton_OIT_not->setChecked(true);

    ui->checkBox_phase_2->setTristate(false);
    ui->checkBox_phase_3->setTristate(false);
    ui->checkBox_phase_4->setTristate(false);
}
void ExperimentSettingForm::slotPhase2StateChanged(int state)
{
    qDebug()<<"slotPhase2StateChanged:"<<state;

    if(state == Qt::PartiallyChecked){
        ui->phase_2_cutoff_temp->setEnabled(true);
        ui->phase_2_scan_rate->setEnabled(true);
        ui->phase_2_constant_temp->setEnabled(true);
        ui->comboBox_phase_2_atmosphere->setEnabled(true);
    }else{
        if(ui->checkBox_phase_3->isChecked()){
            ui->checkBox_phase_2->setCheckState(Qt::Checked);
        }else {
            ui->phase_2_cutoff_temp->setEnabled(false);
            ui->phase_2_scan_rate->setEnabled(false);
            ui->phase_2_constant_temp->setEnabled(false);
            ui->comboBox_phase_2_atmosphere->setEnabled(false);
        }
    }
}

void ExperimentSettingForm::slotPhase3StateChanged(int state)
{
    if(state == Qt::PartiallyChecked){
        if(ui->checkBox_phase_2->isChecked()){
            ui->phase_3_cutoff_temp->setEnabled(true);
            ui->phase_3_scan_rate->setEnabled(true);
            ui->phase_3_constant_temp->setEnabled(true);
            ui->comboBox_phase_3_atmosphere->setEnabled(true);
        }else{
            ui->checkBox_phase_3->setChecked(false);
        }
    }else{
        if(ui->checkBox_phase_4->isChecked()){
            ui->checkBox_phase_3->setChecked(true);
        }else{
            ui->phase_3_cutoff_temp->setEnabled(false);
            ui->phase_3_scan_rate->setEnabled(false);
            ui->phase_3_constant_temp->setEnabled(false);
            ui->comboBox_phase_3_atmosphere->setEnabled(false);
        }
    }
}

void ExperimentSettingForm::slotPhase4StateChanged(int state)
{
    if(state == Qt::PartiallyChecked){
        if(ui->checkBox_phase_3->isChecked()){
            ui->phase_4_cutoff_temp->setEnabled(true);
            ui->phase_4_scan_rate->setEnabled(true);
            ui->phase_4_constant_temp->setEnabled(true);
            ui->comboBox_phase_4_atmosphere->setEnabled(true);
        }else{
            ui->checkBox_phase_4->setChecked(false);
        }
    }else{
        ui->phase_4_cutoff_temp->setEnabled(false);
        ui->phase_4_scan_rate->setEnabled(false);
        ui->phase_4_constant_temp->setEnabled(false);
        ui->comboBox_phase_4_atmosphere->setEnabled(false);
    }
}

void ExperimentSettingForm::slotConnectToDevice()
{
    QVector<Phase> phaseVtr;
    if(ui->checkBox_phase_1->checkState()){
        Phase phase;
        phase.onoff = 1;
        phase.cutoff_temp = ui->phase_1_cutoff_temp->text().toFloat();
        phase.temp_flow = ui->phase_1_scan_rate->text().toFloat();
        phase.constant_temp_time_min = (uint16_t)ui->phase_1_constant_temp->text().toInt();
        //        phase.gas = ui->comboBox_phase_1_atmosphere->currentIndex();
        phase.gas = GasType::N2;

        phaseVtr.push_back(phase);
    }
    if(ui->checkBox_phase_2->checkState()){
        Phase phase;
        phase.onoff = 1;
        phase.cutoff_temp = ui->phase_2_cutoff_temp->text().toFloat();
        phase.temp_flow = ui->phase_2_scan_rate->text().toFloat();
        phase.constant_temp_time_min = (uint16_t)ui->phase_2_constant_temp->text().toInt();
        //        phase.gas = ui->comboBox_phase_1_atmosphere->currentIndex();
        phase.gas = GasType::N2;

        phaseVtr.push_back(phase);
    }

    QByteArray ba = DataParser::connectToDevice(phaseVtr);
    qDebug()<<"ba size:"<<ba.size();

    emit sigConnectToDevice(ba);
}

void ExperimentSettingForm::slotCancel()
{

}
void ExperimentSettingForm::slotPhaseCheck()
{
    if(sender() == ui->checkBox_phase_2){
        if(ui->checkBox_phase_2->isChecked()){
            ui->phase_2_cutoff_temp->setEnabled(true);
            ui->phase_2_scan_rate->setEnabled(true);
            ui->phase_2_constant_temp->setEnabled(true);
            ui->comboBox_phase_2_atmosphere->setEnabled(true);
        }else{
            ui->phase_2_cutoff_temp->setEnabled(false);
            ui->phase_2_scan_rate->setEnabled(false);
            ui->phase_2_constant_temp->setEnabled(false);
            ui->comboBox_phase_2_atmosphere->setEnabled(false);
        }
    }else if(sender() == ui->checkBox_phase_3){
        if(ui->checkBox_phase_3->isChecked()){
            ui->phase_3_cutoff_temp->setEnabled(true);
            ui->phase_3_scan_rate->setEnabled(true);
            ui->phase_3_constant_temp->setEnabled(true);
            ui->comboBox_phase_3_atmosphere->setEnabled(true);
        }else{
            ui->phase_3_cutoff_temp->setEnabled(false);
            ui->phase_3_scan_rate->setEnabled(false);
            ui->phase_3_constant_temp->setEnabled(false);
            ui->comboBox_phase_3_atmosphere->setEnabled(false);
        }
    }else if(sender() == ui->checkBox_phase_4){
        if(ui->checkBox_phase_4->isChecked()){
            ui->phase_4_cutoff_temp->setEnabled(true);
            ui->phase_4_scan_rate->setEnabled(true);
            ui->phase_4_constant_temp->setEnabled(true);
            ui->comboBox_phase_4_atmosphere->setEnabled(true);
        }else{
            ui->phase_4_cutoff_temp->setEnabled(false);
            ui->phase_4_scan_rate->setEnabled(false);
            ui->phase_4_constant_temp->setEnabled(false);
            ui->comboBox_phase_4_atmosphere->setEnabled(false);
        }
    }
}


