extends Node3D
class_name Tile

var place_id: int = 0

#This tile's current construction and selection modes.
@export var construction_mode :int = 0: set = update_construction
@export var selection_mode :int = 0: set = update_selection

const orange = preload("res://tiles/base_tile/orange.tres")
const gray = preload("res://tiles/base_tile/gray.tres")
const red = preload("res://tiles/base_tile/red.tres")
const blue = preload("res://tiles/base_tile/blue.tres")
const lightblue = preload("res://tiles/base_tile/lightblue.tres")

#Lists of possible states for various modes.
enum SEL_MODE {NONE, ROOM, BUILD, INVALID}

enum CON_MODE {NONE, NON_INTERACTABLE, CLOSED, OPEN, BUILT, REINFORCED, CONSTRUCTION}

#List of vectors for each direction a wall can be in
const direction_vector_dict = {
	"North": Vector3i(0, 0, -1),
	"East": Vector3i(1, 0, 0),
	"South": Vector3i(0, 0, 1),
	"West": Vector3i(-1, 0, 0),
}
#TODO: replace this with better math on the above dict
const wall_position_dict = {
	"North": Vector3i(0, 0, 0),
	"East": Vector3i(1, 0, 0),
	"South": Vector3i(1, 0, 1),
	"West": Vector3i(0, 0, 1),
}

const load_wall = preload("res://tiles/base_tile/base_wall.tscn")

var map_position: Vector3i
var place_position: Vector3i

var face_dict :Dictionary :set = build_faces

var lookup_tile_to_room: Object
var lookup_connected_rooms: Array = []

var neighbor_dict = {}

signal neighbor_request
signal wall_request

func save():
	
	#var room_ids: Array
	#for i in lookup_tile_to_room:
		#room_ids.append(i.get_instance_id())
	
	var save_data = {
#Basics
		"id": self.get_instance_id(),
		"type": "Tile",
		"scene_file_path": scene_file_path,
		"parent": get_parent().get_path(),
#Connections
		"place_id": place_id,
		"main_room": lookup_tile_to_room,
		"connected_rooms": lookup_connected_rooms,
#Data
		"position": position,
		"map_position": map_position,
		"face_dict": face_dict.keys(),
		"construction_mode": construction_mode,
	}
	return save_data



func update_faces() -> void:
#Changes the walls when relevant
	emit_signal("wall_request", self) 
	
func build_faces(dict :Dictionary) -> void:
	for i :Vector3 in dict:
		match dict[i]:
			PlaceMap.WALL_STATE.OPEN:
				face_dict[i].queue_free()
			PlaceMap.WALL_STATE.CLOSED:
				var wall :Wall = load_wall.instantiate()
				wall.position = Vector3(i)
				$Walls.add_child(wall)
				match i:
					Vector3(0, 0, 0.5):
						wall.rotation_degrees = Vector3(0, -180, 0)
					Vector3(-0.5, 0, 0):
						wall.rotation_degrees = Vector3(0, -270, 0)
					Vector3(0.5, 0, 0):
						wall.rotation_degrees = Vector3(0, -90, 0)
				face_dict[i] = wall
			PlaceMap.WALL_STATE.DOOR:
				face_dict[i].queue_free()
	
	#for i in new_dict.keys():
		#if face_dict.has(i):
			#if face_dict[i] == new_dict[i]:
				#continue
		#
			#face_dict[i].queue_free()
			#face_dict[i] = null
		#
		#if new_dict[i] is Door:
			#face_dict[i] = new_dict[i]
			#continue
		#var direction: String = direction_vector_dict.find_key(i)
		#var wall = load_wall.instantiate()
		#wall.position = wall_position_dict[direction]
		#wall.name = str(direction, "Wall")
		#match direction:
			#"East":
				#wall.rotation_degrees = Vector3(0, -90, 0)
			#"South":
				#wall.rotation_degrees = Vector3(0, -180, 0)
			#"West":
				#wall.rotation_degrees = Vector3(0, -270, 0)
		#$Walls.add_child(wall)
		#face_dict[i] = wall

func update_neighbors():
#Requests the current neighboring tiles

	neighbor_dict = {}
	emit_signal("neighbor_request", self, map_position, direction_vector_dict)
	pass

func update_construction(mode):
#Changes the tile based on its construction mode

	construction_mode = mode
	
	match mode:
		CON_MODE.NON_INTERACTABLE:
			face_dict = {}
		
		CON_MODE.CLOSED:
			face_dict = {}
			$Floor/FloorMesh.set_material_override(gray)
		
		CON_MODE.OPEN:
			face_dict = {}
	
		CON_MODE.BUILT:
			$Floor/FloorMesh.set_material_override(null)
			update_faces()
			

				
				#var temp_face_dict = {}
				#
				#for i in neighbor_dict.keys():
					#if not neighbor_dict[i].lookup_tile_to_room == lookup_tile_to_room:
						#temp_face_dict[i] = null
			#
				#face_dict = temp_face_dict.duplicate()
	
		CON_MODE.REINFORCED:
			face_dict = {}
		
		CON_MODE.CONSTRUCTION:
			face_dict = {}

func update_selection(mode):
#Changes the tile based on whether it has been selected

	selection_mode = mode
	
	match mode:
		SEL_MODE.NONE:
			$Floor/FloorMesh.set_material_overlay(null)
		
		SEL_MODE.ROOM:
			for i in $Walls.get_children():
				i.Mesh.set_material_override(orange)
			
		SEL_MODE.BUILD:
			$Floor/FloorMesh.set_material_overlay(lightblue)
		
		SEL_MODE.INVALID:
			$Floor/FloorMesh.set_material_overlay(orange)
