extends Node3D

@export_group("Rails")
@export var rail_path: Path3D 

@export_group("Biomes")
@export var biome_list: Array[Biome]

@export_group("Grid and Area")
@export var chunk_size: float = 20.0
@export var eye_line: int = 3 
@export var district_scale: float = 0.05 

@export_group("Lamppost")
@export var lamppost_wire_material: ShaderMaterial 
@export_range(0.01, 1.0) var wire_thickness: float = 0.05

var board: Dictionary = {}
var last_pos_train: Vector2i = Vector2i(999999, 999999) 
var noise_generator: FastNoiseLite 
var altitude_generator: FastNoiseLite
var wire_connections: Dictionary = {}

var manual_biome: Biome = null

func _ready() -> void:
	if biome_list.is_empty() or rail_path == null: return
		
	noise_generator = FastNoiseLite.new()
	noise_generator.noise_type = FastNoiseLite.TYPE_PERLIN
	noise_generator.seed = randi() 
	noise_generator.frequency = district_scale 
	
	altitude_generator = FastNoiseLite.new()
	altitude_generator.noise_type = FastNoiseLite.TYPE_PERLIN
	altitude_generator.seed = randi() 
	altitude_generator.frequency = district_scale * 0.5 
	
	_update_set_pieces()

func _update_set_pieces() -> void:
	var set_pieces = get_tree().get_nodes_in_group("set_pieces")
	
	print("🔍 Found ", set_pieces.size(), " in 'set_pieces' group.")
	
	for sp in set_pieces:
		if not "exclusive_biome" in sp or sp.exclusive_biome == "":
			print("⚠️ Set Piece '", sp.name, "' not have exclusive biome -> always visible.")
			continue 
			
		var local_biome = ""
		if manual_biome != null:
			local_biome = manual_biome.nome
		else:
			var grid_x = roundi(sp.global_position.x / chunk_size)
			var grid_z = roundi(sp.global_position.z / chunk_size)
			local_biome = _get_procedural_biome_name(noise_generator.get_noise_2d(grid_x, grid_z))
			
		print("Analyze: '", sp.name, "' | Need: [", sp.exclusive_biome, "] | Current biome is: [", local_biome, "]")			
		if local_biome == sp.exclusive_biome:
			sp.show()
			sp.process_mode = Node.PROCESS_MODE_INHERIT
			print("   ✅ Temple is visible.")
		else:
			sp.hide()
			sp.process_mode = Node.PROCESS_MODE_DISABLED 
			print("   ❌ Temple is an invisible ghost.")
	
func _destroy_and_regenrate_world() -> void:
	#Turn off old temples
	_update_set_pieces() 
	
	#Destroy all models
	for pos in board.keys():
		var cella = board[pos]
		if cella["tipo"] == "bioma":
			if cella.has("nodo") and is_instance_valid(cella["nodo"]):
				cella["nodo"].queue_free()
				
	#Clean board and wire connections
	board.clear()
	wire_connections.clear()
		
	#Create train
	last_pos_train = Vector2i(999999, 999999)
	_train_radar()
	last_pos_train = Vector2i(999999, 999999)
	_train_radar()

func _physics_process(_delta: float) -> void:
	if rail_path == null or rail_path.train_instance == null: return
		
	var train_pos = rail_path.train_instance.global_position
	var grid_x = roundi(train_pos.x / chunk_size)
	var grid_z = roundi(train_pos.z / chunk_size)
	var current_pos = Vector2i(grid_x, grid_z)
	
	if current_pos != last_pos_train:
		last_pos_train = current_pos
		_generate_pieces_around_train(current_pos)
		_clean_far_chunks(current_pos)
		_train_radar()

func collect_all_chunkinfo(root: Node, list: Array[Node]) -> void:
	if root == null: return
	
	if root.has_method("get_rotated_data") or "have_lamppost" in root:
		list.append(root)
		
	for child in root.get_children():
		collect_all_chunkinfo(child, list)

