extends RoomTemplate

signal ini_room

@onready var mask_memento: Collectable_Ui = %MaskMemento.ui
@onready var clothes_memento: Collectable_Ui = %ClothesMemento.ui
@onready var comic_memento: Collectable_Ui = %ComicMemento.ui
@onready var ceiling_memento: Collectable_Ui = %CeilingMemento.ui
@onready var board_trigger: InteractiveSprite = %MindBoard
@onready var door_trigger: InteractiveSprite = %DoorTrigger
@onready var card_board: CardBoard = %Board
@onready var card_picker: CardPicker = %Picker

func start(save: SaveGame = SaveGame.new()):
	save_game = save
	
	# Setting Memento user interface elemnts as collected based on the savegame.
	mask_memento.collected = save_game.mementos_complete && 1 >> Scenes.id.YOUTH_CHILDHOOD > 0
	clothes_memento.collected = save_game.mementos_complete && 1 >> Scenes.id.YOUTH_JUI_JUTSU > 0
	comic_memento.collected = save_game.mementos_complete && 1 >> Scenes.id.YOUTH_VOICE_TRAINING > 0
	ceiling_memento.collected = save_game.mementos_complete && 1 >> Scenes.id.YOUTH_DRAEVEN > 0
	
	$logic/PlayerController.process_mode = Node.PROCESS_MODE_INHERIT
	ini_room.emit()


func get_ready():
		self.show()
		$sfx/distant_rain.play()
		$"sfx/rain on window".play()
		await get_tree().create_timer(0.1).timeout
		$logic/UI/board.hide()

func _ready():
	if get_parent() == get_tree().root:
		get_ready()
		start()
	card_picker.cards_picked.connect(card_board.populate_board)

func pull_save_state(save: SaveGame) -> void:
	save.board_state = card_board.get_save_dict()

func _on_scene_finished(id: int):
	save_game.mementos_complete &= 1 << id
	save_room()
