#ifndef KQUICKSMOOTHEDANIMATION_P_H
#define KQUICKSMOOTHEDANIMATION_P_H

#include "kquicksmoothedanimation.h"
#include <private/qquickanimation_p.h>

#include <private/qquickanimation_p_p.h>

#include <private/qobject_p.h>
#include <QBasicTimer>

QT_BEGIN_NAMESPACE
class KSmoothedAnimation;
class KSmoothedAnimationTimer : public QTimer
{
    Q_OBJECT
public:
    explicit KSmoothedAnimationTimer(KSmoothedAnimation *animation, QObject *parent = nullptr);
    ~KSmoothedAnimationTimer();
public Q_SLOTS:
    void stopAnimation();
private:
    KSmoothedAnimation *m_animation;
};

class KQuickSmoothedAnimationPrivate;
class Q_AUTOTEST_EXPORT KSmoothedAnimation : public QAbstractAnimationJob
{
    Q_DISABLE_COPY(KSmoothedAnimation)
public:
    KSmoothedAnimation(KQuickSmoothedAnimationPrivate * = nullptr);

    ~KSmoothedAnimation();
    qreal to;
    qreal velocity;
    int userDuration;

    int maximumEasingTime;
    KQuickSmoothedAnimation::ReversingMode reversingMode;

    qreal initialVelocity;
    qreal trackVelocity;

    QQmlProperty target;

    int duration() const override;
    void restart();
    void init();

    void prepareForRestart();
    void clearTemplate() { animationTemplate = nullptr; }

protected:
    void updateCurrentTime(int) override;
    void updateState(QAbstractAnimationJob::State, QAbstractAnimationJob::State) override;
    void debugAnimation(QDebug d) const override;

private:
    qreal easeFollow(qreal);
    qreal initialValue;

    bool invert;

    int finalDuration;

    // Parameters for use in updateCurrentTime()
    qreal a;  // Acceleration
    qreal d;  // Deceleration
    qreal tf; // Total time
    qreal tp; // Time at which peak velocity occurs
    qreal td; // Time at which deceleration begins
    qreal vp; // Velocity at tp
    qreal sp; // Displacement at tp
    qreal sd; // Displacement at td
    qreal vi; // "Normalized" initialvelocity
    qreal s;  // Total s

    int lastTime;
    bool skipUpdate;

    bool recalc();
    void delayedStop();
    KSmoothedAnimationTimer *delayedStopTimer;
    KQuickSmoothedAnimationPrivate *animationTemplate;
};

class KQuickSmoothedAnimationPrivate : public QQuickPropertyAnimationPrivate
{
    Q_DECLARE_PUBLIC(KQuickSmoothedAnimation)
public:
    KQuickSmoothedAnimationPrivate();
    ~KQuickSmoothedAnimationPrivate();
    void updateRunningAnimations();

    KSmoothedAnimation *anim;
    QHash<QQmlProperty, KSmoothedAnimation*> activeAnimations;
};

QT_END_NAMESPACE

#endif // KQUICKSMOOTHEDANIMATION_P_H
