extends Node
class_name PlaceMap

var temp_map_height :int = 1
var temp_map_x :int = 32
var temp_map_z :int = 32
var tile_map :Dictionary = {}
var wall_map :Dictionary = {}
var room_map :Dictionary = {}
var place_nav :AStar3D = AStar3D.new()

enum TILE_STATE {CLOSED, OPEN}
enum WALL_STATE {OPEN, CLOSED, DOOR}

const direction_vectors :Array = [
	Vector3i(0, 0, -12),
	Vector3i(12, 0, 0),
	Vector3i(0, 0, 12),
	Vector3i(-12, 0, 0)
]

func _process(_delta: float) -> void:
	var color_tile_built :Color = Color(0.939, 0.741, 0.73, 1.0)
	var color_wall_built :Color = Color(0.81, 0.299, 0.316, 1.0)
	var built_tiles :Array = []
	var built_walls :Array = []
	var tiles :Array = []
	var walls :Array = []
	var astar_points :Array = place_nav.get_point_ids()
	var astar_pos :Array = []
	for i :Vector3i in tile_map.keys():
		var pos :Vector3 = Vector3(i) / 12
		if tile_map[i]["state"] == TILE_STATE.CLOSED:
			tiles.append(pos)
		elif tile_map[i]["state"] == TILE_STATE.OPEN:
			built_tiles.append(pos)
	for i :Vector3i in wall_map.keys():
		var pos :Vector3 = Vector3(i) / 12
		if wall_map[i]["state"] == WALL_STATE.OPEN:
			walls.append(pos)
		elif wall_map[i]["state"] == WALL_STATE.CLOSED:
			built_walls.append(pos)
	for i :int in astar_points:
		astar_pos.append(Vector3(place_nav.get_point_position(i)) / 12)
	DebugDraw3D.draw_points(built_tiles, 0, 0.2, color_tile_built)
	DebugDraw3D.draw_points(built_walls, 0, 0.2, color_wall_built)
	DebugDraw3D.draw_points(tiles, 0, 0.2, Color(0.459, 0.404, 0.866, 1.0))
	DebugDraw3D.draw_points(walls, 0, 0.2, Color(0.343, 0.409, 0.528, 1.0))
	DebugDraw3D.draw_points(astar_pos, 0, 0.2, Color(0.657, 0.004, 0.956, 1.0))

func temp_create_map() -> void:
	#TEMP: just makes a square. Should be replaced with premade maps eventually.
	for y :int in range(temp_map_height):
		for x :int in range(temp_map_x):
			for z :int in range(temp_map_z):
				var map_pos :Vector3i = Vector3i(x * 12, y, z * 12)
				tile_map[map_pos] = {
					"object": null,
					"state": TILE_STATE.CLOSED
				}
	
	for i :Vector3i in tile_map.keys():
		for v :Vector3i in direction_vectors:
			var map_pos :Vector3i = i - (v / 2)
			wall_map[map_pos] = {
				"object": null,
				"state": WALL_STATE.OPEN,
			}
			
func give_walls(tile :Tile) -> void:
	var temp_dict :Dictionary
	for i :Vector3i in direction_vectors:
		if tile_map.has(tile.map_position + i):
			var neighbor :Tile = tile_map[tile.map_position + i]["object"]
			var room :Room = tile.lookup_tile_to_room
			if not room_map[room]["tiles"].has(neighbor):
				temp_dict[Vector3(i / 2) / 12] = wall_map[tile.map_position + (i / 2)]["state"]
	if temp_dict:
		tile.face_dict = temp_dict.duplicate()

func give_nav(room :Room) -> void:
	var nav :AStar3D = room.nav
	nav.clear()
	var pos_array :Array = []
	for i :Tile in room_map[room]["tiles"]:
		var id :int = nav.get_available_point_id()
		var point_pos :Vector3 = Vector3(i.place_position)
		nav.add_point(id, point_pos)
		pos_array.append(point_pos)

		for n :Vector3 in Tile.direction_vector_dict.values():
			var neighbor_pos :Vector3 = point_pos + Vector3(n)
			if pos_array.has(neighbor_pos):
				var closest :int = nav.get_closest_point(neighbor_pos)
				if (neighbor_pos) == Vector3(nav.get_point_position(closest)):
					nav.connect_points(id, closest)

