#ifndef XLRUCHARTCLASS_H
#define XLRUCHARTCLASS_H

#include <QWidget>

#include <QtCharts/QChart>
#include <QtCharts/QLineSeries>
#include <QtCharts/QChartView>
#include <QtCharts/QValueAxis>
#include <QtCharts/QDateTimeAxis>
#include <QDateTime>
#include <QTimerEvent>
#include <QLabel>
#include <QtCharts/QLegendMarker>
#include <QDebug>
#include <QMessageBox>
#include <QDockWidget>

#include "callout.h"
#include "xlrureportclass.h"
#include "xlruchartconfig.h"
#include "xlrusensorstable.h"

//#define TELEMETRY

#ifdef TELEMETRY
#include "telemetryclass.h"
#endif

// c = class name; e = enum name; v = enum value
#define ENUM_TO_STRING(c, e, v) \
    (c::staticMetaObject.enumerator(c::staticMetaObject.indexOfEnumerator(#e)).valueToKey(v))
// c = class name; e = enum name; s = string value
#define STRING_TO_ENUM(c, e, s) \
    (c::e)(c::staticMetaObject.enumerator(c::staticMetaObject.indexOfEnumerator(#e)).keyToValue(s.toLatin1()))


using namespace QtCharts;

//DEFINE SECTION - VISUALIZATION
#define AXIS_ALIGNMENT_TEMP     Qt::AlignRight
#define AXIS_ALIGNMENT_CURR     Qt::AlignLeft
#define AXIS_ALIGNMENT_VOLT     Qt::AlignLeft
#define AXIS_ALIGNMENT_DATE     Qt::AlignBottom

#define CHART_DIMX_DEF      320
#define CHART_DIMY_DEF      240

#define PEN_DIM_DEF         1
#define FONT_DIM_DEF        10

#define PURGE_DEF           10      //minutes
#define PURGE_PARTIAL_DEF   30      //minutes

//DEFINE SECTION - OPERATIVE
#define TEMP_DEF_MIN        0
#define TEMP_DEF_MAX        80
#define CURR_DEF_MIN        0
#define CURR_DEF_MAX        10
#define VOLT_DEF_MIN        -20
#define VOLT_DEF_MAX        20

#define TEMP_ALARM_DEF      false
#define CURR_ALARM_DEF      false
#define VOLT_ALARM_DEF      false

#define REPORT_ENABLED_DEF  true

#define UPDATE_OPTIMIZED    true
#define TIME_UPDATE_XMAX    30      //seconds
#define TIME_DELTA_XMAX     5       //seconds


//#define TRACKING_VALUE    //display tracking sample


class Sensor;

class XlruChartClass : public QWidget
{
    Q_OBJECT

public:

    explicit XlruChartClass(QWidget *parent = 0);
    ~XlruChartClass();

    bool tgtAlive;

    QDockWidget* dw;

    XlruReportClass* report;

    Qt::CheckState autopurge_enabled;

    QVector<Sensor*> list_sensors;        //list of sensor

    XlruSensorsTableClass *SensorsTable;

signals:

public slots:

    QLineSeries* addSeries(QString _name, QValueAxis* _axis);

    int addSensorFromConfig(sensor_config_t* sensor_config);

    int findSensor(QString _label);

    int findSensorName(QString _name);

    void addSample(QString name_sensor, float value);

    void setReport(bool enabled);

    void connectMarkers();
    void handleMarkerClicked();
    void disconnectMarkers();

    void keepCallout();
    void tooltip(QPointF point, bool state);
    void connectTooltip();

    void setTitle(QString aTitle);
    QString getTitle();

    void setPenDim(int _dim);

    void purge();

    void clear();

    void save_header_info();

    void save_all_sample();

    void timerEvent();

    void setRemoveLimit(bool new_value);
    bool getRemoveLimit();

#ifdef TELEMETRY
    void setTelemetry(telemetryClass *_telemetry);
#endif

private:
    void init();

    QString title;

    QChart* ch;           //main chart

    QChartView* mainView;           //main chart view
    QDateTimeAxis* commonX;         //commonX,  datetime

    QValueAxis* temperatureY;       //temperature axis
    QValueAxis* currentY;           //current axis
    QValueAxis* voltageY;           //voltage axis

    QDateTime timeStart;            //time to start graph
    qint64 timeStartMs;

    QDateTime timeNow;              //time now for graph
    qint64 timeNowMs;
    qint64 lastSampleMs;

    QLabel* valueToScreen;

    bool report_enabled;

    Callout *m_tooltip;
    QList<Callout *> m_callouts;

    void showWarning(QString _mess);

    int penDim;

    QMessageBox alertBox;

    bool remove_limit;

#ifdef TELEMETRY
    telemetryClass *telemetry;
#endif

};

class Sensor
{
public:
    QString name;
    QString label;
    QLineSeries* series;
    QChartView* view;
    QValueAxis* axis;
    bool limitFlag;
    int limit;
    type_series_t type_serie;
    int fpga_ch;

    Sensor():
        series(0),
        view(0),
        axis(0),
        limitFlag(false)
    {
        limit = 0;
        fpga_ch = -1;
        type_serie = type_s_temperature;
    }
};





#endif // XLRUCHARTCLASS_H
