//
// Created by martin on 19.11.21.
//

#ifndef ANIMTESTBED_BLENDSPACE1D_H
#define ANIMTESTBED_BLENDSPACE1D_H

#include "../AnimNode.h"

struct BlendSpace1D : public AnimNode {
  BlendSpace1D(AnimationController* animation_controller);

  virtual ~BlendSpace1D() {}

  int m_num_inputs;
  std::vector<float> m_input_weights;
  std::vector<AnimNode*> m_inputs;
  float m_weight;
  bool m_sync_inputs;

  AnimNode* m_input_0;
  AnimNode* m_input_1;

  float m_normalized_weight;
  float m_weight_0;
  float m_weight_1;
  ozz::vector<ozz::math::SoaTransform> m_local_matrices_0;
  ozz::vector<ozz::math::SoaTransform> m_local_matrices_1;
  ozz::math::Transform m_root_transform_0;
  ozz::math::Transform m_root_transform_1;

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

  virtual void UpdateIsSynced(bool is_synced) override {
    m_is_time_synced = is_synced;

    if (m_sync_inputs) {
      m_is_time_synced = true;
    }

    assert(m_input_weights.size() > 0);
    assert(
        m_weight >= m_input_weights[0]
        && m_weight <= m_input_weights[m_input_weights.size() - 1]);

    int prev_idx = 0;
    for (int next_idx = 1; next_idx < m_input_weights.size(); next_idx++) {
      if (m_input_weights[prev_idx] <= m_weight
          && m_input_weights[next_idx] >= m_weight) {
        m_input_0 = m_inputs[prev_idx];
        m_weight_0 = m_input_weights[prev_idx];

        m_input_1 = m_inputs[next_idx];
        m_weight_1 = m_input_weights[next_idx];

        break;
      }
      prev_idx = next_idx;
    }

    m_input_0->UpdateIsSynced(m_is_time_synced);
    m_input_1->UpdateIsSynced(m_is_time_synced);

    m_normalized_weight = (m_weight - m_weight_0) / (m_weight_1 - m_weight_0);
  }

  virtual void UpdateSyncTrack() override {
    if (m_is_time_synced) {
      m_sync_track = SyncTrack::Blend(
          m_normalized_weight,
          m_input_0->m_sync_track,
          m_input_1->m_sync_track);
    } else {
      assert(false);
    }
  }

  virtual void UpdateTime(float dt) {
    if (m_is_time_synced) {
      float sync_time_old = m_sync_track.CalcSyncFromAbsTime(m_time_current);
      m_time_current = fmodf(m_time_current + dt, m_sync_track.m_duration);
      float sync_time_dt =
          m_sync_track.CalcSyncFromAbsTime(m_time_current) - sync_time_old;

      m_input_0->m_time_current = sync_time_old;
      m_input_1->m_time_current = sync_time_old;
      m_input_0->UpdateTime(sync_time_dt);
      m_input_1->UpdateTime(sync_time_dt);
    } else {
      m_time_current += dt;
      m_input_0->UpdateTime(dt);
      m_input_1->UpdateTime(dt);
    }
  }

  virtual void Evaluate(
      ozz::vector<ozz::math::SoaTransform>* local_matrices,
      ozz::math::Transform* root_transform) override;

  virtual void GetInputNodes(
      std::vector<AnimNode*>& input_nodes) const override {
    for (int i = 0; i < m_inputs.size(); i++) {
      input_nodes.push_back(m_inputs[i]);
    }
  };

  virtual void DrawDebugUi();
};

#endif  //ANIMTESTBED_BLENDSPACE1D_H
