#include <QHBoxLayout>
#include <math.h>

#include "specificheatcomparisonmethodform.h"
#include "ui_specificheatcomparisonmethodform.h"

#include "logger.h"
#include "global.h"
#include "pointcalculate.h"

SpecificHeatComparisonMethodForm::SpecificHeatComparisonMethodForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SpecificHeatComparisonMethodForm)

{
    ui->setupUi(this);

    setWindowTitle("比热比较法");

    _baseLineLineEdit = new QLineEdit(ui->groupBoxDataFile);
    _baseLineLabel = new QLabel(ui->groupBoxDataFile);
    _baseLineLabel->setStyleSheet("background-color: red;");
    _baseLineLabel->setFixedWidth(22);

    _standardSampleLineEdit = new QLineEdit(ui->groupBoxDataFile);
    _standardSampleLabel = new QLabel(ui->groupBoxDataFile);
    _standardSampleLabel->setStyleSheet("background-color: green;");
    _standardSampleLabel->setFixedWidth(22);

    _sampleLineEdit = new QLineEdit(ui->groupBoxDataFile);
    _sampleLabel = new QLabel(ui->groupBoxDataFile);
    _sampleLabel->setStyleSheet("background-color: black;");
    _sampleLabel->setFixedWidth(22);

    QHBoxLayout *baseLineLayout = new QHBoxLayout;
    baseLineLayout->addWidget(_baseLineLineEdit);
    baseLineLayout->addWidget(_baseLineLabel);

    QHBoxLayout *standardSampleLayout = new QHBoxLayout;
    standardSampleLayout->addWidget(_standardSampleLineEdit);
    standardSampleLayout->addWidget(_standardSampleLabel);

    QHBoxLayout *sampleLayout = new QHBoxLayout;
    sampleLayout->addWidget(_sampleLineEdit);
    sampleLayout->addWidget(_sampleLabel);

    ui->formLayoutDataFile->addRow("基线",baseLineLayout);
    ui->formLayoutDataFile->addRow("标样",standardSampleLayout);
    ui->formLayoutDataFile->addRow("样品",sampleLayout);

    //
    _baseLineLineEdit->setEnabled(false);
    _standardSampleLineEdit->setEnabled(false);
    _sampleLineEdit->setEnabled(false);

    ui->LineEditCorrectionCoefficient->setText("1.000");
    ui->LineEditSampleWeight->setText("17.92");
    ui->LineEditStandardSampleWeight->setText("13.92");

    _baseLineCurve = nullptr;
    _standardSampleCurve = nullptr;
    _sampleCurve = nullptr;
}

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

void SpecificHeatComparisonMethodForm::slotSetCurve(const int type,  QCPCurve *curve)
{
    logde<<"index:"<<type;

    switch (type) {
    case CurveType::BaseLine:
        _baseLineCurve = curve;
        break;
    case CurveType::StandardSample:
        _standardSampleCurve = curve;
        break;
    case CurveType::Sample:
        _sampleCurve = curve;
        break;
    default:break;
    }
}

void SpecificHeatComparisonMethodForm::showEvent(QShowEvent *event)
{
#if 0
    auto& curveDataVtr = Global::_curveExperimentDataVtr;

    logde<<"curve data vtr size:"<<Global::_curveExperimentDataVtr.size();

           //    QVector<Global::ExperimentData> *baseLineDataVtr = nullptr,
    //            *standardSampleDataVtr = nullptr,
    //            *sampleDataVtr = nullptr;

    for (auto& item : curveDataVtr) {
        if(item.curve == _baseLineCurve){
            _baseLineLineEdit->setText(item.fileName);
            _baseLineDataVtr = &(item.dataVtr);
        }else if(item.curve == _standardSampleCurve){
            _standardSampleLineEdit->setText(item.fileName);
            _standardSampleDataVtr = &(item.dataVtr);
        }else if(item.curve == _sampleCurve){
            _sampleLineEdit->setText(item.fileName);
            _sampleDataVtr = &(item.dataVtr);
        }
    }
#endif

    for(auto& cfd:Global::_curveFileDataVtr){
        for(auto& item:cfd.phaseTotalVtr){
            if(item.curve == _baseLineCurve){
                _baseLineLineEdit->setText(cfd.filePath);
                _baseLineDataVtr = &(item.dataVtr);
            }else if(item.curve == _standardSampleCurve){
                _standardSampleLineEdit->setText(cfd.filePath);
                _standardSampleDataVtr = &(item.dataVtr);
            }else if(item.curve == _sampleCurve){
                _sampleLineEdit->setText(cfd.filePath);
                _sampleDataVtr = &(item.dataVtr);
            }
        }
    }

    if(!_baseLineDataVtr ||!_standardSampleDataVtr||
            !_sampleDataVtr){
        logde<<"Base,standard,sample not selected.";
        return;
    }

}

