class_name Spaceship extends CharacterBody2D

@onready var hurtArea = $hurtArea
@onready var sprite = $Sprite2D
@onready var weaponNode = $weaponNode
@onready var invincibleTimer = $invincibleTimer

@onready var MINIGUN = preload("res://scenes/weapon/minigun/minigun.tscn")
@onready var RAILGUN = preload("res://scenes/weapon/railgun/railgun.tscn")
@onready var SHOTGUN = preload("res://scenes/weapon/shotgun/shotgun.tscn")

@onready var radiationDamageTimer = $radiationDamageTimer
@onready var radiationDamage = $radiationDamage

@export var shotgunTexture: Texture2D
@export var minigunTexture: Texture2D
@export var railgunTexture: Texture2D


@export var minHealth: int = 0
@export var maxHealth: int = 100
@onready var health: int = maxHealth

@export var speed: int = 800
@export var acceleration: float = 0.8
@export var pushback: int = 500

@export var timeGhost: TimeGhost = null

@export var weapon: Weapon = null
var currentWeapon: PackedScene

@export var invincible: bool = false

var currentRadiationDamage: int = 1

signal destroyed

func _init() -> void:
	health = maxHealth

func _ready() -> void:
	invincibleTimer.wait_time = Globals.currentDelay * 3
	shortInvincibility()

func damage(amount: int):
	if not invincible:
		if health - amount <= minHealth:
			if timeGhost == null:
				emit_signal("destroyed")
			if self.is_in_group("enemy"):
				Globals.enemyDestroyed()
			destroy()
			shortInvincibility()
			health = maxHealth
		else:
			health -= amount

func heal(amount: int):
	if health + amount >= maxHealth:
		health = maxHealth
	else:
		health += amount

func destroy():
	if timeGhost == null:
		destroyed.emit()
		queue_free()
	else:
		timeGhost.destroy()
	
func attack():
	if weapon != null:
		weapon.attack()
	await get_tree().create_timer(Globals.currentDelay).timeout
	if timeGhost != null:
		timeGhost.attack()

func moveTimeGhost(_position: Vector2, _rotation: float):
	await get_tree().create_timer(Globals.currentDelay).timeout
	if timeGhost != null:
		timeGhost.global_position = _position
		timeGhost.rotation = _rotation
		timeGhost.moveTimeGhost(_position, _rotation)

func changeWeapon(_weapon: PackedScene):
	currentWeapon = _weapon
	var newWeapon = _weapon.instantiate()
	for i in weaponNode.get_children():
		i.queue_free()
	weaponNode.add_child(newWeapon)
	weapon = newWeapon
	if newWeapon is Shotgun:
		sprite.texture = shotgunTexture
	elif newWeapon is Minigun:
		sprite.texture = minigunTexture
	elif newWeapon is Railgun:
		sprite.texture = railgunTexture
	
	await get_tree().create_timer(Globals.currentDelay).timeout
	if timeGhost != null:
		timeGhost.changeWeapon(_weapon)
	#match newWeapon:
		#Shotgun:
			#sprite.texture = shotgunTexture
		#Minigun:
			#sprite.texture = minigunTexture
		#Railgun:
			#sprite.texture = railgunTexture
			#

func addTimeGhost(_timeGhost: TimeGhost):
	if timeGhost == null:
		timeGhost = _timeGhost
		timeGhost.setSpaceship(self)
		timeGhost.changeWeapon(currentWeapon)
	else:
		timeGhost.addTimeGhost(_timeGhost)

func shortInvincibility():
	if invincibleTimer.is_stopped():
		invincible = true
		invincibleTimer.start()


func _on_invincible_timer_timeout() -> void:
	invincible = false


func _on_hurt_area_area_entered(area: Area2D) -> void:
	if area.get_parent().is_in_group("obstacle"):
		velocity = Vector2.UP.rotated(rad_to_deg(global_position.angle_to(area.global_position))) * -pushback
		area.get_parent().queue_free()

func inceraseMaxhealth(increase: int):
	maxHealth += increase
	health += increase
	

func applyRadiation(amount: int):
	if radiationDamageTimer.is_stopped():
		currentRadiationDamage = amount
		radiationDamageTimer.start()

func _on_radiation_damge_timer_timeout() -> void:
	radiationDamage.play()
	damage(currentRadiationDamage)