func _generate_pieces_around_train(center: Vector2i) -> void:
	for x in range(-eye_line, eye_line + 1):
		for z in range(-eye_line, eye_line + 1):
			var grid_pos = center + Vector2i(x, z)
			
			if not board.has(grid_pos):
				var ce_obstacle = _register_cell_with_ray(grid_pos)
				if not ce_obstacle:
					_add_compatible_biome(grid_pos)

func _choose_catalogue_by_cell(grid_pos: Vector2i) -> Array[PackedScene]:
	if manual_biome != null:
		return manual_biome.available_chunks
		
	var value = noise_generator.get_noise_2d(grid_pos.x, grid_pos.y)
	var normalized_value = (value + 1.0) / 2.0 
	var indice = clamp(int(normalized_value * biome_list.size()), 0, biome_list.size() - 1)
	return biome_list[indice].available_chunks

func _get_procedural_biome_name(value: float) -> String:
	if manual_biome != null:
		return manual_biome.name
		
	var normalized_value = (value + 1.0) / 2.0
	var index = clamp(int(normalized_value * biome_list.size()), 0, biome_list.size() - 1)
	return biome_list[index].name

func _register_cell_with_ray(grid_pos: Vector2i) -> bool:
	if board.has(grid_pos): return true 
	
	var world_pos = Vector3(grid_pos.x * chunk_size, 0, grid_pos.y * chunk_size)
	var space_state = get_world_3d().direct_space_state
	var ray_origin = Vector3(world_pos.x, 50.0, world_pos.z)
	var ray_end = Vector3(world_pos.x, -10.0, world_pos.z)
	
	var query = PhysicsRayQueryParameters3D.create(ray_origin, ray_end)
	var result = space_state.intersect_ray(query)
	
	if result.size() > 0:
		var collider = result["collider"]
		var root_chunk = collider
		var info_list: Array[Node] = []

		#Find parent node
		while root_chunk != null and root_chunk != get_tree().root:
			collect_all_chunkinfo(root_chunk, info_list)
			if info_list.size() > 0:
				break
			root_chunk = root_chunk.get_parent()

		var right_info_node = null

		#Find closest chunk to the ray
		if info_list.size() > 0:
			var min_distance = 999999.0
			var center_cell_pos = Vector3(world_pos.x, 0, world_pos.z)
			
			for info in info_list:
				if info is Node3D:
					var pos_info = Vector3(info.global_position.x, 0, info.global_position.z)
					var dist = center_cell_pos.distance_to(pos_info)
					
					if dist < min_distance:
						min_distance = dist
						right_info_node = info
						
		#If no node is found use first element
		if right_info_node == null and info_list.size() > 0:
			right_info_node = info_list[0]

		var exit_found = {"north": false, "est": false, "south": false, "west": false}
		var height_found = {"north": 0, "est": 0, "south": 0, "west": 0}
		var have_lamppost = false
		
		#Get node info
		if right_info_node != null:
			if right_info_node.has_method("get_rotated_data"):
				var rotation_steps = roundi(rad_to_deg(right_info_node.global_rotation.y) / -90.0)
				var data = right_info_node.get_rotated_data(rotation_steps)
				exit_found = data["connections"]
				height_found = data["heights"]
				
			if "have_lamppost" in right_info_node:
				have_lamppost = right_info_node.have_lamppost

		#Add piece to the grid
		board[grid_pos] = {
			"type": "obstacle", 
			"exit": exit_found, 
			"heights": height_found, 
			"node": root_chunk,  
			"info": right_info_node,     
			"have_lamppost": have_lamppost
		}
		
		if have_lamppost:
			_connect_lamppost_wires(grid_pos)
			
		return true 
	return false 

