extends CanvasLayer

@onready var player_vars = get_node("/root/PlayerVariables")
@onready var game_time = get_node("/root/GameTimer")
@onready var finances = get_node("/root/Finances")
@onready var registers = get_node("/root/Registers")
@onready var pupil_manager = get_node("/root/Main/Pupils")
@onready var staff_manager = get_node("/root/Main/ClassTeachers")

var estimated_funds : int


func _ready():
	game_time.connect("heartbeat", Callable(self, "_on_heartbeat"))
	update_ui_timers()
	$BuildMenu/ClassroomCost.text = str(player_vars.classroom_cost)

func disable_menus():
	$BudgetMenu.visible = false
	$PolicyMenu.visible = false
	$PrincipalMenu.visible = false
	$HRMenu.visible = false
	$DebugMenu.visible = false
	$BuildMenu.visible = false
	$InfoMenu.visible = false

func update_ui_timers():
	$MainMenu/DayNumber.text = str(player_vars.day)
	$MainMenu/WeekNumber.text = str(player_vars.week)
	$MainMenu/MonthNumber.text = str(player_vars.month)
	$MainMenu/YearNumber.text = str(player_vars.year)	
	if player_vars.season == 1:
		$MainMenu/SeasonNumber.text = "Autumn"
	if player_vars.season == 2:
		$MainMenu/SeasonNumber.text = "Winter"
	if player_vars.season == 3:
		$MainMenu/SeasonNumber.text = "Spring"
	if player_vars.season == 4:
		$MainMenu/SeasonNumber.text = "Easter"
	if player_vars.season == 5:
		$MainMenu/SeasonNumber.text = "Summer"


func update_funds():
	$MainMenu/FundsNumber.text = str(player_vars.funds)
	$MainMenu/RatingNumber.text = str(player_vars.rating)
	$MainMenu/PupilsNumber.text = str(player_vars.pupil_pop)
	$MainMenu/PupilsMax.text = str(player_vars.max_pupils)

func update_trackers():
	$DebugMenu/PupilAchievementNumber.text = str(pupil_manager.pupil_tracker_achievement)
	$DebugMenu/PupilEnjoymentNumber.text = str(pupil_manager.pupil_tracker_enjoyment)
	$DebugMenu/PupilWellbeingNumber.text = str(pupil_manager.pupil_tracker_wellbeing)
	$DebugMenu/PupilTotalScoreNumber.text = str(pupil_manager.pupil_tracker_total)
	$DebugMenu/TeacherAchievementNumber.text = str(staff_manager.staff_tracker_achievement)
	$DebugMenu/TeacherEnjoymentNumber.text = str(staff_manager.staff_tracker_enjoyment)
	$DebugMenu/TeacherWellbeingNumber.text = str(staff_manager.staff_tracker_wellbeing)
	$DebugMenu/TeacherTotalScoreNumber.text = str(staff_manager.staff_tracker_total)
	$MainMenu/Bars/PupilBar.value = pupil_manager.pupil_tracker_total
	$MainMenu/Bars/TeacherBar.value = staff_manager.staff_tracker_total
	$InfoMenu/Year1Number.text = str(pupil_manager.year1_pop)
	$InfoMenu/Year2Number.text = str(pupil_manager.year2_pop)
	$InfoMenu/Year3Number.text = str(pupil_manager.year3_pop)
	$InfoMenu/Year4Number.text = str(pupil_manager.year4_pop)
	$InfoMenu/Year5Number.text = str(pupil_manager.year5_pop)
	$InfoMenu/Year6Number.text = str(pupil_manager.year6_pop)
	$InfoMenu/Year7Number.text = str(pupil_manager.year7_pop)
	$InfoMenu/Year8Number.text = str(pupil_manager.year8_pop)

func update_policy():
	$PolicyMenu/ClassSize.text = str(player_vars.pupils_per_classroom)

func _process(_delta):
	$DebugMenu/TestTimer.text = str(game_time.game_timer)
	update_funds()
	update_policy()
	update_trackers()


func _on_heartbeat():
	update_ui_timers()
	$InfoMenu/PupilRegister.text = str(registers.PupilRegister)

func _on_SpeedOne_pressed():
	game_time.paused = 0
	game_time.wait_time = 6

func _on_SpeedTwo_pressed():
	game_time.paused = 0
	game_time.wait_time = 3

func _on_SpeedThree_pressed():
	game_time.paused = 0
	game_time.wait_time = 1

func _on_SpeedPause_pressed():
	game_time.paused = 1

func _on_DebugMenuClose_pressed():
	$DebugMenu.visible = false

func _on_DebugMenuOn_pressed():
	if player_vars.has_staffroom == true:
		$DebugMenu/StaffroomLabel.text = str("HasStaffroom")
	if player_vars.has_cpdroom == true:
		$DebugMenu/CPDRoomLabel.text = str("HasCPDRoom")
	disable_menus()
	$DebugMenu.visible = true

func _on_BuildMenuOn_pressed():
	disable_menus()
	$BuildMenu.visible = true

func _on_BuildMenuClose_pressed():
	$BuildMenu.visible = false

func _on_ClassroomButton_pressed():
	pass


func _on_BudgetMenuClose_pressed():
	$BudgetMenu.visible = false
#	$PausedRect.visible = false
#	$MainMenu.visible = true
#	$MenuButtons.visible = true
#	game_time.paused = 0


func _on_BudgetMenuOn_pressed():
	disable_menus()
	$BudgetMenu.visible = true
#	$PausedRect.visible = true
#	$MainMenu.visible = false
#	$MenuButtons.visible = false
#	game_time.paused = 1


func _on_ClosePolicyMenu_pressed():
	$PolicyMenu.visible = false
#	game_time.paused = 0


func _on_PolicyMenuOn_pressed():
	disable_menus()
	$PolicyMenu.visible = true
#	game_time.paused = 1


func _on_PupilFeeDecrease_pressed():
	if player_vars.pupil_fee > 99:
		player_vars.pupil_fee -= 100


func _on_PupilFeeIncrease_pressed():
	player_vars.pupil_fee += 100


func _on_ClassSizeLower_pressed():
	player_vars.pupils_per_classroom -= 1


func _on_ClassSizeRaise_pressed():
	player_vars.pupils_per_classroom += 1


func _on_PrincipalMenuOn_pressed():
	disable_menus()
	$PrincipalMenu/NameLine.text = player_vars.principal_name
	$PrincipalMenu/SchoolNameLine.text = player_vars.school_name
	$PrincipalMenu.visible = true


func _on_ClosePrincipalMenu_pressed():
	$PrincipalMenu.visible = false


func _on_HRMenuButton_pressed():
	disable_menus()
	$HRMenu.visible = true


func _on_CloseHRMenu_pressed():
	$HRMenu.visible = false


func _on_NameLineOK_pressed():
	player_vars.principal_name = $PrincipalMenu/NameLine.text


func _on_InfoButton_pressed():
	disable_menus()
	$InfoMenu.visible = true


func _on_InfoMenuClose_pressed():
	$InfoMenu.visible = false
