#ifndef __GLOBAL_H
#define __GLOBAL_H

#include <qobject.h>
#include <qfile.h>
#include <qcustomplot.h>

#include "protocol.h"
#include "analysisoperationrecorder.h"

namespace Global {
const QString ExperimentDirPath  = QDir::currentPath()+"/../experiment_data";

const QString SampleDataFloder = ExperimentDirPath + "/sample_data";
const QString BaseLineFolder = ExperimentDirPath + "/base_line";
const QString AnalysisStateFolder = ExperimentDirPath + "/analysis_state";

const QString CurveOfTimeTypeObjectName("curve_time");

// json string.
const QString AtomsphereData("atomsphereData");
const QString SampleData("sampleData");

const QString Atmosphere("atmosphere");
const QString HeatingRate("heatingRate");

const QString SampleName("sampleName");
const QString TemperatureStr("temperature");
const QString RateStr("rate");

//
const double DefaultParamter = 8.177;
const double OnsetAndEndSetRate = 0.01;

//curve object name
const QString ObjectNameExperiemnt("experiment");
const QString ObjectNameSmooth("smooth");
const QString Separator("___");
//
enum LanguageType{
    Chinese,
    English
};

enum AxisMode{
    SingleY,
    DoubleY
};

const QString EnglishStr("English");
const QString ChineseStr("中文");

enum Mode{
    None,
    Analysis,
    ConnectedToDev,
    DeliveredData,
    Experiment
};

enum DeviceConnectionStatus{
    Disconnected,
    Connected
};

struct ExperimentData {
    float runTime;
    float constantTempTime;
    float sampleTemp;
    float dsc;
};

enum TestType{
    OIT,
    NoneOIT
};

struct ExperimentInfo{
    // experiment info
    QString sampleName;
    QString sampleWeight;
    QString date;
    QString experimentor;
    // phase info
    TestType testType;
    GasType initialAtmosPhere;
    int phaseSize;

    QVector<Phase> phaseVtr;
};

struct PhaseTotalInfo{
    int phaseIndex; // from 1 to 6.
    Phase phase;
    QCPCurve * curve;
    QVector<ExperimentData> dataVtr;
    QVector<ExperimentData> smoothDataVtr;
};

struct CurveFileData{
//    QString fileName;
    QString filePath;
    Global::ExperimentInfo ei;
    QVector<Global::PhaseTotalInfo> phaseTotalVtr;
    QVector<AnalysisOperationRecorder::AnalysisOperation> analysisOperationVtr;
};

struct CurveExperimentData{
    QCPCurve * curve;
    QString fileName; // discard
    QVector<Global::ExperimentData> dataVtr;
    QVector<ExperimentData> smoothDataVtr;
};

// Soft mode.
extern Mode _mode;
// Xlsx file data.
extern QVector<CurveFileData> _curveFileDataVtr;
// Experiment setting data.
extern int _currentPhase;
extern ExperimentInfo _experimentInfo;
extern QVector<CurveExperimentData> _curveExperimentDataVtr;
extern CurveExperimentData* _currentCurveExperimentDataPtr;
//extern bool _smoothnessFlag;

#if 0
// Smoothness data
extern QVector<CurveExperimentData> _curveSmoothnessDataVtr;
extern QString _smoothnessFileName;
#endif

// Call clear function at the right time.
void clearExperimentData();

// Instrument coefficient.
extern bool _enthalpyCoefficientEnableFlag;
///abc
extern QVector<double> _enthalpyCoefficientVtr;
// peak comprehensive ananlysis
extern bool _displayTimeValue;
//
extern LanguageType _languageType;

extern AxisMode _axisMode;

// common func
QString converDoubleToStr(const double);
void quadraticLeastSquaresFit(double x[], double y[], int n, double coeff[]);
double findNegativeStartPoint(double m, double b, double start, double end);
bool isZero(double value, double epsilon = 1e-9);
QString getFileName(const QString filePath);
};

#endif // GLOBAL_H
