extends Node3D

@export var multiplier = 20

const TILE : PackedScene = preload("uid://btmuoxdocev5r")
const TILE_DARK = preload("uid://lc84reoxexjp")
const TILE_LIGHT = preload("uid://drsd4o5kptjhm")
var board_size = Vector2i(8, 8)
var all_tiles = []

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	var tile_array2 = generate_tile_array(board_size, 2)
	generate_board_from_array(tile_array2)
	pass
	
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass
	
func generate_board_from_array(tile_array):
	# Board spacing
	var TILE_SPACING := 2.0

	for data in tile_array:
		# Read coord + height from your array entries
		var coord = data.get("coord", Vector2(0, 0))
		var y_offset = data.get("tile_y_offset")

		# Support Vector2 or Vector2i
		var x := int(coord.x)
		var z := int(coord.y)
		
		all_tiles.append(Vector2i(x, z))

		if TILE.can_instantiate():
			var tile := TILE.instantiate() as Tile
			var mesh := tile.get_child(0) as MeshInstance3D

			# Checker materials (same logic you had)
			if x % 2 == 0:
				if z % 2 == 0:
					mesh.set_surface_override_material(0, TILE_DARK)
				else:
					mesh.set_surface_override_material(0, TILE_LIGHT)
			else:
				if z % 2 == 0:
					mesh.set_surface_override_material(0, TILE_LIGHT)
				else:
					mesh.set_surface_override_material(0, TILE_DARK)

			tile.position = Vector3(x * TILE_SPACING, y_offset, z * TILE_SPACING)
			tile.name = "fffx%dz%d" % [x, z]
			add_child(tile)

			# Your coord setter (assuming Tile expects Vector2i)
			tile.set_coord(Vector2i(x, z))

	# Connect signals after adding
	for child in get_children():
		child.is_clicked.connect(_on_tile_clicked)
	
#generate simple 8x8 board
func generate_board():
	for x in range(8):
		for z in range(8):
			if TILE.can_instantiate():
				var tile = TILE.instantiate() as Tile
				var mesh = tile.get_child(0) as MeshInstance3D
				mesh.set_surface_override_material(0, TILE_DARK)
				
				tile.translate(Vector3(x*2, 0, z*2))
				tile.name = "fffx{0}z{1}".format([x,z])
				add_child(tile)
				#tile.is_clicked.connect(board.on_tile_clicked)
				#board.
				#tile.connect("is_clicked", board, "on_tile_clicked")
				#tile.owner = get_scene()
				tile.set_coord(Vector2i(x,z))
				if x % 2 == 0:
					if z % 2 == 0:
						mesh.set_surface_override_material(0, TILE_DARK)
					else:
						mesh.set_surface_override_material(0, TILE_LIGHT)
				else:
					if z % 2 == 0:
						mesh.set_surface_override_material(0, TILE_LIGHT)
					else:
						mesh.set_surface_override_material(0, TILE_DARK)
				#mesh.owner = get_scene()
	for child in get_children():
		child.is_clicked.connect(_on_tile_clicked)
	pass # Replace with function body.


const FAST_NOISE_LITE = preload("uid://ba38fsam4t5xj")
	
func generate_tile_array(grid_dimensions: Vector2, smoothing_iterations: int = 4) -> Array:
	var rng: RandomNumberGenerator = RandomNumberGenerator.new()
	rng.randomize()

	var tiles: Array = []

	var grid_width: int = int(grid_dimensions.x)
	var grid_height: int = int(grid_dimensions.y)

	# elevation_map[z][x] -> float
	var elevation_map: Array = []

	# ----------------------------
	# STEP 3: build your tile array
	# ----------------------------
	for x: int in range(grid_width):
		for z: int in range(grid_height):
			var final_elevation: float = FAST_NOISE_LITE.get_noise_2d(x,z) * multiplier

			tiles.append({
				"coord": Vector2(x, z),
				"tile_height": 1,
				"tile_y_offset": 0 #final_elevation
			})

	return tiles

	

func _on_tile_clicked(coord) -> void:
	print("Tile Coord:", coord);
	pass # Replace with function body.