func give_path(actor :Actor, start :Tile, end :Tile) -> void:
#	TODO: Sloppy

	if tile_map[end.map_position]["state"] == TILE_STATE.OPEN:
		
		if start.lookup_tile_to_room == end.lookup_tile_to_room:
			actor.current_task_path = [start.map_position, end.map_position]
			return
		
		var start_id :int = place_nav.get_available_point_id()
		place_nav.add_point(start_id, start.map_position)
		var start_room_doors :Array = room_map[start.lookup_tile_to_room]["doors"]
		if start_room_doors == []:
			actor.temp_complete_task()
			return
		for i :Vector3i in start_room_doors:
			var door_id :int = place_nav.get_closest_point(i)
			place_nav.connect_points(start_id, door_id)
			
		var end_id :int = place_nav.get_available_point_id()
		place_nav.add_point(end_id, end.map_position)
		var end_room_doors :Array = room_map[end.lookup_tile_to_room]["doors"]
		if end_room_doors == []:
			actor.temp_complete_task()
			return
		for i :Vector3i in end_room_doors:
			var door_id :int = place_nav.get_closest_point(i)
			place_nav.connect_points(end_id, door_id)
		var place_path_id :Array = place_nav.get_id_path(start_id, end_id)
		var place_path_pos :Array = []
		for i :int in place_path_id:
			place_path_pos.append(place_nav.get_point_position(i))
		actor.current_task_path = place_path_pos
		place_nav.remove_point(start_id)
		place_nav.remove_point(end_id)
	else:
		actor.temp_complete_task()

func write_door(pos :Vector3i) -> void:
	var tile_1_pos :Vector3i
	var tile_2_pos :Vector3i
	if pos.x % 12 == 0:
		tile_1_pos = pos + Vector3i(0, 0, -6)
		tile_2_pos = pos + Vector3i(0, 0, 6)
	else:
		tile_1_pos = pos + Vector3i(-6, 0, 0)
		tile_2_pos = pos + Vector3i(6, 0, 0)
	var tile_1 :Tile = tile_map[tile_1_pos]["object"]
	var tile_2 :Tile = tile_map[tile_2_pos]["object"]
	
	wall_map[pos]["state"] = WALL_STATE.DOOR
	tile_1.update_faces()
	tile_2.update_faces()
	
	room_map[tile_1.lookup_tile_to_room]["tiles"].append(tile_2)
	tile_1.lookup_tile_to_room.update_nav()
	var point_pos_1 :Vector3i = pos - ((pos - tile_1_pos) / 2)
	var id_1 :int = place_nav.get_available_point_id()
	place_nav.add_point(id_1, point_pos_1)
	for i :Vector3i in room_map[tile_1.lookup_tile_to_room]["doors"]:
		var id :int = place_nav.get_closest_point(i)
		place_nav.connect_points(id_1, id)
	room_map[tile_1.lookup_tile_to_room]["doors"].append(point_pos_1)
	
	room_map[tile_2.lookup_tile_to_room]["tiles"].append(tile_1)
	tile_2.lookup_tile_to_room.update_nav()
	var point_pos_2 :Vector3i = pos - ((pos - tile_2_pos) / 2)
	var id_2 :int = place_nav.get_available_point_id()
	place_nav.add_point(id_2, point_pos_2)
	for i :Vector3i in room_map[tile_2.lookup_tile_to_room]["doors"]:
		var id :int = place_nav.get_closest_point(i)
		place_nav.connect_points(id_2, id)
	room_map[tile_2.lookup_tile_to_room]["doors"].append(point_pos_2)
	
	place_nav.connect_points(id_1, id_2)

func give_room(actor, start, end) -> void:
	for i in room_map.keys():
		for j in room_map[i]["tiles"]:
			if Vector3i(end) == j.map_position:
				actor.current_room_path = i.get_room_path(start, j.place_position)
				return
		for j in room_map[i]["doors"]:
			if Vector3i(end) == j:
				actor.current_room_path = i.get_room_path(start, Vector3i(round(Vector3(end) / 12)))
				return
