#ifndef QGSCOPECHRONO_H
#define QGSCOPECHRONO_H

#include <QObject>

class QTimer;

class QgScopeChronoTimer : public QObject
{
    Q_OBJECT

    Q_PROPERTY(bool frozen READ frozen WRITE setFreeze NOTIFY chronoFrozen)

    Q_PROPERTY(unsigned long period READ period)

public:
    typedef unsigned long long us_t;

    explicit QgScopeChronoTimer(QObject *parent = 0);
    explicit QgScopeChronoTimer(const QgScopeChronoTimer&);
    explicit QgScopeChronoTimer(const QString& name, QObject *parent = 0);

    us_t timeNow();

    void start(us_t period);
    void stop();
    void restart();

    int id() const;

    QString name() const;

    bool frozen() const;
    void setFreeze(bool freeze);

    QTimer* qtTimer() { return m_timer;}

    unsigned long period() const
    {
        return m_period;
    }

signals:
    void periodExpired();
    void chronoFrozen(bool froozen);

public slots:

private slots:
    void onPeriodExpired();
private:
    int m_id;
    unsigned int m_multiplier;
    bool m_frozen;
    us_t m_period;
    QTimer* m_timer;
};


#include <QElapsedTimer>

class QgScopeChrono: public QObject
{
    Q_OBJECT

    Q_PROPERTY(unsigned int multiplier READ multiplier WRITE setMultiplier NOTIFY chronoMultiplierChanged)
    Q_PROPERTY(bool frozen READ frozen WRITE setFreeze NOTIFY chronoFrozen)

public:

    static QgScopeChrono& instance();

    unsigned int multiplier() const;
    void setMultiplier(unsigned int n);

    unsigned long long timeNowUs() const;

    bool frozen() const;
    void setFreeze(bool freeze);

public slots:
    void chronoStart(unsigned long long ms_resolutions);

    void step();

signals:
    void chronoMultiplierChanged(int new_multiplier);
    void chronoFrozen(bool);

private:
    unsigned int m_multiplier;
    bool m_frozen;
    QElapsedTimer elapsedTimer;

    explicit QgScopeChrono();
    ~QgScopeChrono();
};

#endif // QGSCOPECHRONO_H
