#include <cmath>
#include <QJsonArray>
#include <QJsonObject>
#include <QFile>
#include <QJsonDocument>
#include <QDebug>
#include <QFileDialog>

#include "enthalpydatacorrectionform.h"
#include "ui_enthalpydatacorrectionform.h"
#include "global.h"

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

    setWindowTitle("热焓校正参数编辑");

    //    ui->lineEditTheoryIn->setText("28.6");
    //    ui->lineEditTheorySn->setText("60.5");
    //    ui->lineEditTheoryBi->setText("53.3");

}

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

void EnthalpyDataCorrectionForm::on_pushButtonSave_clicked()
{
    _atmosphere = ui->lineEditAtomsphere->text().toDouble();
    _heatingRate = ui->lineEditHeatingRate->text().toDouble();

    if(ui->checkBoxC6H12->isChecked()){
        double temperature = ui->labelTempC6H12->text().toDouble();
        double theory = ui->lineEditTheoryC6H12->text().toDouble();
        double measure = ui->lineEditMeasC6H12->text().toDouble();

        _theoryDataVtr.push_back({"C6H12",temperature,theory/measure});
    }
    if(ui->checkBoxHg->isChecked()){
        double temperature = ui->labelTempHg->text().toDouble();
        double theory = ui->lineEditTheoryHg->text().toDouble();
        double measure = ui->lineEditMeasHg->text().toDouble();

        _theoryDataVtr.push_back({"Hg",temperature,theory/measure});
    }
    if(ui->checkBoxC6H5COOH->isChecked()){
        double temperature = ui->labelTempC6H5COOH->text().toDouble();
        double theory = ui->lineEditTheoryC6H5COOH->text().toDouble();
        double measure = ui->lineEditMeasC6H5COOH->text().toDouble();

        _theoryDataVtr.push_back({"C6H5COOH",temperature,theory/measure});
    }
    if(ui->checkBoxIn->isChecked()){
        double temperature = ui->labelTempIn->text().toDouble();
        double theory = ui->lineEditTheoryIn->text().toDouble();
        double measure = ui->lineEditMeasIn->text().toDouble();

        _theoryDataVtr.push_back({"In",temperature,theory/measure});
    }
    if(ui->checkBoxSn->isChecked()){
        double temperature = ui->labelTempSn->text().toDouble();
        double theory = ui->lineEditTheorySn->text().toDouble();
        double measure = ui->lineEditMeasSn->text().toDouble();

        _theoryDataVtr.push_back({"Sn",temperature,theory/measure});
    }
    if(ui->checkBoxBi->isChecked()){
        double temperature = ui->labelTempBi->text().toDouble();
        double theory = ui->lineEditTheoryBi->text().toDouble();
        double measure = ui->lineEditMeasBi->text().toDouble();

        _theoryDataVtr.push_back({"Bi",temperature,theory/measure});
    }
    if(ui->checkBoxKC1O3->isChecked()){
        double temperature = ui->labelTempKC1O3->text().toDouble();
        double theory = ui->lineEditTheoryKC1O3->text().toDouble();
        double measure = ui->lineEditMeasKC103->text().toDouble();

        _theoryDataVtr.push_back({"KC1O3",temperature,theory/measure});
    }
    if(ui->checkBoxPb->isChecked()){
        double temperature = ui->labelTempPb->text().toDouble();
        double theory = ui->lineEditTheoryPb->text().toDouble();
        double measure = ui->lineEditMeasPb->text().toDouble();

        _theoryDataVtr.push_back({"Pb",temperature,theory/measure});
    }
    if(ui->checkBoxKNO3->isChecked()){
        double temperature = ui->labelTempKNO3->text().toDouble();
        double theory = ui->lineEditTheoryKNO3->text().toDouble();
        double measure = ui->lineEditMeasKNO3->text().toDouble();

        _theoryDataVtr.push_back({"KNO3",temperature,theory/measure});
    }
    if(ui->checkBoxZn->isChecked()){
        double temperature = ui->labelTempZn->text().toDouble();
        double theory = ui->lineEditTheoryZn->text().toDouble();
        double measure = ui->lineEditMeasZn->text().toDouble();

        _theoryDataVtr.push_back({"Zn",temperature,theory/measure});
    }
    if(ui->checkBoxAg2SO4->isChecked()){
        double temperature = ui->labelTempAg2SO4->text().toDouble();
        double theory = ui->lineEditTheoryAg2SO4->text().toDouble();
        double measure = ui->lineEditMeasAg2SO4->text().toDouble();

        _theoryDataVtr.push_back({"Ag2SO4",temperature,theory/measure});
    }
    if(ui->checkBoxCsCl->isChecked()){
        double temperature = ui->labelTempCsC1->text().toDouble();
        double theory = ui->lineEditTheoryCsC1->text().toDouble();
        double measure = ui->lineEditMeasCsCl->text().toDouble();

        _theoryDataVtr.push_back({"CsCl",temperature,theory/measure});
    }
    if(ui->checkBoxSiO2->isChecked()){
        double temperature = ui->labelTempSiO2->text().toDouble();
        double theory = ui->lineEditTheorySiO2->text().toDouble();
        double measure = ui->lineEditMeasSiO2->text().toDouble();

        _theoryDataVtr.push_back({"SiO2",temperature,theory/measure});
    }
    if(ui->checkBoxK2SO4->isChecked()){
        double temperature = ui->labelTempK2SO4->text().toDouble();
        double theory = ui->lineEditTheoryK2SO4->text().toDouble();
        double measure = ui->lineEditMeasK2SO4->text().toDouble();

        _theoryDataVtr.push_back({"K2SO4",temperature,theory/measure});
    }
    if(ui->checkBoxK2CrO4->isChecked()){
        double temperature = ui->labelTempK2CrO4->text().toDouble();
        double theory = ui->lineEditTheoryK2CrO4->text().toDouble();
        double measure = ui->lineEditMeasK2CrO4->text().toDouble();

        _theoryDataVtr.push_back({"K2CrO4",temperature,theory/measure});
    }
    if(ui->checkBoxBaCO3->isChecked()){
        double temperature = ui->labelTempBaCO3->text().toDouble();
        double theory = ui->lineEditTheoryBaCO3->text().toDouble();
        double measure = ui->lineEditMeasBaCO3->text().toDouble();

        _theoryDataVtr.push_back({"BaCO3",temperature,theory/measure});
    }
    if(ui->checkBoxAg->isChecked()){
        double temperature = ui->labelTempAg->text().toDouble();
        double theory = ui->lineEditTheoryAg->text().toDouble();
        double measure = ui->lineEditMeasAg->text().toDouble();

        _theoryDataVtr.push_back({"Ag",temperature,theory/measure});
    }
    if(ui->checkBoxCu->isChecked()){
        double temperature = ui->labelTempCu->text().toDouble();
        double theory = ui->lineEditTheoryCu->text().toDouble();
        double measure = ui->lineEditMeasCu->text().toDouble();

        _theoryDataVtr.push_back({"Cu",temperature,theory/measure});
    }
    //
    saveJson();
}

