extends GridMap

@export var ChunkWidth: int = 128
@export var ChunkHeight: int = 128

var world = World.new(1024, 1024)

@onready var camera = $Camera3D

func _ready():
	world.generate_world()
	setGridChunk(0, ChunkWidth, 0, ChunkHeight)
	
func generateGridChunk(cameraPos: Vector3) -> void:
	clear()
	var chunkCenter = local_to_map(cameraPos)
	var rowStart: float = chunkCenter.z - (ChunkHeight / 2)
	var rowEnd: float = chunkCenter.z + (ChunkHeight / 2)
	var columnStart: float = chunkCenter.x - (ChunkWidth / 2)
	var columnEnd: float = chunkCenter.x + (ChunkWidth / 2)
	setGridChunk(columnStart, columnEnd, rowStart, rowEnd)

func setGridChunk(columnStart: float, columnEnd: float, rowStart: float, rowEnd: float) -> void:
	if columnStart < 0 : 
		columnStart = 0
	if rowStart < 0 : 
		rowStart = 0
	if columnEnd > world._data.width - 1 : 
		columnEnd = world._data.width - 2
	if rowEnd > world._data.height - 1 : 
		rowEnd = world._data.height - 2

	for mz in range(rowStart, rowEnd):
		for mx in range(columnStart, columnEnd):
			var my: float = world.find_elevation(Vector2(mx, mz))
			var meshID 
			var mesh_rotation 
			
			if my > 0:
				var neighbours = world.get_neighbours_4_at_same_height(Vector2(mx, mz))
				match neighbours:
					Global.directions_4.RIGHT:
						meshID = Global.bloc_sides_id.SIDE_3
						mesh_rotation = Global.GRID_ROTATION[3]
						
					Global.directions_4.LEFT:
						meshID = Global.bloc_sides_id.SIDE_3
						mesh_rotation = Global.GRID_ROTATION[1]
						
					Global.directions_4.LEFT + Global.directions_4.RIGHT:
						meshID = Global.bloc_sides_id.SIDE_2_OPPOSITE
						mesh_rotation = Global.GRID_ROTATION[0]
						
					Global.directions_4.BOTTOM:
						meshID = Global.bloc_sides_id.SIDE_3
						mesh_rotation = Global.GRID_ROTATION[0]
						
					Global.directions_4.BOTTOM + Global.directions_4.RIGHT:
						meshID = Global.bloc_sides_id.SIDE_2_ANGLE
						mesh_rotation = Global.GRID_ROTATION[3]
						
					Global.directions_4.BOTTOM + Global.directions_4.LEFT:
						meshID = Global.bloc_sides_id.SIDE_2_ANGLE
						mesh_rotation = Global.GRID_ROTATION[0]
						
					Global.directions_4.BOTTOM + Global.directions_4.LEFT + Global.directions_4.RIGHT:
						meshID = Global.bloc_sides_id.SIDE_1
						mesh_rotation = Global.GRID_ROTATION[0]
						
					Global.directions_4.TOP:
						meshID = Global.bloc_sides_id.SIDE_3
						mesh_rotation = Global.GRID_ROTATION[2]
						
					Global.directions_4.TOP + Global.directions_4.RIGHT:
						meshID = Global.bloc_sides_id.SIDE_2_ANGLE
						mesh_rotation = Global.GRID_ROTATION[2]
						
					Global.directions_4.TOP + Global.directions_4.LEFT:
						meshID = Global.bloc_sides_id.SIDE_2_ANGLE
						mesh_rotation = Global.GRID_ROTATION[1]
						
					Global.directions_4.TOP + Global.directions_4.LEFT + Global.directions_4.RIGHT:
						meshID = Global.bloc_sides_id.SIDE_1
						mesh_rotation = Global.GRID_ROTATION[2]
						
					Global.directions_4.TOP + Global.directions_4.BOTTOM:
						meshID = Global.bloc_sides_id.SIDE_2_OPPOSITE
						mesh_rotation = Global.GRID_ROTATION[1]
						
					Global.directions_4.TOP + Global.directions_4.BOTTOM + Global.directions_4.RIGHT:
						meshID = Global.bloc_sides_id.SIDE_1
						mesh_rotation = Global.GRID_ROTATION[3]
						
					Global.directions_4.TOP + Global.directions_4.BOTTOM + Global.directions_4.LEFT:
						meshID = Global.bloc_sides_id.SIDE_1
						mesh_rotation = Global.GRID_ROTATION[1]
						
					Global.directions_4.TOP + Global.directions_4.BOTTOM + Global.directions_4.LEFT + Global.directions_4.RIGHT:
						meshID = Global.bloc_sides_id.SIDE_0
						mesh_rotation = Global.GRID_ROTATION[0]
						
					0:
						meshID = Global.bloc_sides_id.SIDE_4
						mesh_rotation = Global.GRID_ROTATION[0]

				set_cell_item( Vector3(mx, my, mz) , meshID, mesh_rotation)

func _on_MapUpdateTimer_timeout():
	generateGridChunk(camera.global_transform.origin)