func _add_compatible_biome(grid_pos: Vector2i) -> void:
	var req_conn_north = _needed_connection(grid_pos + Vector2i(0, -1), "south")
	var req_conn_est = _needed_connection(grid_pos + Vector2i(1, 0), "west")
	var req_conn_south = _needed_connection(grid_pos + Vector2i(0, 1), "north")
	var req_conn_west = _needed_connection(grid_pos + Vector2i(-1, 0), "est")
	
	var req_height_north = _needed_heights(grid_pos + Vector2i(0, -1), "south")
	var req_height_est = _needed_heights(grid_pos + Vector2i(1, 0), "west")
	var req_height_south = _needed_heights(grid_pos + Vector2i(0, 1), "north")
	var req_height_west = _needed_heights(grid_pos + Vector2i(-1, 0), "est")
	
	var height_noise = altitude_generator.get_noise_2d(grid_pos.x, grid_pos.y)
	var height_target = clamp(roundi((height_noise + 1.0) / 2.0), 0, 1)
	
	var zone_catalogue = _choose_catalogue_by_cell(grid_pos)
	var valid_candidates = []
	
	for scene in zone_catalogue:
		var test_chunk = scene.instantiate()
		var test_list: Array[Node] = []
		collect_all_chunkinfo(test_chunk, test_list)
		var info_test = test_list[0] if test_list.size() > 0 else null

		if info_test != null and info_test.has_method("get_rotated_data"):
			for rot in range(4):
				var data = test_chunk.get_rotated_data(rot)
				var u_conn = data["connections"]
				var u_height = data["heights"]
				
				var match_conn_n = (req_conn_north == -1) or ((req_conn_north == 1) == u_conn["north"])
				var match_conn_e = (req_conn_est == -1)  or ((req_conn_est == 1) == u_conn["est"])
				var match_conn_s = (req_conn_south == -1)  or ((req_conn_south == 1) == u_conn["south"])
				var match_conn_o = (req_conn_west == -1) or ((req_conn_west == 1) == u_conn["west"])
				
				var match_alt_n = (req_height_north == -1) or (req_height_north == u_height["north"])
				var match_alt_e = (req_height_est == -1)  or (req_height_est == u_height["est"])
				var match_alt_s = (req_height_south == -1)  or (req_height_south == u_height["south"])
				var match_alt_o = (req_height_west == -1) or (req_height_west == u_height["west"])
				
				var diff_n = abs(u_height["north"] - height_target) if req_height_north == -1 else 0
				var diff_e = abs(u_height["est"] - height_target) if req_height_est == -1 else 0
				var diff_s = abs(u_height["south"] - height_target) if req_height_south == -1 else 0
				var diff_o = abs(u_height["west"] - height_target) if req_height_west == -1 else 0
				var excessive_jumps = diff_n > 1 or diff_e > 1 or diff_s > 1 or diff_o > 1
				
				if match_conn_n and match_conn_e and match_conn_s and match_conn_o and match_alt_n and match_alt_e and match_alt_s and match_alt_o and not excessive_jumps:
					var score = 0
					if req_height_north == -1 and u_height["north"] == height_target: score += 1
					if req_height_est == -1 and u_height["est"] == height_target: score += 1
					if req_height_south == -1 and u_height["south"] == height_target: score += 1
					if req_height_west == -1 and u_height["west"] == height_target: score += 1
					
					valid_candidates.append({"scene": scene, "rotation": rot, "data": data, "score": score})
					
		test_chunk.queue_free()
		
	if valid_candidates.size() > 0:
		var max_score = -1
		for c in valid_candidates:
			if c.score > max_score: max_score = c.score
				
		var best_candidate = []
		for c in valid_candidates:
			if c.score == max_score: best_candidate.append(c)
				
		var choise = best_candidate.pick_random()
		var new_chunk = choise.scene.instantiate()
		add_child(new_chunk)
		new_chunk.position = Vector3(grid_pos.x * chunk_size, 0, grid_pos.y * chunk_size)
		new_chunk.rotation.y = choise.rotation * (-PI / 2.0)
		
		var new_list: Array[Node] = []
		collect_all_chunkinfo(new_chunk, new_list)
		var info_new = new_list[0] if new_list.size() > 0 else null
		
		var have_lamppost = false
		if info_new != null and "have_lamppost" in new_chunk:
			have_lamppost = info_new.have_lamppost
			
		board[grid_pos] = {
			"type": "bioma", 
			"exit": choise.data["connections"], 
			"heights": choise.data["heights"], 
			"node": new_chunk, 
			"info": info_new,
			"have_lamppost": have_lamppost
		}
		
		if have_lamppost:
			_connect_lamppost_wires(grid_pos)

	else:	
		var backup = zone_catalogue[0].instantiate()
		add_child(backup)
		backup.position = Vector3(grid_pos.x * chunk_size, 0, grid_pos.y * chunk_size)
		
		var backup_list: Array[Node] = []
		collect_all_chunkinfo(backup, backup_list)
		var info_backup = backup_list[0] if backup_list.size() > 0 else null
	
		var safe_heights = {
			"north": req_height_north if req_height_north != -1 else height_target,
			"est": req_height_est if req_height_est != -1 else height_target,
			"south": req_height_south if req_height_south != -1 else height_target,
			"west": req_height_west if req_height_west != -1 else height_target
		}
		board[grid_pos] = {
			"type": "biome", 
			"exit": {"north":false, "est":false, "south":false, "west":false}, 
			"heights": safe_heights, 
			"node": backup,
			"info": info_backup,
			"have_lamppost": false
		}

