extends Node


const visitors = {
	"test": {
		"data":preload("res://templates/test_adventurer.tscn"),
		"sprite":preload("res://templates/test_adventurer_sprite.tscn")
	}
}


var surnames : Array[String] = []
var masc_names : Array[String] = []
var femme_names : Array[String] = []

const job_list = [
	preload("res://data/jobs/farmer.tres"),
	preload("res://data/jobs/rookie.tres")
]

var jobs : Dictionary[String, JobData] = {}

var members : Array[Adventurer] = []
var quests : Dictionary[Quest,bool] = {}
var hall : Guildhall = null
var visitor_spawner : VisitorSpawner = null
var gold : int
var level : int
var reputation : int
var rep_tnl : int
var vault : Array = []

signal changed()


func _ready() -> void:
	var file = FileAccess.open("res://data/names/surnames.txt", FileAccess.READ)
	while !file.eof_reached():
		var line = file.get_line().strip_edges()
		surnames.append(line)
	file.close()
	
	file = FileAccess.open("res://data/names/masc_names.txt", FileAccess.READ)
	while !file.eof_reached():
		var line = file.get_line().strip_edges()
		masc_names.append(line)
	file.close()
	
	file = FileAccess.open("res://data/names/femme_names.txt", FileAccess.READ)
	while !file.eof_reached():
		var line = file.get_line().strip_edges()
		femme_names.append(line)
	file.close()
	
	for job : JobData in job_list:
		jobs[job.name] = job

func register_guild_member(member : Adventurer, first : bool = false) -> void:
	members.append(member)
	Game.top_menu.add_member(member)
	changed.emit()
	if first:
		Game.notice("%s has joined the guild!" % member.name, 5)

func has_guild_member(member : Adventurer) -> bool:
	if member == null:
		return false
	return members.has(member)

func add_quest(quest : Quest) -> void:
	quests[quest] = false
	Game.top_menu.add_quest(quest)
	#Game.quest_log.add_entry(quest)

func assign_quest(member : Adventurer, quest : Quest) -> void:
	member.assign_quest(quest)
	quests[quest] = true #Mark it as active

func spawn_visitor(pos : Vector2) -> void:
	var data : Adventurer = visitors["test"].data.instantiate()
	var sprite : AdventurerSprite = visitors["test"].sprite.instantiate()
	var r = randf()
	if r > 0.8:
		data.gender = 2 #Nonbinary
		if randf() > 0.5:
			data.given_name = femme_names.pick_random()
		else:
			data.given_name = masc_names.pick_random()
	elif r > 0.4:
		data.gender = 1 #Female presenting
		data.given_name = femme_names.pick_random()
	else:
		data.gender = 0 #Male presenting
		data.given_name = masc_names.pick_random()
	data.surname = surnames.pick_random()
	data.name = data.given_name + " " + data.surname
	data.job = jobs["Farmer"]
	data.generate()
	sprite.data = data
	hall.add_sprite(sprite)
	sprite.global_position = pos
