extends Node2D

var shootingTime = 0
var bulletSpeed = 0
var hitpoint = 0

var follower = false
var target = null

var SFX_loudness = 1.0
var master_volume = 1.0

const ARROW = preload("res://Game/Bullets/arrow.tscn")
const MAGIC = preload("res://Game/Bullets/magic.tscn")
const ROCK = preload("res://Game/Bullets/rock.tscn")

var lifter = false
var lifteramount = 0

func _ready() -> void:
	follower = true
	get_node("CharacterBody2D/Area2D").body_entered.connect(choose_target)
	get_node("CharacterBody2D/Area2D").body_exited.connect(choose_target)
	get_node("Timer").timeout.connect(shoot)
	set_process_input(true)
	set_properties()
	var config = ConfigFile.new()
	var err = config.load("user://settings.cfg")
	if err == OK:
		var master_volume_raw = config.get_value("audio", "master_volume", 100.0)
		master_volume = clamp(master_volume_raw / 100.0, 0.0, 1.0)
		var sfx_volume_raw = config.get_value("audio", "sfx_volume", 100.0)
		SFX_loudness = clamp(sfx_volume_raw / 100.0, 0.0, 1.0)

func _process(delta: float) -> void:
	if follower:
		position = get_viewport().get_mouse_position()
		if not name.contains("MortarTower"):
			if get_node("Area2D").get_overlapping_bodies().size() > 1 or (self.get_node("CharacterBody2D") not in get_parent().get_node("../TowerArea").get_overlapping_bodies() or self.get_node("CharacterBody2D") in get_parent().get_node("../TrapArea").get_overlapping_bodies()):
				get_node("CharacterBody2D/AnimatedSprite2D").modulate = Color(255, 0, 0, 0.3)
			else:
				get_node("CharacterBody2D/AnimatedSprite2D").modulate = Color(1, 1, 1, 1)
		else:
			if get_node("Area2D").get_overlapping_bodies().size() > 1 or self.get_node("CharacterBody2D") not in get_parent().get_node("../TowerArea").get_overlapping_bodies() or self.get_node("CharacterBody2D") in get_parent().get_node("../TrapArea").get_overlapping_bodies():
				get_node("CharacterBody2D/lifter - 1").modulate = Color(255, 0, 0, 0.3)
				get_node("CharacterBody2D/lifter - 2").modulate = Color(255, 0, 0, 0.3)
				get_node("CharacterBody2D/MortarPart(2)").modulate = Color(255, 0, 0, 0.3)
			else:
				get_node("CharacterBody2D/lifter - 1").modulate = Color(1, 1, 1, 1)
				get_node("CharacterBody2D/lifter - 2").modulate = Color(1, 1, 1, 1)
				get_node("CharacterBody2D/MortarPart(2)").modulate = Color(1, 1, 1, 1)
		
	elif not target:
		if get_node("CharacterBody2D/Area2D").get_overlapping_bodies():
			choose_target(null)
	elif lifter:
		lifteramount += 4
		if lifteramount <= 40:
			get_node("CharacterBody2D/lifter - 1").position.y -= 4
			get_node("CharacterBody2D/lifter - 2").position.y -= 4
		elif lifteramount >= 40 and lifteramount <= 80:
			get_node("CharacterBody2D/lifter - 1").position.y += 4
			get_node("CharacterBody2D/lifter - 2").position.y += 4
		elif lifteramount >= 80:
			lifteramount = 0
			lifter = false
		

func set_properties() -> void:
	if self.name.contains("ArcherTower"):
		get_node("Timer").wait_time = 0.5
		bulletSpeed = 600
		hitpoint = 25
		get_node("CharacterBody2D/Area2D/CollisionShape2D").shape.radius = 150
	if self.name.contains("WizardTower"):
		get_node("Timer").wait_time = 1.0
		bulletSpeed = 450
		hitpoint = 15
		get_node("CharacterBody2D/Area2D/CollisionShape2D").shape.radius = 150
	if self.name.contains("MortarTower"):			#??? not sure if necessary yet
		get_node("Timer").wait_time = 2
		hitpoint = 80

func get_if_moving_state():
	return follower

func _input(event):
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
		if abs(position) <= get_viewport().get_mouse_position():
			if get_node("Area2D").get_overlapping_bodies().size() <= 1 and self.get_node("CharacterBody2D") in get_parent().get_node("../TowerArea").get_overlapping_bodies() and not self.get_node("CharacterBody2D") in get_parent().get_node("../TrapArea").get_overlapping_bodies():
				follower = false

func choose_target(body : Node2D) -> void:
	if not follower:
		var surroinding_enemies = get_node("CharacterBody2D/Area2D").get_overlapping_bodies()
		target = null
		if surroinding_enemies.size() > 0:
			target = surroinding_enemies[0]
			if self.name.contains("ArcherTower")  or self.name.contains("MortarTower"):
				for i in surroinding_enemies:
					if i.get_parent().get_progress() > target.get_parent().get_progress():
						target = i
			if self.name.contains("WizardTower"):
				for i in surroinding_enemies:
					if i.get_parent().get_progress() < target.get_parent().get_progress():
						target = i

func shoot() -> void:
	if target != null:
		if self.name.contains("ArcherTower"):
			var arrow = ARROW.instantiate()
			arrow.name = "Arrow" + str(randi())
			arrow.position = self.position
			arrow.set_speed(bulletSpeed)
			arrow.set_targe(target)
			arrow.set_hitpoint(hitpoint)
			get_parent().add_child(arrow)
			
		if self.name.contains("WizardTower"):
			var magic = MAGIC.instantiate()
			magic.name = "Magic" + str(randi())
			magic.position = self.position
			magic.set_speed(bulletSpeed)
			magic.set_targe(target)
			magic.set_hitpoint(hitpoint)
			get_parent().add_child(magic)
		
		if self.name.contains("MortarTower"):
			var rock = ROCK.instantiate()
			rock.name = "Rock" + str(randi())
			rock.position = self.position
			rock.set_speed(bulletSpeed)
			rock.set_targe(target)
			rock.set_hitpoint(hitpoint)
			get_parent().add_child(rock)
			lifter = true
		
		self.get_node("AudioStreamPlayer2D").volume_db = linear_to_db(master_volume * SFX_loudness)
		self.get_node("AudioStreamPlayer2D").playing = true
		
