extends Node2D
class_name WaveManager

var timer: Timer = Timer.new()
var current_wave: int = -1
var done = false
@onready var logg = $"../../../UILayer/UI/Log/PC/MC/Label"
@onready var time_label = $"../../../UILayer/UI/NextWave/HB/Label"
var power_level: float = 1.0

func _ready():
    add_child(timer)
    timer.set_one_shot(true)
    timer.connect("timeout", next_wave)
    logg.set_text("")
    next_wave()
    
    $"../../../UILayer/UI/NextWave/HB/Button".connect("pressed", next_wave)

func _process(delta: float):
    time_label.set_text("in " + str(round(timer.get_time_left())) + "s")

func _input(input: InputEvent):
    if input.is_action_pressed("wave_cheat"):
        current_wave += 1
        print("Skipped wave ", current_wave + 1)

func next_wave():
    current_wave += 1
    if current_wave < get_children().size() - 1:
        var wave = get_child(current_wave)
        power_level = wave.power_level
        wave.activate()
        timer.set_wait_time(wave.duration)
        timer.start()
        if wave.title:
            logg.append_text("[color=#ffcccc]Wave " + str(current_wave + 1) + ":[/color] " + wave.title + "\n")
        else:
            logg.append_text("[color=#ffcccc]Wave " + str(current_wave + 1) + "[/color] " + "\n")
        logg.append_text(wave.get_log_data())
    else:
        done = true
        logg.append_text("[color=#ccffcc]No waves left[/color]\n")

func is_done():
    return done
