/***************************** LICENSE START ***********************************

 Copyright 2012 ECMWF and INPE. This software is distributed under the terms
 of the Apache License version 2.0. In applying this license, ECMWF does not
 waive the privileges and immunities granted to it by virtue of its status as
 an Intergovernmental Organization or submit itself to any jurisdiction.

 ***************************** LICENSE END *************************************/

#pragma once

#include <vector>

#include <QBrush>
#include <QGraphicsItem>
#include <QPen>

#include "MvQPlotItem.h"

#include "Layer.h"

class QTextDocument;

class MgQLayoutItem;
class MgQSceneItem;

class MvQCursorData : public MvQPlotItem, public ValuesCollectorVisitor
{
    Q_OBJECT

public:
    enum PositionMode
    {
        Anchored,
        FollowCursor
    };
    enum ProbeMode
    {
        AnchoredProbe,
        FollowCursorProbe
    };

    MvQCursorData(MgQPlotScene*, MvQPlotView*, QGraphicsItem* parent = nullptr);
    ~MvQCursorData() override = default;

    void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*) override;
    QRectF boundingRect() const override;

    void setActivated(bool) override;
    void prepareForReset() override;
    void reset() override;

    void setPositionMode(PositionMode);
    void changePositionMode(QPointF&);
    void changeProbeMode(QPointF&);
    void setCursorPos(QPointF&);
    void setText(QPointF, QList<ValuesCollector> layerData, QString, QString, bool);
    void setText();
    void setMagnifierInfo(bool, QPointF magScenePos = QPointF(), float magZoomFactor = 1.);

    void mousePressEventFromView(QMouseEvent*) override;
    void mouseMoveEventFromView(QMouseEvent*) override;
    void mouseReleaseEventFromView(QMouseEvent*) override;

    void setZoomWasPerformedAfterMouseRelease(bool b) { zoomWasPerformedAfterMouseRelease_ = b; }

    QString unitsString(QString);

    void visit(const ValuesCollectorData&) override;
    void visit(const ValuesCollectorUVData&) override;
    void visit(const ValuesCollectorSDData&) override;

signals:
    void positionChanged(QPointF, QList<ValuesCollector>);
    void probeEnabled(bool);

private:
    void setText(QString);
    QString formatNumber(double);
    void setData(QPointF);

    QGraphicsScene* scene_{nullptr};
    QString text_;
    QPointF coordinates_;

    PositionMode positionMode_{FollowCursor};
    QPointF offsetFromCursor_{20, 20};
    QPointF cursorPos_;

    ProbeMode probeMode_{FollowCursorProbe};
    QRectF boundingRect_;
    QTextDocument* textDoc_{nullptr};
    MgQLayoutItem* dataLayout_{nullptr};
    MgQSceneItem* sceneItem_{nullptr};
    bool pointInMagnifier_{false};
    QPointF magnifierScenePos_;
    float magnifierZoomFactor_{1.};
    bool zoomWasPerformedAfterMouseRelease_{true};
    std::vector<float> currentValues_;
};
