#include "Player.h"

#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/input.hpp>
#include <godot_cpp/classes/input_map.hpp>
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/variant/utility_functions.hpp>

static const float WalkSpeed = 2.0;
static const float RunSpeed = 5.0;
static const float JumpStrength = 15.0;
static const float Gravity = 50.0;

static const float LerpValue = 0.15;
static const float AnimationBlend = 7.0;

namespace blitz {

void Player::_bind_methods() {}

Player::Player() {}
Player::~Player() {}

void Player::_ready() {
	godot::InputMap::get_singleton()->load_from_project_settings();
	m_PlayerMesh = Object::cast_to<godot::Node3D>(get_child(0));
	m_SpringArmPivot = Object::cast_to<godot::Node3D>(get_child(2));
	m_AnimationTree = Object::cast_to<godot::AnimationTree>(get_child(4));
	DEV_ASSERT(m_PlayerMesh);
	DEV_ASSERT(m_SpringArmPivot);
	DEV_ASSERT(m_AnimationTree);

	apply_floor_snap();
	animate(0);
}

void Player::_physics_process(float delta) {
	if (godot::Engine::get_singleton()->is_editor_hint())
		return;


	auto* Input = godot::Input::get_singleton();
	godot::Vector3 move_direction{0, 0, 0};
	move_direction.x = Input->get_action_strength("move_right") - Input->get_action_strength("move_left");
	move_direction.z = Input->get_action_strength("move_backwards") - Input->get_action_strength("move_forwards");
	move_direction = move_direction.rotated({0, 1, 0}, m_SpringArmPivot->get_rotation().y);

	godot::Vector3 newVelocity = get_velocity();
	newVelocity.y -= Gravity * delta;
	set_velocity(newVelocity);



	if (Input->is_action_pressed("run"))
		m_Speed = RunSpeed;
	else
		m_Speed = WalkSpeed;

	newVelocity = get_velocity();
	newVelocity.x = move_direction.x * m_Speed;
	newVelocity.z = move_direction.z * m_Speed;
	set_velocity(newVelocity);

	if (move_direction != godot::Vector3{0, 0, 0}) {
		godot::Vector3 newRotation = m_PlayerMesh->get_rotation();
		newRotation.y = godot::UtilityFunctions::lerp_angle(
			newRotation.y, godot::UtilityFunctions::atan2(get_velocity().x, get_velocity().z), LerpValue);
		m_PlayerMesh->set_rotation(newRotation);
	}

	bool justLanded = is_on_floor() && m_SnapVector == godot::Vector3{0, 0, 0};
	bool isJumping = is_on_floor() && Input->is_action_just_pressed("jump");

	if (isJumping) {
		newVelocity = get_velocity();
		newVelocity.y = JumpStrength;
		set_velocity(newVelocity);
		m_SnapVector.zero();
	} else if (justLanded) {
		m_SnapVector = {0, -1, 0};
	}

	apply_floor_snap();
	move_and_slide();
	animate(delta);
}

void Player::animate(float delta) {
	if (is_on_floor()) {
		m_AnimationTree->set("parameters/ground_air_transition/transition_request", "grounded");

		if (get_velocity().length() > 0) {
			if (m_Speed == RunSpeed) {
				m_AnimationTree->set("parameters/iwr_blend/blend_amount",
					godot::UtilityFunctions::lerp(
						m_AnimationTree->get("parameters/iwr_blend/blend_amount"), 1.0, delta * AnimationBlend));
			} else {
				m_AnimationTree->set("parameters/iwr_blend/blend_amount",
					godot::UtilityFunctions::lerp(
						m_AnimationTree->get("parameters/iwr_blend/blend_amount"), 0.0, delta * AnimationBlend));
			}
		} else {
			m_AnimationTree->set("parameters/iwr_blend/blend_amount",
				godot::UtilityFunctions::lerp(
					m_AnimationTree->get("parameters/iwr_blend/blend_amount"), -1.0, delta * AnimationBlend));
		}
	} else {
		m_AnimationTree->set("parameters/ground_air_transition/transition_request", "air");
	}
}
}  // namespace blitz