func _needed_connection(near_pos: Vector2i, side_needed: String) -> int:
	if not board.has(near_pos): 
		_register_cell_with_ray(near_pos)
	if board.has(near_pos) and board[near_pos].has("exit"):
		return 1 if board[near_pos]["exit"][side_needed] else 0 
	return -1 

func _needed_heights(near_pos: Vector2i, side_needed: String) -> int:
	if board.has(near_pos) and board[near_pos].has("heights"):
		return board[near_pos]["heights"][side_needed]
	return -1 

func _train_radar() -> void:
	if rail_path == null or rail_path.curve == null or rail_path.train_instance == null: return
		
	if manual_biome != null:
		return
		
	var train_pos = rail_path.train_instance.global_position
	var grid_x = roundi(train_pos.x / chunk_size)
	var grid_z = roundi(train_pos.z / chunk_size)
	var current_biome = _get_procedural_biome_name(noise_generator.get_noise_2d(grid_x, grid_z))
	
	var current_progress = rail_path.train_progress
	var total_length = rail_path.curve.get_baked_length()
	#var exchange_distance = 0.0
	#var next_biome = ""

	for step in range(1, 31):
		var next_progress = wrapf(current_progress + (step * chunk_size), 0.0, total_length)
		var next_local_pos = rail_path.curve.sample_baked(next_progress, true)
		var next_global_pos = rail_path.to_global(next_local_pos)
		
		var f_x = roundi(next_global_pos.x / chunk_size)
		var f_z = roundi(next_global_pos.z / chunk_size)
		var future_biome = _get_procedural_biome_name(noise_generator.get_noise_2d(f_x, f_z))
		
		if future_biome != current_biome:
			#exchange_distance = step * chunk_size
			#next_biome = future_biome
			break
			
func _clean_far_chunks(centro_attuale: Vector2i) -> void:
	var cells_to_remove = []
	var safe_margin = 2 
	
	for grid_pos in board.keys():
		var cell = board[grid_pos]
		if cell["type"] == "obstacle": continue
			
		var dist_x = abs(grid_pos.x - centro_attuale.x)
		var dist_z = abs(grid_pos.y - centro_attuale.y)
		
		if dist_x > eye_line + safe_margin or dist_z > eye_line + safe_margin:
			if cell.has("node") and is_instance_valid(cell["node"]):
				cell["node"].queue_free()
			cells_to_remove.append(grid_pos)
			
	for pos in cells_to_remove:
		board.erase(pos)