void SpecificHeatComparisonMethodForm::on_pushButtonCalculate_clicked()
{
    if(!_baseLineDataVtr ||!_standardSampleDataVtr||
            !_sampleDataVtr){
        logde<<"Base,standard,sample not selected.";
        return;
    }

    //
    float targetTemperature = ui->LineEditTemperature->text().toFloat();

    PointCalculate::setAnalysisData(*_baseLineDataVtr);
    QPointF baseLinePoint = PointCalculate::getClosestPointByX(targetTemperature);

    PointCalculate::setAnalysisData(*_standardSampleDataVtr);
    QPointF standardSamplePoint = PointCalculate::getClosestPointByX(targetTemperature);

    PointCalculate::setAnalysisData(*_sampleDataVtr);
    QPointF samplePoint = PointCalculate::getClosestPointByX(targetTemperature);

    logde<<"baseLinePoint:"<<baseLinePoint.x()<<","<<baseLinePoint.y();
    logde<<"standardSamplePoint:"<<standardSamplePoint.x()<<","<<standardSamplePoint.y();
    logde<<"samplePoint:"<<samplePoint.x()<<","<<samplePoint.y();

    double cp = calculateHeatCapacity(targetTemperature);
    logde<<"cp:"<<cp;

    float standardSampleWeight = ui->LineEditStandardSampleWeight->text().toFloat();
    float sampleWeight = ui->LineEditSampleWeight->text().toFloat();

    float k = ui->LineEditCorrectionCoefficient->text().toFloat();

    // 蓝宝石比热容 * 标样质量 * （样品流值 - 基线流值）* 系数 /(样品质量 * (标样流值 - 基线流值))
    double cpTarget = cp * standardSampleWeight * ( samplePoint.y() - baseLinePoint.y()) * k /
            (sampleWeight *(standardSamplePoint.y() - baseLinePoint.y()));

    ui->LineEditStandardSampleSpecificHeat->setText(QString::number(cp,'f',3));
    ui->LineEditSampleSpecificHeat->setText(QString::number(cpTarget,'f',3));

//    100℃比热容=x J/(g*℃)
    QString str = QString("%1℃比热容= %2J/(g*℃)")
            .arg(QString::number(targetTemperature,'f',3))
            .arg(QString::number(cpTarget,'f',3));
    emit sigDrawCustomText(str);
}

void SpecificHeatComparisonMethodForm::on_pushButtonQuit_clicked()
{
    hide();
}

double SpecificHeatComparisonMethodForm::calculateHeatCapacity(double theta) {
    // 系数A_i
    const double A0 = 1.12705;
    const double A1 = 0.23260;
    const double A2 = -0.21704;
    const double A3 = 0.26410;
    const double A4 = -0.23778;
    const double A5 = -0.10023;
    const double A6 = 0.15393;
    const double A7 = 0.54579;
    const double A8 = -0.47824;
    const double A9 = -0.37623;
    const double A10 = 0.34407;

    // 温度转换
    double T = theta + 273.15; // 摄氏温度转开尔文
    double x = (T - 650.0) / 550.0; // 归一化温度变量

    // 计算比热容
    double Cp = A0 + A1 * x + A2 * std::pow(x, 2) + A3 * std::pow(x, 3) + A4 * std::pow(x, 4) +
            A5 * std::pow(x, 5) + A6 * std::pow(x, 6) + A7 * std::pow(x, 7) + A8 * std::pow(x, 8) +
            A9 * std::pow(x, 9) + A10 * std::pow(x, 10);

    return Cp;
}
