extends KinematicBody2D

var Level0Des = Vector2(-280, -640)

var inarea1 = false
var inarea2 = false
var inarea3 = false
var inarea4 = false
var inarea5 = false
var inarea6 = false
var inarea7 = false

onready var OpenDoor = get_node("../OpenDoor")

# Player stats
var health = 100
var health_max = 100
var health_regeneration = 1
var mana = 100
var mana_max = 100
var mana_regeneration = 2

# Player movement speed
export var speed = 75
var last_direction = Vector2(0, 1)

var one_desired_zoom_out = Vector2(1.1,1.1)
var one_desired_zoom_in = Vector2(0.9,0.9)
var two_desired_zoom_out = Vector2(1.0,1.0)
var two_desired_zoom_in = Vector2(0.8,0.8)

func get_animation_direction(direction: Vector2):
	var norm_direction = direction.normalized()
	if norm_direction.x <= -0.707:
		return "left"
	elif norm_direction.x >= 0.707:
		return "right"
	return "right"

func animates_player(direction: Vector2):
	if direction != Vector2.ZERO:
		# update last_direction
		last_direction = direction
		
		# Choose walk animation based on movement direction
		var animation = get_animation_direction(last_direction) + "_run"
		
		# Play the walk animation
		$Body.play(animation)
	else:
		# Choose idle animation based on last movement direction and play it
		var animation = get_animation_direction(last_direction) + "_idle"
		$Body.play(animation)
		
		$Body.frames.set_animation_speed(animation, 2 + 8 * direction.length())

func _ready():
	$Level0.hide()
	OpenDoor.hide()

func _process(delta):
	# Regenerates mana
	var new_mana = min(mana + mana_regeneration * delta, mana_max)
	if new_mana != mana:
		mana = new_mana
		emit_signal("player_stats_changed", self)

	# Regenerates health
	var new_health = min(health + health_regeneration * delta, health_max)
	if new_health != health:
		health = new_health
		emit_signal("player_stats_changed", self)

func _physics_process(delta):
	# Get player input
	var direction: Vector2
	direction.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
	direction.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
	
	# If input is digital, normalize it for diagonal movement
	if abs(direction.x) == 1 and abs(direction.y) == 1:
		direction = direction.normalized()
	
	# Apply movement
	var movement = speed * direction * delta
	move_and_collide(movement)
	
	# Animate player based on direction
	animates_player(direction)
	
	if inarea1 == true:
		if (Input.is_action_just_pressed("enter_d")):
			position = Vector2(-280, -640)

	if inarea2 == true:
		if (Input.is_action_just_pressed("enter_d")):
			position = Vector2(40, -14)
	
	if inarea3 == true:
		position = Vector2(-840, -698)

	if inarea4 == true:
		position = Vector2(-392, -761)
		
	if inarea5 == true:
		position = Vector2(-808, -793)
		
	if inarea6 == true:
		position = Vector2(-329, -889)
			
	if inarea7 == true:
		OpenDoor.show()
		if (Input.is_action_just_pressed("enter_d")):
			position = Vector2(40, -14)

func _on_Area2D_body_exited(_body):
	if (_body == self):
		$Level0.hide()
		inarea1 = false
		print("out area1")
func _on_Area2D_body_entered(_body):
	if (_body == self):
		$Level0.show()
		inarea1 = true
		print("in area1")
func _on_Area2D2_body_entered(_body):
	if (_body == self):
		inarea2 = true
		print("in area2")
func _on_Area2D3_body_entered(_body):
	if (_body == self):
		inarea3 = true
		print("in area3")
func _on_Area2D4_body_entered(_body):
	if (_body == self):
		inarea4 = true
		print("in area4")
func _on_Area2D2_body_exited(_body):
	if (_body == self):
		inarea2 = false
		print("out area2")
func _on_Area2D3_body_exited(_body):
	if (_body == self):
		inarea3 = false
		print("out area3")
func _on_Area2D4_body_exited(_body):
	if (_body == self):
		inarea4 = false
		print("out area4")
func _on_Area2D5_body_entered(_body):
	if (_body == self):
		inarea5 = true
		print("in area5")
func _on_Area2D5_body_exited(_body):
	if (_body == self):
		inarea5 = false
		print("out area5")
func _on_Area2D6_body_entered(_body):
	if (_body == self):
		inarea6 = true
		print("in area6")
func _on_Area2D6_body_exited(_body):
	if (_body == self):
		inarea6 = false
		print("out area6")
func _on_Area2D7_body_entered(_body):
	if (_body == self):
		OpenDoor.show()
		inarea7 = true
		print("in area7")
func _on_Area2D7_body_exited(_body):
	if (_body == self):
		OpenDoor.hide()
		inarea7 = false
		print("out area7")

func _on_Layer1_body_entered(_body):
	$Body.scale = Vector2(1.0, 1.0)
	$CollisionShape2D.scale = Vector2(1.0, 1.0)
	
func _on_Layer1_body_exited(_body):
	$Body.scale = Vector2(0.9, 0.9)
	$CollisionShape2D.scale = Vector2(0.9, 0.9)
	
func _on_Layer2_body_entered(_body):
	$Body.scale = Vector2(0.9, 0.9)
	$CollisionShape2D.scale = Vector2(0.9, 0.9)
		

func _on_Layer2_body_exited(_body):
	$Body.scale = Vector2(0.8, 0.8)
	$CollisionShape2D.scale = Vector2(0.8, 0.8)