extends Spatial

export var camera_path: NodePath
export var camera_pos_path: NodePath
export var camera_rot_y_path: NodePath
export var camera_rot_x_path: NodePath
export var camera_offset_path: NodePath
export var camera_arm_path: NodePath
export var fps_camera_scene: PackedScene
export var fps_camera_rot_y_node: String = "fps_cam_rot_y"
export var fps_camera_rot_x_node: String = "fps_cam_rot_x"
export var fps_camera_node: String = "fps_camera"

var door_sc = preload("res://scenes/door_control.tscn")
var town_menu_e = preload("res://ui/town_menu.tscn")
var town_menu

# Declare member variables here. Examples:
# var a = 2
# var b = "text"


# Called when the node enters the scene tree for the first time.
func _ready():
	$waiting.show()
	characters.set_navmesh(null, Transform())
	scenario.connect("spawn_player", self, "spawn_player")
	if scenario.save_data.has("world"):
		RoadsData.emit_signal("startup", scenario.save_data.world)
	else:
		RoadsData.emit_signal("startup", Dictionary())
	town_menu = town_menu_e.instance()
	town_menu.hide()
	add_child(town_menu)
var start_delay0 = 2.0
var start_delay1 = 3.0
var start_delay2 = 30.0
var start_delay3 = 2.0
var start_delay4 = 2.0
var state = 0
var viewer

var player_place_intended
var player_place_fact

var player_spawned = false
var first_town = -1
var bandit_camp = -1

func spawn_player(xform: Transform):
	if !player_spawned:
		player_spawned = true
		if scenario.save_data.has("player_xform"):
			$player.global_transform = str2var(scenario.save_data.player_xform)
		else:
			$player.global_transform = xform

func _process(delta):
	match state:
		0:
			start_delay0 -= delta
			if start_delay0 < 0:
				state = 1
		1:
			start_delay1 -= delta
			first_town = RoadsData.get_first_city_site()
			assert(first_town >= 0)
			if start_delay1 < 0:
				if has_node("Roads") && get_node("Roads") is Roads:
					for site in range(RoadsData.get_site_count()):
						if site == first_town:
							streaming.setup_first_town(site)
						else:
							if RoadsData.site_is_town(site):
								if bandit_camp < 0:
									bandit_camp = site
									scenario.camp = streaming.setup_bandit_camp(site)
									scenario.camp_site = site
									streaming.setup_bandit_camp_outskirts(site)
								else:
									streaming.setup_town(site)
						streaming.setup_traffic(site)
						if site > first_town && bandit_camp >= 0:
							break
					print("towns done: ", streaming.towns)
				var poly = RoadsData.get_site_polygon_3d(0)
				var center = Vector3()
				for p in poly:
					center += p
				center /= poly.size()
				var infl = RoadsData.get_influence_cached(center.x, center.z, 256)
				center.y = infl.y
				var v = center
				$player.global_transform.origin.x = v.x
				$player.global_transform.origin.y = v.y + 1.0
				$player.global_transform.origin.z = v.z
				player_place_intended = $player.global_transform.origin
				viewer = VoxelViewer.new()
				$player.add_child(viewer)
				print("added viewer")
				state = 2
		2:
			Spawner.set_startup_xform($player.global_transform)
			if player_spawned:
				state = 3
		3:
			start_delay3 -= delta
			if start_delay3 < 0:
				print("start configuring player")
				for e in get_tree().get_nodes_in_group("spawn"):
					print("SPAWN! ", e.name)
					if e.name.begins_with("spawn_player"):
						$player.global_transform = e.global_transform
						e.queue_free()
						break
				var player
				var cam: Camera
				var fps_cam: Camera
				var cam_pos
				var cam_rot_y
				var cam_rot_x
				var cam_offset
				var cam_arm
				var fps_cam_rot_y
				var fps_cam_rot_x
				var default_offset
				player = characters.replace_player_character($player, "male", ["player", "cmdq", "player_clothes", "hurtboxes"])
				$player.remove_child(viewer)
				player.add_child(viewer)
#				player.add_child(VoxelViewer.new())
				player.set_script(load("res://characters/character.gd"))
				var fps_cam_pos = fps_camera_scene.instance()
				player.add_child(fps_cam_pos)
				fps_cam = fps_cam_pos.find_node(fps_camera_node, true, false)
				
				cam = get_node(camera_path)
				cam.current = true
				cam_pos = get_node(camera_pos_path)
				cam_rot_y = get_node(camera_rot_y_path)
				cam_rot_x = get_node(camera_rot_x_path)
				cam_offset = get_node(camera_offset_path)
				cam_arm = get_node(camera_arm_path)
				fps_cam_rot_y = fps_cam_pos.find_node(fps_camera_rot_y_node, true, false)
				fps_cam_rot_x = fps_cam_pos.find_node(fps_camera_rot_x_node, true, false)
				default_offset = cam_offset.translation
				cam.set_meta("cam_pos", cam_pos)
				cam.set_meta("cam_rot_y", cam_rot_y)
				cam.set_meta("cam_rot_x", cam_rot_x)
				cam.set_meta("cam_offset", cam_offset)
				cam.set_meta("cam_arm", cam_arm)
				cam.set_meta("default_offset", default_offset)
				fps_cam.set_meta("fps_cam_rot_y", fps_cam_rot_y)
				fps_cam.set_meta("fps_cam_rot_x", fps_cam_rot_x)
				
				cam_arm.add_excluded_object(player.get_rid())
				cam.set_meta("player", player)
				fps_cam.set_meta("player", player)
				player.set_meta("cam", cam)
				player.set_meta("fps_cam", fps_cam)
				print(player)
				controls.switch_fps_mode(false)
				print("configured player")
				player_place_fact = player.global_transform.origin
				streaming.emit_signal("character_ready")
				state = 4
		4:
			start_delay4 -= delta
			if start_delay4 < 0:
				$waiting.hide()
				print("starting game")
				print("intended: ", player_place_intended, " fact: ", player_place_fact)
				state = 5
		5:
			RoadsData.save_json("user://world-gen.json")
			state = 6
		6:
			if Input.is_action_pressed("action2") && !controls.is_gui:
				controls.is_gui = true
				town_menu.show()

