#include <iostream>
#include "AnimationSystem.h"
#include <tracy/Tracy.hpp>

namespace AnimationSystem
{

bool AnimationSystem::addTime(float time)
{
    int i;
    ZoneScopedN("AnimationSystem::addTime");
    preUpdateTriggers();
    bool ret = m_builder.animation_nodes[0]->addTime(time);
    for (i = 0; i < m_builder.animationNodeList.size(); i++) {
        ZoneScoped;
        AnimationNodeAnimation *anim = m_builder.animationNodeList[i];
        OgreAssert(anim->mAnimation, "No animation");
        float weight = anim->getWeight();
        anim->mAnimation->increaseAccWeight(weight);
#ifdef VDEBUG
        if (debug)
            std::cout << i
                  << " node: " << anim->mAnimation->getName()
                  << " " << weight << std::endl;
#endif
        if (anim->getWeight() > 0.01f) {
            ZoneScoped;
            ZoneText("builder", strlen("builder"));
            ZoneText(anim->mAnimation->getName().c_str(),
                 anim->mAnimation->getName().length());
            ZoneValue(anim->getWeight() * 100.0f);
        }
    }
    for (i = 0; i < vanimation_list.size(); i++) {
        ZoneScoped;
        float weight = vanimation_list[i]->getAccWeight();
        vanimation_list[i]->setWeight(weight);
        vanimation_list[i]->resetAccWeight();
// #define VDEBUG
#ifdef VDEBUG
        if (debug && vanimation_list[i]->getEnabled())
            std::cout << i << " animation: "
                  << vanimation_list[i]->getName() << " "
                  << weight << std::endl;
#endif
#undef VDEBUG
        if (vanimation_list[i]->mAnimationState->getEnabled()) {
            ZoneScoped;
            ZoneText("animation", strlen("animation"));
            ZoneText(vanimation_list[i]->getName().c_str(),
                 vanimation_list[i]->getName().length());
            ZoneValue(vanimation_list[i]->getWeight() * 100.0f);
        }
    }
    postUpdateTriggers(time);
    return ret;
}
Ogre::Vector3 AnimationSystem::getRootMotionDelta()
{
    ZoneScopedN("AnimationSystem::getRootMotionDelta");
    Ogre::Vector3 motionDelta(0, 0, 0);
    int i;
    for (i = 0; i < vanimation_list.size(); i++)
        motionDelta += vanimation_list[i]->getRootMotionDelta();
    return motionDelta;
}

}
