extends Node

@onready var game_time = get_node("/root/GameTimer")
@onready var player_vars = get_node("/root/PlayerVariables")
@onready var name_gen = get_node("/root/NameGenerator")
@onready var registers = get_node("/root/Registers")
@onready var pupil_manager = get_node("/root/Main/Pupils")

var pupil_neg_chance : int
var pupil_id : int
var pupil_first_name : String
var pupil_last_name : String
var pupil_full_name : String
var pupil_gender : bool

var pupil_achievement : int
var pupil_enjoyment : int
var pupil_wellbeing : int

var pupil_report_card : int
var pupil_year_group : int

var pupil_disorder : int
var pupil_diagnosed : bool

var pupil_in_game : int # The day ticker for how long the pupil was in the school

# Money related variables

var pupil_income : float # Both dailies added together
var pupil_daily_income : float # Daily income from Pupil Fee
var pupil_state_income : float # Daily income from State Funding

# For the portrait generator

var pupil_face : int
var pupil_eyes : int
var pupil_hair : int
var pupil_shirt : int


func _ready():
	randomize()
	pupil_neg_chance = (randi() % 20) + 1
	game_time.connect("year_heartbeat", Callable(self, "pupil_on_year_heartbeat"))
	game_time.connect("month_heartbeat", Callable(self, "pupil_on_month_heartbeat"))
	game_time.connect("heartbeat", Callable(self, "pupil_on_heartbeat"))
	player_vars.pupil_baseid += 1
	pupil_gender = (randi() & 1)
	randomize()
	pupil_last_name = name_gen.last_name[randi() % name_gen.last_name.size()]
	randomize()
	if pupil_gender == true:
		pupil_first_name = name_gen.boy_first_name[randi() % name_gen.boy_first_name.size()]
	if pupil_gender == false:
		pupil_first_name = name_gen.girl_first_name[randi() % name_gen.girl_first_name.size()]
	pupil_id = player_vars.pupil_baseid
	pupil_full_name = pupil_first_name + " " + pupil_last_name
	registers.PupilRegister.append(pupil_full_name)
	decide_year_group()
	# SEN Need
	randomize()
	if pupil_neg_chance == 7:
		pupil_disorder = randi() % 8

func decide_year_group():
	if pupil_manager.year8_pop < pupil_manager.year1_max_pop:
		pupil_year_group = 8
		pupil_manager.year8_pop += 1
	elif pupil_manager.year7_pop < pupil_manager.year2_max_pop:
		pupil_year_group = 7
		pupil_manager.year7_pop += 1
	elif pupil_manager.year6_pop < pupil_manager.year3_max_pop:
		pupil_year_group = 6
		pupil_manager.year6_pop += 1
	elif pupil_manager.year5_pop < pupil_manager.year4_max_pop:
		pupil_year_group = 5
		pupil_manager.year5_pop += 1
	elif pupil_manager.year4_pop < pupil_manager.year5_max_pop:
		pupil_year_group = 4
		pupil_manager.year4_pop += 1
	elif pupil_manager.year3_pop < pupil_manager.year6_max_pop:
		pupil_year_group = 3
		pupil_manager.year3_pop += 1
	elif pupil_manager.year2_pop < pupil_manager.year7_max_pop:
		pupil_year_group = 2
		pupil_manager.year2_pop += 1
	elif pupil_manager.year1_pop < pupil_manager.year8_max_pop:
		pupil_year_group = 1
		pupil_manager.year1_pop += 1
#	pupil_year_group = (randi() % 8) + 1

func pupil_on_too_many():
	if pupil_year_group == 1: pupil_manager.year1_pop -= 1
	if pupil_year_group == 2: pupil_manager.year1_pop -= 2
	if pupil_year_group == 3: pupil_manager.year1_pop -= 3
	if pupil_year_group == 4: pupil_manager.year1_pop -= 4
	if pupil_year_group == 5: pupil_manager.year1_pop -= 5
	if pupil_year_group == 6: pupil_manager.year1_pop -= 6
	if pupil_year_group == 7: pupil_manager.year1_pop -= 7
	if pupil_year_group == 8: pupil_manager.year1_pop -= 8
	registers.PupilRegister.remove(pupil_full_name)
	queue_free()

func pupil_on_year_heartbeat():
	pupil_year_group += 1
	if pupil_year_group == 2:
		pupil_manager.year1_pop -= 1
		pupil_manager.year2_pop += 1
	if pupil_year_group == 3:
		pupil_manager.year2_pop -= 1
		pupil_manager.year3_pop += 1
	if pupil_year_group == 4:
		pupil_manager.year3_pop -= 1
		pupil_manager.year4_pop += 1
	if pupil_year_group == 5:
		pupil_manager.year4_pop -= 1
		pupil_manager.year5_pop += 1
	if pupil_year_group == 6:
		pupil_manager.year5_pop -= 1
		pupil_manager.year6_pop += 1
	if pupil_year_group == 7:
		pupil_manager.year6_pop -= 1
		pupil_manager.year7_pop += 1
	if pupil_year_group == 8:
		pupil_manager.year7_pop -= 1
		pupil_manager.year8_pop += 1
	
	if pupil_year_group == 9:
		pupil_manager.year8_pop -= 1
		queue_free()

func pupil_on_month_heartbeat():
	# Too many pupils
	
	if player_vars.max_pupils < player_vars.pupil_pop:
		pupil_on_too_many()

func pupil_on_heartbeat():
	pupil_in_game += 1
	player_vars.pupils_income += (player_vars.pupil_fee / 20) + (player_vars.pupil_funding / 20)
	
	# PupilTracker stuff
	pupil_manager.pa_pool += 1
	pupil_manager.pe_pool += 1
	pupil_manager.pw_pool += 1
