class_name Globals
extends Node

var map_height: int = 200
var map_width: int = 200
var _map_data: Array
var _biome_data: Dictionary = {}
var _camp_data: Dictionary = {}
var spawn_point: Vector3 = Vector3(0, 0, 0)

# Property for map_data with logging
var map_data: Array:
	get:
		return _map_data
	set(value):
		_map_data = value
		Log.pr("Map data updated")

# Property for biome_data with logging
var biome_data: Dictionary:
	get:
		return _biome_data
	set(value):
		_biome_data = value
		_log_biome_update()

func _init() -> void:
	_map_data.resize(map_height)
	for y in range(map_height):
		_map_data[y] = []
		_map_data[y].resize(map_width)
	Log.pr("Globals initialized with map size: %d x %d" % [map_width, map_height])

# Helper methods for updating specific parts of the data
func set_map_cell(x: int, y: int, value) -> void:
	if x >= 0 and x < map_width and y >= 0 and y < map_height:
		_map_data[y][x] = value
		Log.pr("Map cell updated at (%d, %d)" % [x, y])

func update_biome(key: String, value) -> void:
	_biome_data[key] = value
	Log.pr("Biome data updated for key: %s" % key)

# Internal logging function for biome updates
func _log_biome_update() -> void:
	var details = []
	for key in _biome_data.keys():
		var value = _biome_data[key]
		if value is Image:
			var img = value as Image
			details.append("%s: Image[%dx%d]" % [key, img.get_width(), img.get_height()])
		else:
			details.append("%s: %s" % [key, str(value)])
	Log.pr("Biome data updated: { %s }" % ", ".join(details))
	display_all_noise_maps()

# Display visual representation of noise map
func display_noise_map(key: String, scale: int = 4) -> void:
	if not _biome_data.has(key):
		Log.pr("No biome data found for key: %s" % key)
		return
	
	var img = _biome_data[key]
	if not img is Image:
		Log.pr("Data for key '%s' is not an image" % key)
		return
	
	var image = img as Image
	var width = image.get_width()
	var height = image.get_height()
	
	# Sample the image at lower resolution for console display
	var display_width = min(width / scale, 50) # Max 50 chars wide
	var display_height = min(height / scale, 25) # Max 25 lines tall
	
	Log.pr("=== %s Noise Map (%dx%d) ===" % [key.capitalize(), width, height])
	
	for y in range(display_height):
		var line = ""
		for x in range(display_width):
			# Sample from the original image
			var sample_x = int(x * scale)
			var sample_y = int(y * scale)
			
			# Get pixel color (assuming grayscale or we'll use red channel)
			var pixel = image.get_pixel(sample_x, sample_y)
			var intensity = pixel.r # Use red channel as intensity
			
			# Convert intensity to colored circle
			line += _get_colored_circle(intensity)
		
		Log.pr(line)
	
	Log.pr("Scale: 1 character = %dx%d pixels" % [scale, scale])

# Convert intensity value to colored circle character
func _get_colored_circle(intensity: float) -> String:
	# ANSI color codes for red > yellow > green gradient
	var color_code: String
	var circle = "●"
	
	if intensity < 0.1:
		color_code = "[color=maroon]" # Dark red
	elif intensity < 0.2:
		color_code = "[color=red]" # Red
	elif intensity < 0.3:
		color_code = "[color=#FF4500]" # Orange red
	elif intensity < 0.4:
		color_code = "[color=orange]" # Orange
	elif intensity < 0.5:
		color_code = "[color=#FFD700]" # Gold
	elif intensity < 0.6:
		color_code = "[color=yellow]" # Yellow
	elif intensity < 0.7:
		color_code = "[color=#ADFF2F]" # Green yellow
	elif intensity < 0.8:
		color_code = "[color=lime]" # Lime
	elif intensity < 0.9:
		color_code = "[color=green]" # Green
	else:
		color_code = "[color=darkgreen]" # Dark green
	
	return color_code + circle + "[/color]"

# Display all biome maps
func display_all_noise_maps(scale: int = 4) -> void:
	for key in _biome_data.keys():
		if _biome_data[key] is Image:
			display_noise_map(key, scale)
			Log.pr("") # Empty line between maps

# Helper function to get detailed biome info
func log_biome_details() -> void:
	Log.pr("=== Biome Data Details ===")
	for key in _biome_data.keys():
		var value = _biome_data[key]
		if value is Image:
			var img = value as Image
			Log.pr("%s: Image [%dx%d, Format: %s]" % [key, img.get_width(), img.get_height(), _get_format_name(img.get_format())])
		else:
			Log.pr("%s: %s" % [key, str(value)])

# Helper to convert image format enum to readable string
func _get_format_name(format: Image.Format) -> String:
	match format:
		Image.FORMAT_L8: return "L8"
		Image.FORMAT_LA8: return "LA8"
		Image.FORMAT_R8: return "R8"
		Image.FORMAT_RG8: return "RG8"
		Image.FORMAT_RGB8: return "RGB8"
		Image.FORMAT_RGBA8: return "RGBA8"
		Image.FORMAT_RF: return "RF"
		Image.FORMAT_RGF: return "RGF"
		Image.FORMAT_RGBF: return "RGBF"
		Image.FORMAT_RGBAF: return "RGBAF"
		_: return "Unknown (%d)" % format