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

#include "BlendSpace1D.h"

#include <imgui.h>
#include <ozz/animation/runtime/blending_job.h>

#include "../SkinnedMesh.h"

BlendSpace1D::BlendSpace1D(AnimationController* animation_controller)
    : AnimNode(animation_controller),
      m_input_0(nullptr),
      m_weight_0(0.f),
      m_input_1(nullptr),
      m_weight_1(0.f),
      m_weight(0.f),
      m_sync_inputs(false) {
  m_anim_node_type = AnimNodeType::Blend;
  const SkinnedMesh* skinned_mesh = m_animation_controller->m_skinned_mesh;
  const int num_soa_joints = skinned_mesh->m_skeleton.num_soa_joints();
  const int num_joints = skinned_mesh->m_skeleton.num_joints();
  m_local_matrices_0.resize(num_soa_joints);
  m_local_matrices_1.resize(num_soa_joints);
}

void BlendSpace1D::Evaluate(
    ozz::vector<ozz::math::SoaTransform>* local_matrices,
    ozz::math::Transform* root_transform) {
  const SkinnedMesh* skinned_mesh = m_animation_controller->m_skinned_mesh;

  m_input_0->Evaluate(
      &m_local_matrices_0,
      root_transform != nullptr ? &m_root_transform_0 : nullptr);
  m_input_1->Evaluate(
      &m_local_matrices_1,
      root_transform != nullptr ? &m_root_transform_1 : nullptr);

  // perform blend
  ozz::animation::BlendingJob::Layer layers[2];
  layers[0].transform = make_span(m_local_matrices_0);
  layers[0].weight = (1.0f - m_normalized_weight);

  layers[1].transform = make_span(m_local_matrices_1);
  layers[1].weight = (m_normalized_weight);

  ozz::animation::BlendingJob blend_job;
  blend_job.threshold = ozz::animation::BlendingJob().threshold;
  blend_job.layers = layers;
  blend_job.bind_pose = skinned_mesh->m_skeleton.joint_bind_poses();
  blend_job.output = make_span(*local_matrices);

  if (!blend_job.Run()) {
    ozz::log::Err() << "Error blending animations." << std::endl;
  }
}

void BlendSpace1D::DrawDebugUi() {
  float min_weight = m_input_weights[0];
  float max_weight = m_input_weights.back();
  ImGui::SliderFloat("Weight", &m_weight, min_weight, max_weight);
  ImGui::Checkbox("Sync Inputs", &m_sync_inputs);

  ImGui::Text("SyncTrack");
  m_sync_track.DrawDebugUi();
}
