extends Node

var current_level
var current_level_scene
var level_scene_instance
var player_scene
var is_finished

@onready var level_done_screen = $LevelDoneScreen
@onready var pause_screen = $PauseScreen
@onready var info_box_scene = $InfoBox
@onready var debug_text_scene = $LevelDebugText
@onready var barrel_counter_scene = $HUD/HBoxContainer/BarrelCounter
@onready var puddle_counter_scene = $HUD/HBoxContainer/PuddleCounter
@onready var health_counter_scene = $HUD/HBoxContainer/HealthCounter
@onready var bomb_counter_scene = $HUD/HBoxContainer/BombCounterUI

func _ready() -> void:
	print ("Game.. Ready")
	set_process(true)
	level_done_screen.visible = false
	barrel_counter_scene.visible = false
	puddle_counter_scene.visible = false
	health_counter_scene.visible = false
	bomb_counter_scene.visible = false
	bomb_counter_scene.label.text = "error"
	is_finished = false
	
	
func _process(delta: float) -> void:
	if is_finished == false:
		if Input.is_action_just_pressed("ui_cancel"):
			pause_game(true)

func load_level(level):
	print(level)
	current_level = level
	current_level_scene = load(current_level)
	#if current_level_scene == null:
	#	print ("do nothing: level not found")
	#	return

	level_scene_instance = current_level_scene.instantiate()
	
	level_scene_instance.level_finished.connect(Callable(self, "on_level_done"))
	add_child(level_scene_instance)
	barrel_counter_scene.visible = level_scene_instance.has_barrels()
	barrel_counter_scene.label.text = str(level_scene_instance.barrels_amount)
	puddle_counter_scene.visible = level_scene_instance.has_puddles()
	puddle_counter_scene.label.text = str(level_scene_instance.puddles_amount)
	
	
	player_scene = get_tree().get_first_node_in_group("player")
	assert(player_scene != null)
	health_counter_scene.visible = true
	health_counter_scene.get_node("HealthCounterLabel").text = str(player_scene.get_node("Health").max_health)
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func on_level_done():
	is_finished = true
	#wait a short time, that sounds can play to the end before pausing the game and showing the level done ui
	await get_tree().create_timer(0.15).timeout
	print ("-- level finished --")
	Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
	
	level_done_screen.visible = true
	get_tree().paused = true
	

func update_labels():
	if level_scene_instance != null:
		barrel_counter_scene.label.text = str(level_scene_instance.barrels_remaining)
		puddle_counter_scene.label.text = str(level_scene_instance.puddles_remaining)
		if puddle_counter_scene.visible == false and level_scene_instance.has_puddles():
			puddle_counter_scene.visible = true
			
			

		health_counter_scene.get_node("HealthCounterLabel").text = str(player_scene.get_node("Health").health)
	
func pause_game(paused: bool):
	Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
	pause_screen.visible = true
	pause_screen.just_paused = true
	get_tree().paused = true
	
func update_bomb_ui():
	bomb_counter_scene.visible = player_scene.bomb_counter.has_bombs()
	bomb_counter_scene.label.text = str(player_scene.bomb_counter.bombs_count)
		
	
