class_name Player extends MechCharacter

const MAX_HEAT: float = 100.0
const MAX_DAMAGE: float = 100.0

@export var manager: PlayerManager

var _heat: float = 0.0
var _damage: float = 0.0
var _controllable: bool = true

@onready var camera_root: ThirdPersonCamera = $CameraRoot
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var aim_cast: RayCast3D = $CameraRoot/AimCast

# gdlint: disable=class-definitions-order
var structure: int:
	set(value):
		manager.structure_meter.structure_count = clamp(
			value, StructureMeter.MIN_STRUCTURE, StructureMeter.MAX_STRUCTURE
		)
	get:
		return manager.structure_meter.structure_count
# gdlint: enable=class-definitions-order


func _ready() -> void:
	super._ready()
	manager.heat_meter.value = _heat
	manager.damage_meter.value = _damage
	_weapon.aimcast = aim_cast


func deal_damage(delta: float) -> void:
	_damage += delta
	manager.damage_meter.value = _damage
	if _damage >= MAX_DAMAGE:
		overdamage()


func deal_heat(delta: float) -> void:
	_heat += delta
	manager.heat_meter.value = _heat
	if _heat >= MAX_HEAT:
		overheat()


func deal_structure(delta: int) -> void:
	structure += delta


func overheat() -> void:
	destructure()


func overdamage() -> void:
	destructure()


func destructure() -> void:
	_controllable = false
	shutdown()
	if structure > 0:
		manager.overlay.play_destructure()
		animation_player.play("destructure_respawn")
	else:
		manager.overlay.play_death()
		print("GAME OVER DUDE")


func respawn() -> void:
	structure -= 1
	manager.respawn()


func _physics_process(delta: float) -> void:
	if not _controllable:
		super.physics_process(Vector3.ZERO, delta)
		return

	var delta_factor: float = delta * GameState.TARGET_FPS

	if is_boosting():
		manager.crosshair.jostle(Vector2(randf_range(-3, 3), randf_range(-3, 3)))

	# Handle jump.
	if Input.is_action_just_pressed(manager.jump_action()) and is_on_floor():
		jump()

	# Get the input direction and handle the movement/deceleration.
	var movement: Vector3 = Vector3.ZERO
	var input_dir: Vector2 = manager.get_input_vector()
	if input_dir:
		# Transform movement based on camera angle
		movement = (
			(
				camera_root.global_transform.basis
				* Vector3(input_dir.x, 0.0, input_dir.y)
				* Vector3(-1.0, 0.0, -1.0)
			)
			. normalized()
		)

		if Input.is_action_just_pressed(manager.boost_action()) and boost_ready():
			boost(movement)
			manager.overlay.play_boost()
			animation_player.play("boost_fov")
			animation_player.seek(0)

		# Slowly turn mesh towards camera vector when moving on ground
		if is_on_floor():
			mesh.rotation.y = lerp_angle(
				mesh.rotation.y, camera_root.rotation.y, delta_factor * TURN_SENSITIVITY
			)

	animation_tree["parameters/walk_space/blend_position"] = lerp(
		animation_tree["parameters/walk_space/blend_position"],
		input_dir if input_dir.y <= 0 else (input_dir * Vector2(-1, 1)),
		delta_factor * TURN_SENSITIVITY
	)

	if Input.is_action_just_pressed(manager.weapon_action()):
		_weapon.trigger()

	if Input.is_action_just_released(manager.weapon_action()):
		_weapon.release()

	# Take heat if exposed to the sun:
	if not shadow_listener.in_shadow():
		deal_heat(GameState.SOLAR_HEAT_RATE)

	super.physics_process(movement, delta)


func _on_mech_stomp() -> void:
	manager.crosshair.jostle(Vector2(randf_range(-1, 1), randf_range(-1, 5)))
	# camera_root.jostle(randf_range(-2, 2))