void EnthalpyDataCorrectionForm::on_pushButtonExit_clicked()
{
    hide();
}

void EnthalpyDataCorrectionForm::saveJson()
{
    // 创建一个 QJsonArray 来存储数据
    QJsonObject majorObj;

    {
        QJsonObject subObj;
        subObj[Global::Atmosphere] = _atmosphere;
        subObj[Global::HeatingRate] = _heatingRate;

        majorObj[Global::AtomsphereData] = subObj;
    }

    QJsonArray jsonArray;
    for (const TheoryData &data : _theoryDataVtr) {
        QJsonObject jsonObj;
        jsonObj[Global::ElementName] = data.elementName;
        jsonObj[Global::TemperatureStr] = data.temperature;
        jsonObj[Global::RateStr] = data.rate;
        jsonArray.append(jsonObj);
    }

    // 创建一个 QJsonDocument 并设置根数组
    majorObj[Global::SampleData] = jsonArray;
    QJsonDocument doc(majorObj);

    // 打开保存文件对话框，让用户选择保存位置
    QString fileName = QFileDialog::getSaveFileName(nullptr, "保存 JSON 文件",
                                                    "new.json", "JSON Files (*.json)");
    if (fileName.isEmpty()) {
        qDebug() << "用户取消了操作";
        return;
    }

    // 确保文件名以 .json 扩展名结尾
    if (!fileName.endsWith(".json")) {
        fileName.append(".json");
    }

    // 写入 JSON 数据到文件
    QFile file(fileName);
    if (!file.open(QIODevice::WriteOnly)) {
        qDebug() << "无法打开文件进行写入:" << file.errorString();
        return ;
    }

    file.write(doc.toJson(QJsonDocument::Indented));  // 以格式化的方式写入 JSON 数据
    file.close();
}



