extends Node

var player : Player = null
var panel : GamePanel = null
var player_profile : Window = null
var quest_log : QuestLog = null
var top_menu : TopMenu = null
var open : bool = true
var end_shift_confirmation : ConfirmationDialog
var end_shift_confirm_template = preload("res://templates/end_shift_confirmation.tscn")
var player_profile_template = preload("res://templates/player_profile_window.tscn")

func _ready() -> void:
	DisplayServer.register_additional_output(self)
	end_shift_confirmation = end_shift_confirm_template.instantiate()
	add_child(end_shift_confirmation)
	
func _process(delta: float) -> void:
	if open:
		if Input.is_action_just_pressed("profile"):
			toggle_player_profile()
		if Input.is_action_just_pressed("switch modes"):
			confirm_end_shift()
		
		
func add_quest_progress_bar(quest : Quest) -> void:
	panel.add_quest_progress_bar(quest)

func confirm_end_shift() -> void:
	end_shift_confirmation.popup_centered()

func setup_visitor_ui(spawner: VisitorSpawner)-> void:
	if panel:
		panel.connect_visitor_spawner(spawner)

func toggle_player_profile():
	if player_profile != null:
		player_profile.queue_free()
		player_profile = null
	else:
		player_profile = player_profile_template.instantiate()
		add_child(player_profile)
		player_profile.setup(player.data)

	
func end_shift() -> void:
	open = false
	if player_profile != null:
		toggle_player_profile()
	panel.switch_panel(open)
	var window = get_window()
	window.mode = Window.MODE_WINDOWED
	var size = DisplayServer.screen_get_size()
	window.content_scale_size = Vector2i(415,700)
	var wsize = window.get_size_with_decorations()
	#window.position = Vector2i(size.x - 345, 25)
	window.transparent = true
	window.transparent_bg = true
	DisplayServer.window_set_flag(DisplayServer.WINDOW_FLAG_BORDERLESS, true)
	DisplayServer.window_set_flag(DisplayServer.WINDOW_FLAG_TRANSPARENT, true)
	#window.per
	Guild.hall.process_mode = Node.PROCESS_MODE_DISABLED
	Guild.hall.visible = false
	top_menu.hide()
	panel.get_parent().global_position = Vector2i(5,5)
	window.size = Vector2i(415,700)
	panel.populate_quest_views()
	panel.populate_quest_bars()

func notice(msg : String, time : float = 1) -> void:
	panel.notice(msg, time)

func calculate_kill_exp(killer : QuestSprite, killed : QuestSprite) -> int:
	return clamp(1, (killed.level - killer.level) * 5, 100)