func _get_lampposts(info_node: Node, side: String) -> Array[Node3D]:
	var lampposts: Array[Node3D] = []
	if side == "sx":
		if "connection_left" in info_node and info_node.connection_left != null:
			for p in info_node.connection_left: 
				if is_instance_valid(p): lampposts.append(p)
		#if lampposts.is_empty() and "nodo_attacco_sx" in nodo_info and is_instance_valid(nodo_info.get("nodo_attacco_sx")):
		#	lampposts.append(nodo_info.get("nodo_attacco_sx"))
	else:
		if "connection_right" in info_node and info_node.connection_right != null:
			for p in info_node.connection_right: 
				if is_instance_valid(p): lampposts.append(p)
		#if lampposts.is_empty() and "nodo_attacco_dx" in nodo_info and is_instance_valid(nodo_info.get("nodo_attacco_dx")):
		#	lampposts.append(nodo_info.get("nodo_attacco_dx"))
	return lampposts

func _connect_lamppost_wires(new_board_pos: Vector2i) -> void:
	var new_cell = board[new_board_pos]
	var new_root = new_cell["node"]
	var new_info = new_cell["info"]
	
	if new_info == null: return
	if new_info is Node3D: new_info.force_update_transform()

	var new_sx = _get_lampposts(new_info, "sx")
	var new_dx = _get_lampposts(new_info, "dx")
	if new_sx.is_empty() or new_dx.is_empty(): return
	
	var new_id = new_root.get_instance_id()
	if not wire_connections.has(new_id): wire_connections[new_id] = 0

	if wire_connections[new_id] >= 2: return

	var p_sx_my_best = null; var p_dx_my_best = null
	var p_sx_your_best = null; var p_dx_your_best = null
	var best_closest_to_root = null
	var best_distance = 999999.0
	var ray_research = 3 
	
	for x in range(-ray_research, ray_research + 1):
		for z in range(-ray_research, ray_research + 1):
			if x == 0 and z == 0: continue
			
			var closest_post = new_board_pos + Vector2i(x, z)
			if board.has(closest_post) and board[closest_post].get("have_lamppost", false):
				var closest_root = board[closest_post]["node"]
				var closest_info = board[closest_post]["info"]
				
				if not is_instance_valid(closest_root) or closest_root == new_root or closest_info == null: continue
				
				var closest_id = closest_root.get_instance_id()
				var closest_connections = wire_connections.get(closest_id, 0)
				
				if closest_connections >= 2: continue
				
				if closest_info is Node3D: closest_info.force_update_transform()
				var closest_sx = _get_lampposts(closest_info, "sx")
				var closest_dx = _get_lampposts(closest_info, "dx")
				if closest_sx.is_empty() or closest_dx.is_empty(): continue
				
				for i_m in range(new_sx.size()):
					for i_t in range(closest_sx.size()):
						var my_c = (new_sx[i_m].global_position + new_dx[i_m].global_position) / 2.0
						var your_c = (closest_sx[i_t].global_position + closest_dx[i_t].global_position) / 2.0
						var dist = my_c.distance_to(your_c)
						
						if dist < best_distance:
							best_distance = dist
							best_closest_to_root = closest_root
							p_sx_my_best = new_sx[i_m]
							p_dx_my_best = new_dx[i_m]
							p_sx_your_best = closest_sx[i_t]
							p_dx_your_best = closest_dx[i_t]

	if best_closest_to_root == null:
		for x in range(-ray_research, ray_research + 1):
			for z in range(-ray_research, ray_research + 1):
				if x == 0 and z == 0: continue
				
				var closest_pos = new_board_pos + Vector2i(x, z)
				if board.has(closest_pos) and board[closest_pos].get("have_lamppost", false):
					var closest_root = board[closest_pos]["node"]
					var closest_info = board[closest_pos]["info"]
					
					if not is_instance_valid(closest_root) or closest_root == new_root or closest_info == null: continue
					
					if closest_info is Node3D: closest_info.force_update_transform()
					var closest_sx = _get_lampposts(closest_info, "sx")
					var closest_dx = _get_lampposts(closest_info, "dx")
					if closest_sx.is_empty() or closest_dx.is_empty(): continue
					
					for i_m in range(new_sx.size()):
						for i_t in range(closest_sx.size()):
							var c_mio = (new_sx[i_m].global_position + new_dx[i_m].global_position) / 2.0
							var c_tuo = (closest_sx[i_t].global_position + closest_dx[i_t].global_position) / 2.0
							var dist = c_mio.distance_to(c_tuo)
							
							if dist < best_distance:
								best_distance = dist
								best_closest_to_root = closest_root
								p_sx_my_best = new_sx[i_m]
								p_dx_my_best = new_dx[i_m]
								p_sx_your_best = closest_sx[i_t]
								p_dx_your_best = closest_dx[i_t]

	var max_dist = chunk_size * 8.0 
	
	if best_closest_to_root != null and best_distance < max_dist:
		var dist_streight = p_sx_my_best.global_position.distance_to(p_sx_your_best.global_position) + p_dx_my_best.global_position.distance_to(p_dx_your_best.global_position)
		var dist_cross = p_sx_my_best.global_position.distance_to(p_dx_your_best.global_position) + p_dx_my_best.global_position.distance_to(p_sx_your_best.global_position)

		if dist_streight <= dist_cross:
			_draw_parable(p_sx_my_best.global_position, p_sx_your_best.global_position, new_root)
			_draw_parable(p_dx_my_best.global_position, p_dx_your_best.global_position, new_root)
		else:
			_draw_parable(p_sx_my_best.global_position, p_dx_your_best.global_position, new_root)
			_draw_parable(p_dx_my_best.global_position, p_sx_your_best.global_position, new_root)

		wire_connections[new_id] += 1
		var closest_id = best_closest_to_root.get_instance_id()
		if not wire_connections.has(closest_id): wire_connections[closest_id] = 0
		wire_connections[closest_id] += 1

