extends Node

var lang = "en"

var current_scene = Utils.GAME_SCENES.MAIN_MENU

var paused = false

var screen_flash_effects = true

var skip_cutscenes = false

var current_time = Utils.TIME.PRESENT

var current_input_type : Utils.INPUT_TYPE

var current_room_id : int = 0

var interactable_states = {}

var max_active_levers = 4
var current_active_levers = 0
var levers : Dictionary[NodePath, bool] = {} #for access to boss room
var boss_accessible : bool = false
var is_in_boss_room : bool = false

var previous_run_exists : bool = false

var forcing_player_movement : bool = false

var entrance_cutscene_done : bool = false
var timeshift_cutscene_done : bool = false
var remnant_cutscene_done : bool = false

func _ready():
	SignalBus.paused.connect(pause)
	SignalBus.unpaused.connect(unpause)
	SignalBus.changed_scene_to.connect(change_scene_to)
	SignalBus.entered_boss_room.connect(func() : is_in_boss_room = true)
	TranslationServer.set_locale(lang)
	Input.joy_connection_changed.connect(_set_current_input_type)

func _exit_tree() -> void:
	SignalBus.paused.disconnect(pause)
	SignalBus.unpaused.disconnect(unpause)
	SignalBus.changed_scene_to.disconnect(change_scene_to)

func pause():
	paused = true

func unpause():
	paused = false

func change_scene_to(scene : Utils.GAME_SCENES):
	paused = false
	current_scene = scene

func reset_for_new_game():
	Globals.previous_run_exists = false
	is_in_boss_room = false
	boss_accessible = false
	current_time = Utils.TIME.PRESENT
	current_active_levers = 0
	current_room_id = 0
	interactable_states = {}
	levers = {}
	entrance_cutscene_done = false
	timeshift_cutscene_done = false
	remnant_cutscene_done = false

func _set_current_input_type(_device = 0, _connected = false):
	if Input.get_connected_joypads() != null and Input.get_connected_joypads().size() > 0:
		current_input_type = _match_joypadname_to_type(Input.get_joy_name(0))
	else:
		current_input_type = Utils.INPUT_TYPE.KEYBOARD
	SignalBus.change_input_type(current_input_type)

func _match_joypadname_to_type(joypadname : String) -> Utils.INPUT_TYPE:
	if joypadname.begins_with("PS"):
		return Utils.INPUT_TYPE.PLAYSTATION
	if joypadname.begins_with("Nintendo"):
		return Utils.INPUT_TYPE.NINTENDO
	return Utils.INPUT_TYPE.XBOX

func toggle_boss_access_lever(active = true):
	current_active_levers += 1 * (1 if active else -1)
	if current_active_levers == max_active_levers:
		boss_accessible = true
		SignalBus.boss_accessible.emit(true)
	elif boss_accessible:
		boss_accessible = false
		SignalBus.boss_accessible.emit(false)
	GUI.lever_dialogues.start_dialogue()
