extends Node

@onready var player_vars = get_node("/root/PlayerVariables")
@onready var game_time = get_node("/root/GameTimer")

var pupil_agent = preload("res://Pupil.tscn")

var pupils_per_heartbeat : int

var pupil_tracker_achievement : int
var pa_pool : int
var pupil_tracker_enjoyment : int
var pe_pool : int
var pupil_tracker_wellbeing : int
var pw_pool : int
var pupil_tracker_total : int

var year1_max_pop : float
var year2_max_pop : float
var year3_max_pop : float
var year4_max_pop : float
var year5_max_pop : float
var year6_max_pop : float
var year7_max_pop : float
var year8_max_pop : float
var year1_pop : int
var year2_pop : int
var year3_pop : int
var year4_pop : int
var year5_pop : int
var year6_pop : int
var year7_pop : int
var year8_pop : int

# Called when the node enters the scene tree for the first time.
func _ready():
	game_time.connect("heartbeat", Callable(self, "on_pupils_heartbeat"))
	pupil_tracker_achievement = 50
	pupil_tracker_enjoyment = 50
	pupil_tracker_wellbeing = 50
	pupil_tracker_total = 50

func on_pupils_heartbeat():
	
	player_vars.pupil_pop = get_child_count()

# Hieronder. Dit moet waarschijnlijk sequentially verhuizen om geen leerlingen onnodig de school uit te schoppen.

	var ct_multiplier = player_vars.classroom_count - player_vars.teacher_pop

	player_vars.max_pupils = (player_vars.classroom_count * player_vars.pupils_per_classroom)
	
	if player_vars.classroom_count > player_vars.teacher_pop:
		player_vars.max_pupils -= ct_multiplier * player_vars.pupils_per_classroom

	pupil_tracker()
	
	year1_max_pop = player_vars.max_pupils * 0.125
	year2_max_pop = player_vars.max_pupils * 0.125
	year3_max_pop = player_vars.max_pupils * 0.125
	year4_max_pop = player_vars.max_pupils * 0.125
	year5_max_pop = player_vars.max_pupils * 0.125
	year6_max_pop = player_vars.max_pupils * 0.125
	year7_max_pop = player_vars.max_pupils * 0.125
	year8_max_pop = player_vars.max_pupils * 0.125
	
	pupil_fill_spaces()

	# Adding pupils when there is capacity - random number per day	
func pupil_fill_spaces():
	if (player_vars.max_pupils - player_vars.pupil_pop) > 6:
		randomize()
		pupils_per_heartbeat = randi() % 5
		while pupils_per_heartbeat > 0:
			add_pupil()
			pupils_per_heartbeat -= 1
	elif (player_vars.max_pupils - player_vars.pupil_pop) <= 6:
		player_vars.pupil_difference = player_vars.max_pupils - player_vars.pupil_pop
		pupils_per_heartbeat = player_vars.pupil_difference
		while pupils_per_heartbeat > 0:
			add_pupil()
			pupils_per_heartbeat -= 1

func pupil_tracker():
	if pa_pool > 0 && pupil_tracker_achievement < 100:
		pupil_tracker_achievement += (pa_pool / player_vars.pupil_pop)
	if pe_pool > 0 && pupil_tracker_enjoyment < 100:
		pupil_tracker_enjoyment += (pe_pool / player_vars.pupil_pop)
	if pw_pool > 0 && pupil_tracker_wellbeing < 100:
		pupil_tracker_wellbeing += (pw_pool / player_vars.pupil_pop)
	pa_pool = 0
	pw_pool = 0
	pe_pool = 0
	pupil_tracker_total = int(float(pupil_tracker_achievement * 0.5 + pupil_tracker_enjoyment * 0.2 + pupil_tracker_wellbeing * 0.3))

func add_pupil():
	var pupil = pupil_agent.instantiate()
	add_child(pupil)
	player_vars.pupil_pop = get_child_count()

func yeargroup_manager():
	pass
