#pragma once

#include "Array.h"
#include "ReferenceCounter.h"
#include "Vec3.h"

namespace Framework
{
    class Skeleton;
    class Bone;

    struct KeyFrame
    {
        double time = 0;
        Vec3<float> pos;
        Vec3<float> rot;
    };

    class BoneAnimation : public virtual ReferenceCounter
    {
    private:
        KeyFrame* frames;
        int boneId;
        KeyFrame current;
        int frameCount;
        double maxTime;
        bool loop;

    public:
        //! create an animation for a specific bone
        //! \param boneId the id of the bone
        //! \param originPos the position of the bone at the beginning of the
        //! animation
        //! \param originRot the rotation of the bone at the beginning of the
        //! animation
        DLLEXPORT BoneAnimation(
            int boneId, Vec3<float> originPos, Vec3<float> originRot);
        //! destructor
        DLLEXPORT ~BoneAnimation();
        //! make the animation to a loop that starts again if the end is reached
        DLLEXPORT void setLoop(bool loop);
        //! adds a keyframe
        //! \param time the time in seconds since the start of the animation at
        //! which the bone should have the given position and rotation relative
        //! to its parent bone \param pos the position of the bone \param rot
        //! the rotation of the bone
        DLLEXPORT void addKeyFrame(
            double time, Vec3<float> pos, Vec3<float> rot);
        //! adds a keyframe that is the same as the last keyframe with the time
        //! time \returns true if the keyframe was added successfully, false if
        //! time is maller than the current length of the animation
        DLLEXPORT bool doNothingUntil(double time);
        //! calculates the positions and rotation at the next time
        //! \param time the passed time in seconds since the last call
        DLLEXPORT void tick(double time);
        //! applys the animation on a given skeleton
        //! \param zS: the skeleton
        DLLEXPORT void apply(Skeleton* zSkelett) const;
        //! returns true if the animation has reached the last keyframe and does
        //! not loop
        DLLEXPORT bool isFinished() const;
        //! returns the bone id this animation is for
        DLLEXPORT int getBoneId() const;
        //! returns the maximum time of the animation
        DLLEXPORT double getMaxTime() const;
    };

    class SkeletonAnimation : public virtual ReferenceCounter
    {
    private:
        RCArray<BoneAnimation> subAnimations;
        bool loop;

    public:
        //! Constructor
        DLLEXPORT SkeletonAnimation();
        //! make the animation to a loop that starts again if the end is reached
        DLLEXPORT void setLoop(bool loop);
        //! adds an animation for a specific bone
        //! \param boneId the bone id
        //! \param originPos the position of the bone at the beginning of the
        //! animation \param originRot the rotation of the bone at the beginning
        //! of the animation \return true if the animation was added, false if
        //! there is already an animation for that bone
        DLLEXPORT bool addAnimation(
            int boneId, Vec3<float> originPos, Vec3<float> originRot);
        //! adds a keyframe for a specific bone of the sceleton
        //! \param boneId id of the bone
        //! \param time the time in seconds since the start of the animation at
        //! which the bone should have the given position and rotation relative
        //! to its parent bone
        //! \param pos the position of the bone
        //! \param rot the rotation of the bone
        //! \return true if the frame was added, false if there is no annimation
        //! for that bone
        DLLEXPORT bool addKeyFrame(
            int boneId, double time, Vec3<float> pos, Vec3<float> rot);
        //! applys the animation on a given skeleton
        //! \param zS: the sceleton
        DLLEXPORT void apply(Skeleton* zS) const;
        //! calculates the positions and rotation at the next time
        //! \param time the passed time in seconds since the last call
        DLLEXPORT void tick(double time);
        //! returns true if the animation has reached the last keyframe and does
        //! not loop
        DLLEXPORT bool isFinished() const;
        //! returns the maximum time of the animation
        DLLEXPORT double getMaxTime() const;
        //! returns the animation for a specific bone or 0 if it does not exist
        DLLEXPORT BoneAnimation* zAnimation(int boneId) const;
    };
} // namespace Framework