//
// Created by martin on 12.11.21.
//

#ifndef ANIMTESTBED_SPEEDSCALENODE_H
#define ANIMTESTBED_SPEEDSCALENODE_H

#include "../AnimNode.h"

struct SpeedScaleNode : public AnimNode {
  SpeedScaleNode(AnimationController* animation_controller)
      : AnimNode(animation_controller), m_time_scale(1.f) {
    m_anim_node_type = AnimNodeType::SpeedScale;
  }

  float m_time_scale;
  AnimNode* m_input_node;

  virtual void Reset() { m_current_time = 0.f; }

  virtual void UpdateIsSynced(bool is_synced) override {
    m_is_time_synced = is_synced;
    m_input_node->UpdateIsSynced(is_synced);
  }

  virtual void EvalAnimDuration() override {
    assert(fabs(m_time_scale) >= 0.01f);
    m_anim_duration = fabsf(m_input_node->m_anim_duration / m_time_scale);
  }

  virtual void UpdateTime(float dt) {
    if (!m_is_time_synced) {
      m_current_time += dt * m_time_scale;
      m_input_node->UpdateTime(dt * m_time_scale);
    } else {
      m_current_time += dt;
      m_input_node->UpdateTime(dt);
    }
  }

  virtual void Evaluate(
      ozz::vector<ozz::math::SoaTransform>* local_matrices) override {
    m_input_node->Evaluate(local_matrices);
  };

  virtual void CollectNodeOrdering(
      std::vector<AnimNode*>& anim_nodes) override {
    anim_nodes.push_back(this);
    m_input_node->CollectNodeOrdering(anim_nodes);
  }

  virtual void DrawDebugUi() override;
};

#endif  //ANIMTESTBED_SPEEDSCALENODE_H