func _draw_parable(p1: Vector3, p2: Vector3, parent: Node3D) -> void:
	var segments = 15
	var lowering = 1.5 
	var curve_points = []
	var sag_factors = []
	
	for i in range(segments + 1):
		var t = float(i) / float(segments)
		var global_post = p1.lerp(p2, t)
		var gravity = lowering * (1.0 - pow(2.0 * t - 1.0, 2.0))
		global_post.y -= gravity
		
		curve_points.append(parent.to_local(global_post))
		sag_factors.append(sin(t * PI))

	var st = SurfaceTool.new()
	st.begin(Mesh.PRIMITIVE_TRIANGLES)
	
	var lati = 4 
	var ray = wire_thickness / 2.0
	
	for i in range(curve_points.size() - 1):
		var p_current = curve_points[i]
		var p_next = curve_points[i+1]
		var sag_currnet = sag_factors[i]
		var sag_next = sag_factors[i+1]
		
		var dir = (p_next - p_current).normalized()
		var up = Vector3.UP
		var right = dir.cross(up).normalized()
		if right.length_squared() < 0.01:
			right = dir.cross(Vector3.RIGHT).normalized()
		up = right.cross(dir).normalized()
		
		for s in range(lati):
			var ang1 = (float(s) / lati) * TAU
			var ang2 = (float((s + 1) % lati) / lati) * TAU
			var offset1 = (right * cos(ang1) + up * sin(ang1)) * ray
			var offset2 = (right * cos(ang2) + up * sin(ang2)) * ray
			
			st.set_color(Color(sag_currnet, 0, 0, 1))
			st.add_vertex(p_current + offset1)
			st.set_color(Color(sag_currnet, 0, 0, 1))
			st.add_vertex(p_current + offset2)
			st.set_color(Color(sag_next, 0, 0, 1))
			st.add_vertex(p_next + offset1)
			
			st.set_color(Color(sag_currnet, 0, 0, 1))
			st.add_vertex(p_current + offset2)
			st.set_color(Color(sag_next, 0, 0, 1))
			st.add_vertex(p_next + offset2)
			st.set_color(Color(sag_next, 0, 0, 1))
			st.add_vertex(p_next + offset1)
			
	var wire_node = MeshInstance3D.new()
	wire_node.mesh = st.commit()
	if lamppost_wire_material: wire_node.material_override = lamppost_wire_material
	parent.add_child(wire_node)
