#ifndef EventHandler_H
#define EventHandler_H

#include <QApplication>
#include <QMouseEvent>
#include <QDebug>
#include <QPixmap>
#include <QLabel>
#include <QCursor>
#include <QMenu>

#include "qcustomplot.h"

class EventHandler : public QObject
{
    Q_OBJECT
public:
    EventHandler(QCustomPlot *plot, QCPItemStraightLine *line1, QCPItemStraightLine *line2,
                    QObject *parent);
    ~EventHandler();

    void setEnable(const bool flag){_enableFlag = flag;}
    enum Mode{
        Null,
        SelectCurve,
        SelectTwoLines
    };
    void setMode(const Mode mode){ _mode = mode;}
signals:
    void sigSendLineXCoord(const int,const double);
    void sigSetCurve(const int,  QCPCurve*);
    void sigDelCurve(QCPCurve*);
protected:
    bool eventFilter(QObject *obj, QEvent *event) override;
private slots:
    void slotBaseLine();
    void slotStandardSample();
    void slotSample();

    void slotDelCurve();
private:
    void initMenu();
    int findClosestIndex(const QVector<double> &data, double target);
    void updateSelectedRegion();
    bool isNearLine(QCPItemStraightLine *line, const QPoint &pos);
private:
    Mode _mode;
    bool _enableFlag;
    QCustomPlot *_plot;
    QCPItemStraightLine *_line1,*_line2;
    QCPItemStraightLine *_draggingLine = nullptr;
    QMenu* _menu;
    QAction* _specificHeatItemAction,*_baseLineAction,
            *_standardSampleAction,*_sampleAction;
    QAction* _delCurveAction;
};

#endif
