extends CharacterBody3D

@onready var animated_mesh: Node3D = %GobotSkin
@onready var camera: Camera3D = %Camera3D

## State
@export var battle_state := Global.BattleState.NOT_IN_BATTLE
var in_battle: bool = false
var player_monster: Monster = null
var enemy: Monster = null

## Movement
const SPEED = 5.0
const JUMP_VELOCITY = 4.5

func _ready() -> void:
	Utils.set_player(self)


func _physics_process(delta: float) -> void:
	# If the player is in a battle, handle it via start_battle and then handle_battle
	if battle_state != Global.BattleState.NOT_IN_BATTLE:
		return
		
	animated_mesh.idle()
	
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta
		animated_mesh.fall()

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept"):
		print("jump")
		animated_mesh.run()
	
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY
		
	# Camera rotation
	var camera_input_dir := Input.get_vector("joystick_right_left", "joystick_right_right", "joystick_right_left", "joystick_right_right")
	$CameraRoot.rotation.y += camera_input_dir.x

	# Get the input direction and handle the movement / deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
	
	if input_dir.length() > 0 and is_on_floor():
		animated_mesh.run()
	if input_dir.length() > 0 and not is_on_floor():
		animated_mesh.fall()
		
	var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()

	# Rotate the direction with the camera, so that the cameras forward direction
	# is the joysticks and the players forward direction
	direction = direction.rotated(Vector3.UP, $CameraRoot.rotation.y)
	
	# Move character to the direction
	if direction.length() != 0:
		$GobotSkin.rotation.y = atan2(direction.x,direction.z)
	
	if direction:
		velocity.x = direction.x * SPEED
		velocity.z = direction.z * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
		velocity.z = move_toward(velocity.z, 0, SPEED)

	move_and_slide()
	
	if velocity.length() < 0.05:
		animated_mesh.idle()


func on_save_game(saved_data : Array[SavedData]):
	var data = SavedData.new()
	data.position = global_position
	data.scene_path = scene_file_path
	saved_data.append(data)
	
func on_before_load_game():
	get_parent().remove_child(self)
	queue_free()

func on_load_game(saved_data: SavedData):
	global_position = saved_data.position
	
	# Update the global reference to the player
	Utils.set_player(self)


func handle_battle() -> void:
	match battle_state:
		Global.BattleState.STARTED:
			# disable battle_UI
			# UI.battle_ui.disable()
			UI.battle_ui.set_message("A wild %s appeared!" % enemy.data.name)
			await get_tree().create_timer(3.0).timeout
			battle_state = Global.BattleState.PLAYER_TURN
			handle_battle()
		Global.BattleState.PLAYER_TURN:
			UI.battle_ui.set_message("What will you do?")
			# todo: enable battle_ui again
			# UI.battle_ui.enable()
			UI.battle_ui.update()
		Global.BattleState.ENEMY_TURN:
			# If the enemy has no health left, we win
			if enemy.data.current_health <= 0:
				print("win")
				battle_state = Global.BattleState.WIN
				handle_battle()
				return
				
			UI.battle_ui.set_message("What will the enemy do?")
			await get_tree().create_timer(3.0).timeout
			var damage_amount: int = enemy.attack_enemy(player_monster)
			UI.battle_ui.set_message("The enemy attacked you for %s damage!" % str(damage_amount))
			await get_tree().create_timer(1.0).timeout
			UI.battle_ui.update()
			battle_state = Global.BattleState.PLAYER_TURN
			handle_battle()
		Global.BattleState.CATCH:
			print("Catch")
			await get_tree().create_timer(3.0).timeout
		Global.BattleState.WIN:
			UI.battle_ui.set_message("You won!")
			await get_tree().create_timer(3.0).timeout
			battle_state = Global.BattleState.NOT_IN_BATTLE
			enemy.queue_free()
			player_monster.queue_free()
			UI.ingame_menu.update()
			UI.show_battle_ui(false)
			UI.show_ingame_controls(true)
		Global.BattleState.LOSE:
			print("LOSE")
			await get_tree().create_timer(3.0).timeout
			
			
## This method sets up a battle between the player and an enemy monster
func start_battle(p_enemy: Monster) -> void:
	# Don't start a battle if we are already in one
	# TODO: Maybe add a cooldown of 1.5s after the battle ended
	if battle_state != Global.BattleState.NOT_IN_BATTLE:
		return
	
	# Stop the player
	velocity = Vector3.ZERO
	animated_mesh.idle()
	
	# Set the current enemy
	enemy = p_enemy
	
	# Get the loccal z axis of the camera to position the player
	var local_z = camera.global_transform.basis.z
	
	# Get the new player and monster positions
	var new_player_position = camera.global_transform.origin + local_z * 3
	var monster_position = camera.global_transform.origin + local_z * 3
	
	# Set the players new position
	position = Vector3(new_player_position.x, position.y, new_player_position.z)
	
	# Change the UI
	UI.show_battle_ui(true)
	UI.show_ingame_controls(false)
	
	# Create the player's monster
	player_monster = Monster.new()
	
	# Get the data from the party
	var monster_data : MonsterData = SaveManager.current_save.party[0]
	
	# And add it to the monster
	player_monster.data = monster_data
	player_monster.idle = true
	player_monster.position = Vector3(monster_position.x, position.y + 1, monster_position.z)
	add_child(player_monster)
	
	# Get into the battle and switch between the states
	battle_state = Global.BattleState.STARTED
	handle_battle()


func attack_enemy():
	UI.battle_ui.set_message("You attacked the enemy!")
	await get_tree().create_timer(1.0).timeout
	
	# this should rather be player_monster.attack_enemy(enemy) for correct values
	enemy.take_damage(7)
	UI.battle_ui.update()
	await get_tree().create_timer(1.0).timeout
	battle_state = Global.BattleState.ENEMY_TURN
	handle_battle()
