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

#include "BlendNode.h"

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

#include "../SkinnedMesh.h"

BlendNode::BlendNode(AnimationController* animation_controller)
    : AnimNode(animation_controller),
      m_input_A(nullptr),
      m_input_B(nullptr),
      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_A.resize(num_soa_joints);
  m_local_matrices_B.resize(num_soa_joints);
}

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

  m_input_A->Evaluate(&m_local_matrices_A);
  m_input_B->Evaluate(&m_local_matrices_B);

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

  layers[1].transform = make_span(m_local_matrices_B);
  layers[1].weight = (m_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 BlendNode::DrawDebugUi() {
  std::string node_name = "BlendNode: " + m_name;
  if (ImGui::TreeNode(node_name.c_str())) {
    ImGui::Text("Input A:");
    m_input_A->DrawDebugUi();
    ImGui::Text("Input B:");
    m_input_B->DrawDebugUi();
    ImGui::SliderFloat("Weight", &m_weight, 0.f, 1.f);
    ImGui::Checkbox("Sync Inputs", &m_sync_inputs);
    ImGui::TreePop();
  }
}
