extends Node

var finland_data = {
	"helsinki" : helsinki,
	"uudenmaa" : uudenmaa,
	"varsinaissuomi" : varsinaissuomi,
	"satakunta" : satakunta,
	"ahvenanmaa" : ahvenanmaa,
	"hame" : hame,
	"pirkanmaa" : pirkanmaa,
	"kaakkoissuomi" : kaakkoissuomi,
	"savokarjala" : savokarjala,
	"vaasa" : vaasa,
	"oulu" : oulu,
	"lappi" : lappi,
}

var helsinki = []
var uudenmaa = []
var varsinaissuomi = []
var satakunta = []
var ahvenanmaa = []
var hame = []
var pirkanmaa = []
var kaakkoissuomi = []
var savokarjala = []
var vaasa = []
var oulu = []
var lappi = []
@onready var score_ui: Control = $ScoreUi

@export var map : Node2D
@export var region : Node2D
@export var encounter : Node2D
@export var encounter_buttons: Control
@export var finland : Finland
var current_depth = 0

func _ready() -> void:
	SignalBus.region_clicked.connect(open_region)
	SignalBus.worldmap.connect(open_worldmap)
	SignalBus.encounter_clicked.connect(open_encounter)
	
	finland.init()
	current_depth = 2
	open_worldmap()
	open_worldmap()
	score_ui.button.hide()

	#for child in region.get_children():
		#child.hide()
	#for child in encounter.get_children():
		#child.hide()

func open_region(scene : PackedScene, region_node : Region):
	for child in region.get_children():
		child.hide()
	map.hide()
	region.show()
	
	if region_node.children.size() <= 0:
		var spawned_node = scene.instantiate()
		var node_name = spawned_node.name
		finland_data[node_name] = spawned_node.encounter_spawner.spawn_positions
		region.add_child(spawned_node)
		region_node.children.append(spawned_node)

	else:
		for child in region_node.children:
			child.show()
			
	current_depth += 1
	score_ui.button.show()

func open_worldmap():
	if current_depth <= 0:
		return
		

	current_depth -= 1

	if current_depth > 1:
		map.hide()
		region.hide()
		encounter.show()
	elif current_depth > 0:
		map.hide()
		region.show()
		encounter.hide()

	else:
		map.show()
		region.hide()
		encounter.hide()		
		score_ui.button.hide()


func open_encounter(scene : PackedScene, encounter_node : EncounterNode):
	for child in encounter.get_children():
		child.hide()
		
	encounter.show()
	region.hide()
	if encounter_node.children.size() <= 0:		
		var spawned_node = scene.instantiate()
		spawned_node.parent_node = encounter_node
		encounter_node.reactivated.connect(spawned_node.reset_bubbles)
		encounter.add_child(spawned_node)
		encounter_node.children.append(spawned_node)
	else:
		for child in encounter_node.children:
			child.show()
	current_depth += 1
	score_ui.button.show()
