extends Node2D

var speed = 0
var health = 0
var shield = 0
var damage = 0
var value = 0

var current_health = 0
var current_shield = 0

var last_position: Vector2 = Vector2.ZERO  # for previous position

static var NAMES = ["Forkman", "Cobold", "Ork"]

const COIN = preload("res://Game/Accesories/coin.tscn")

func _ready() -> void:
	last_position = get_parent().position
	get_node("CharacterBody2D/Area2D").body_entered.connect(Collision_Handler)
	get_node("CharacterBody2D/AnimatedSprite2D").animation_finished.connect(AnimatedSprite2D_animation_finished)
	set_character_data()
	adjust_health_bar()

func _process(delta: float) -> void:
	move_character(delta)


func move_character(delta: float) -> void:
	var path_follow = get_parent()

	# Update the progress and prevent rotation
	path_follow.set_progress(path_follow.get_progress() + speed * delta)
	path_follow.rotation = 0

	#turn the character to to the way its headed
	var current_position = path_follow.position
	if current_position.x < last_position.x:
		path_follow.scale.x = -abs(path_follow.scale.x)
	else:
		path_follow.scale.x = abs(path_follow.scale.x)

	# Update the last_position for the next frame
	last_position = current_position
	
	#if reached the end of the path remove both the path, and the character
	if path_follow.get_progress_ratio() >= 0.999:
		get_parent().get_parent().get_parent().decrease_life(damage)
		get_parent().queue_free()


func set_character_data():
	match self.name:
		"Forkman":
			self.name = "Forkman-" + str(randi())
			speed = 120
			health = 100
			shield = 100
			damage = 2
			value = 5
		"Ork":
			self.name = "Ork-" + str(randi())
			speed = 80
			health = 150
			shield = 200
			damage = 5
			value = 10
		"Cobold":
			self.name = "Cobold-" + str(randi())
			speed = 200
			health = 50
			shield = 0
			damage = 1
			value = 2
			
	adjust_speed_of_animation() #adjust animation speed based on the character speed
	current_health = health
	current_shield = shield


func adjust_speed_of_animation() -> void:
	get_node("CharacterBody2D/AnimatedSprite2D").speed_scale = snapped(float(speed)  / 100, 0.1)

func adjust_health_bar() -> void:
	var health_bar = get_node("ProgressBar")
	
	# Create new StyleBox resources for each instance
	var fill_style = StyleBoxFlat.new()
	fill_style.corner_radius_top_left = 20
	fill_style.corner_radius_top_right = 20
	fill_style.corner_radius_bottom_left = 20
	fill_style.corner_radius_bottom_right = 20
	
	var bg_style = StyleBoxFlat.new()
	bg_style.corner_radius_top_left = 20
	bg_style.corner_radius_top_right = 20
	bg_style.corner_radius_bottom_left = 20
	bg_style.corner_radius_bottom_right = 20
	
	if current_shield > 0:
		fill_style.bg_color = Color(0, 0, 1)
		bg_style.bg_color = Color(0, 1, 0)
		health_bar.value = int((current_shield / float(shield)) * 100) 
	else:
		fill_style.bg_color = Color(0, 1, 0)
		bg_style.bg_color = Color(1, 0, 0)    
		health_bar.value = int((current_health / float(health)) * 100)
	
	# Apply the styles
	health_bar.add_theme_stylebox_override("fill", fill_style)
	health_bar.add_theme_stylebox_override("background", bg_style)

func enemy_hurt(amount) -> void:
	get_node("CharacterBody2D/AnimatedSprite2D").play("hurt")
	if current_shield > 0:
		if current_shield > amount:
			current_shield -= amount
		else:
			amount -= current_shield
			current_shield = 0
			current_health -= amount
	else:
		current_health -= amount

	if current_health <= 0:
		get_parent().get_parent().get_parent().get_node("CanvasLayer/SidePanel").Update_Coins(value)
		var coin = COIN.instantiate()
		coin.position = self.global_position
		get_parent().get_parent().add_child(coin)
		get_parent().queue_free()
	else:
		adjust_health_bar()

func AnimatedSprite2D_animation_finished() -> void:
	get_node("CharacterBody2D/AnimatedSprite2D").play("walk")


func Collision_Handler(body: Node2D):
	if body.get_parent().name.contains("StickTrap"):
		if not body.get_parent().get_if_moving_state():
			if body.get_parent().get_node("AudioStreamPlayer2D").playing == false:
				body.get_parent().get_node("AudioStreamPlayer2D").playing = true
			enemy_hurt(25)
	if body.get_parent().name.contains("Mine"):
		if not body.get_parent().get_if_moving_state():
			var surrounding_enemies = body.get_node("Area2D").get_overlapping_bodies()
			for i in surrounding_enemies:
				for j in NAMES:
					if i.get_parent().name.contains(j):
						enemy_hurt(100)
			if body.get_parent().get_node("AudioStreamPlayer2D").playing == false:
				body.get_parent().get_node("AudioStreamPlayer2D").playing = true
			body.get_parent().Explode_Mine()


func get_progress():
	return get_parent().get_progress()
	
func set_progress(amount) -> void:
	get_parent().set_progress(amount)
	
func get_speed():
	return speed
	
func set_speed(nspeed) -> void:
	speed = nspeed
