#ifndef CENTRALWIDGET_H
#define CENTRALWIDGET_H

#include <QWidget>
#include <QMenu>

#include "qcustomplot.h"
#include "protocol.h"
#include "global.h"
#include "eventhandler.h"
#include "filemanager.h"
#include "pointcalculate.h"
#include "localcustomplot.h"
#include "analysisoperationrecorder.h"
#include "global.h"

class CentralWidget:public QWidget
{
    Q_OBJECT
public:
    using AnalysisOperation = AnalysisOperationRecorder::AnalysisOperation;
    using AnalysisMode = AnalysisOperationRecorder::AnalysisMode;

    CentralWidget(QWidget *parent = nullptr);
    ~CentralWidget();

    void setAnalysisMode(const AnalysisMode);
    void clearAllData();

    EventHandler* getEvnetHandler(){return _eventHandler;}

    QPixmap getPixMap();

    void switchAxisMode();

    struct AxisInfo{
        bool visiable;
        double lower;
        double upper;
    };

    bool isCurrentCurve(QCPCurve* curve);

#if 0
    void deleteCurrentCurve();
    void deleteAllExperimentCurve();
#endif

    void deleteCurveByObjectName(const QString);

    QCPCurve* addCurveData(const QVector<Global::ExperimentData> &,const QString objectName);

    QCPCurve * getCurrentCurve(){return _currentCurve;}

    void startExperiment();

    void setVerticalLineRange(const double,const double);
signals:
    void sigContextMenuShow(const QPoint);
    void sigSendLineXCoord(const int,const double);
    void sigRightDockWidgetHide();

    void sigGetAxisInfoWithData(const QVector<AxisInfo>);
public slots:
    // experiment
    void slotAxisModify(const float temp);
    void slotModeModify(const Global::Mode);
    // 接收串口传递过来的数据，保存到缓存中，并且更新ui显示。
    void slotRecvCommonData(const CommonData&);
    void slotRecvAnalysisFileName(const QString&);

    //analysis setting
    void slotAnalysisSettingApply();
    void slotAnalysisSettingConfirm();
    void slotAnalysisSettingUndo();
    void slotAnalysisSettingCancel();

    void slotAnalysisSettingLineXPoint(const int index,const double);

    void slotDrawCustomText(const QString);
    // 右键菜单，删除曲线
    void slotDelCurve(QCPCurve*);
    //
    void slotGetAxisInfo();
    void slotSetAxisSettings(const QVector<double>);

protected:
    void timerEvent(QTimerEvent* event);
private slots:
    void slotSelectionChangedByUser();
private:
    const QString AxisTime = "Time/min";
    const QString AxisTemperature = "Temperature/℃";
    const QString AxisDSC = "DSC/mW";

    void uiLoadXlsxFileData();

    void glassTransitionHandle(const double x1,const double x2,const QString objectName);
    void quadraticFit(const QVector<QPointF>& points, double& a, double& b, double& c);
    double derivativeAt(const double a, const double b, const double x);
    PointCalculate::Line calculateLinearRegression(const QVector<double>& x,
                                                   const QVector<double>& y);

    QPointF onsetTemperaturePointHandle(const double x1,const double x2);
    QPointF endSetTemperaturePointHandle(const double x1,const double x2);

    void setEventHandlerEnable(const bool);
    QPointF getTheCoordinatesOfTheTextBox(const QPointF point);
    void drawText(const QPointF,const QString,const QString objectName = "");
    void fillGraph(const double x1,const double x2,const QString objectName);

    enum ClearDataMode{
        All,
        Undo,
        JustUi
    };
    void clearData(const ClearDataMode);
    void clearAllUiData();

    void calculateAnalysisResult(const AnalysisMode mode,const double x1,const double x2,const QString objectName);

    void drawOITLine(const Global::ExperimentData startData,
                     const Global::ExperimentData endData,
                     const int step,const QString objectName);

    void setAxisMode(Global::AxisMode mode);

#if 0
    void peakSynthesisDoubleAxisY(const double,const double,const QString objectName);
    void peakSynthesisSingleAxisY(const double,const double,const QString objectName);
#endif
private:
    AnalysisOperationRecorder::AnalysisMode _analysisMode;
    LocalCustomPlot *_customPlot;
    QCPCurve * _currentCurve,*_yAxis2Curve;
    EventHandler* _eventHandler;
    QCPItemStraightLine *_line1,*_line2;
    QVector<QCPItemStraightLine*> _lineVtr;
    QVector<QString> _analysisFilePathVtr;

    double _x1Record;
    double _x2Record;
};

#endif // CENTRALWIDGET_H
