extends Node2D

class_name SolarPanel

@onready var sponge_area := $SpongeArea
@onready var mop_area := $MopArea
@onready var gun_area := $GunArea

@onready var dirt_sprites := [$Dirt/Sprite2D1, $Dirt/Sprite2D2, $Dirt/Sprite2D3]

@onready var clean_timer := $CleanTimer
@onready var particle_emitter := $CPUParticles2D

@onready var update_timer := $UpdateTimer
@onready var progress_bar := $TextureProgressBar

@export var dirt_level := 0

signal cleaned

var points := [50, 20, 10]
var clean_time := [5.0, 5.0, 5.0]
var cleaner : Astronaut = null

var clean := false

var satellite : Satellite = null

var target_pos := [Vector2(-2, 9), Vector2(-2, 16), Vector2(-2, 30)]

var progress_textures := [load("res://gfx/tmp/clean_progress.png"),
load("res://gfx/tmp/clean_progress_m.png"),
load("res://gfx/tmp/clean_progress_d.png")]

### 	 ACCESSORS 	 ###






### 	 BUILT-IN 	 ###

func _ready() -> void:
	connect_signals()




### 	 VIRTUAL 	 ###





### 	 LOGIC 	 ###

func init_dirt() -> void:
	if dirt_level < 0:
		clean = true
	else:
		for i in range(dirt_level+1):
			dirt_sprites[i].visible = true


func connect_signals() -> void:
	sponge_area.connect("body_entered", _on_sponge_area_body_entered)
	mop_area.connect("body_entered", _on_mop_area_body_entered)
	gun_area.connect("body_entered", _on_gun_area_body_entered)
	clean_timer.connect("timeout", _on_clean_timer_timeout)
	update_timer.connect("timeout", _on_update_timer_timeout)


func start_cleaning(astro) -> void:
	progress_bar.texture_progress = progress_textures[dirt_level]
	progress_bar.value = 0
	progress_bar.visible = true
	update_timer.start()
	particle_emitter.emitting = true
	cleaner = astro
	cleaner.set_state("Clean")
	clean_timer.start(clean_time[dirt_level] * (cleaner.stress_modifier * cleaner.stats[Astronaut.STAT_TYPES.CLEAN_SPEED]))


func cleaning_done() -> void:
	EVENTS.money_earned.emit(points[dirt_level])
	dirt_sprites[dirt_level].set_deferred("visible", false)
	dirt_level -= 1
	if dirt_level < 0:
		clean = true
		remove_cleaner()
		satellite.check_clean()
	else:
		progress_bar.value = 0
		progress_bar.texture_progress = progress_textures[dirt_level]
		clean_timer.start(clean_time[dirt_level] * cleaner.stats[Astronaut.STAT_TYPES.CLEAN_SPEED])


func remove_cleaner() -> void:
	progress_bar.visible = false
	update_timer.stop()
	particle_emitter.emitting = false
	if cleaner:
		cleaner.panel = null
		cleaner.set_state("Idle")
		cleaner.check_satellite_timer.start()
		cleaner = null
		clean_timer.stop()
	if !clean:
		satellite.available_panels.append(self)


func get_target_pos(astro : Astronaut) -> Vector2:
	if astro.weapon_type == Equipment.TYPE.SPONGE:
		return target_pos[0]
	elif astro.weapon_type == Equipment.TYPE.SPONGE:
		return target_pos[1]
	return target_pos[2]


func update() -> void:
	var ratio = clean_timer.time_left/clean_timer.wait_time
	var progress = 100 * (1 - (clean_timer.time_left/clean_timer.wait_time))
	progress_bar.value = 100 * (1 - (clean_timer.time_left/clean_timer.wait_time))


func update_stress_level() -> void:
	if cleaner and clean_timer.time_left != 0:
		var rem_time = clean_timer.time_left
		clean_timer.start(rem_time * cleaner.stress_modifier)

### 	 SIGNAL-RESPONSE 	 ###

func _on_sponge_area_body_entered(body) -> void:
	if body.owner.weapon_type == Equipment.TYPE.SPONGE and body.owner.panel == self:
		if clean:
			body.owner.set_state("Idle")
		else:
			start_cleaning(body.owner)


func _on_mop_area_body_entered(body) -> void:
	if body.owner.weapon_type == Equipment.TYPE.MOP and body.owner.panel == self:
		if clean:
			body.owner.set_state("Idle")
		else:
			start_cleaning(body.owner)


func _on_gun_area_body_entered(body) -> void:
	if body.owner.weapon_type > Equipment.TYPE.MOP and body.owner.panel == self:
		if clean:
			body.owner.set_state("Idle")
		else:
			start_cleaning(body.owner)


func _on_clean_timer_timeout() -> void:
	cleaning_done()


func _on_update_timer_timeout() -> void:
	update()
