class_name CellDataResource
extends Resource

@export var cell_seed: int = randi()
@export var x: int = 0
@export var z: int = 0

# Core cell type properties - these will trigger automatic updates
@export var camp: bool = false: set = set_camp
@export var path: bool = false: set = set_trail
@export var water: bool = false: set = set_water

# Dependent properties that get automatically managed
@export var vegetation_density: float = 0.5: set = set_vegetation_density
@export var ground_compaction: float = 0.0: set = set_ground_compaction
@export var moisture_level: float = 0.6
@export var trees: Array = []

# Internal flag to prevent infinite recursion during initialization
var _initializing: bool = false

func _init():
	_initializing = true
	# Set default values
	vegetation_density = 0.5
	ground_compaction = 0.0
	camp = false
	path = false
	water = false
	_initializing = false

func set_camp(value: bool):
	camp = value
	if not _initializing:
		_update_dependent_properties()

## Using a different name due to Resource using the function set_path in Godot
func set_trail(value: bool):
	path = value
	if not _initializing:
		_update_dependent_properties()

func set_water(value: bool):
	water = value
	if not _initializing:
		_update_dependent_properties()

func set_vegetation_density(value: float):
	# Only allow manual setting if not a special cell type
	if _initializing or not (camp or path or water):
		vegetation_density = value
	else:
		vegetation_density = 0.0

func set_ground_compaction(value: float):
	# Camp always has maximum compaction, others can be set manually
	if camp:
		ground_compaction = 1.0
	elif not _initializing:
		ground_compaction = value

func _update_dependent_properties():
	# Update vegetation density - always 0 for camp, path, or water
	if camp or path or water:
		vegetation_density = 0.0
	
	# Update ground compaction - camp always has maximum compaction
	if camp:
		ground_compaction = 1.0

# Utility function to set cell type and ensure only one is active
func set_cell_type(cell_type: String):
	_initializing = true
	
	# Clear all cell types first
	camp = false
	path = false
	water = false
	
	# Set the specified type
	match cell_type.to_lower():
		"camp":
			camp = true
		"path":
			path = true
		"water":
			water = true
		"terrain", "normal", "":
			pass # Leave all false for normal terrain
		_:
			push_warning("Unknown cell type: " + cell_type)
	
	_initializing = false
	_update_dependent_properties()

# Utility function to get the primary cell type
func get_cell_type() -> String:
	if camp:
		return "camp"
	elif path:
		return "path"
	elif water:
		return "water"
	else:
		return "terrain"

# Check if this is a special cell type (not normal terrain)
func is_special_cell() -> bool:
	return camp or path or water

func add_trees(tree: TreeDataResource, qty: int) -> void:
	for i in qty:
		trees.append(tree)

# Override vegetation density and ground compaction for special cases
func force_set_vegetation_density(value: float):
	# Allows bypassing the automatic management if absolutely needed
	vegetation_density = value

func force_set_ground_compaction(value: float):
	# Allows bypassing the automatic management if absolutely needed
	ground_compaction = value

func should_spawn_trees() -> bool:
	# Only spawn trees if this is not a special cell type
	return not (camp or path or water) and vegetation_density > 0.0

func should_spawn_grass() -> bool:
	# Grass can spawn in any terrain cell, but not in special cells
	return not (camp or path or water) and vegetation_density > 0.0

func should_spawn_bushes() -> bool:
	# Bushes can spawn in any terrain cell, but not in special cells
	return not (camp or path or water) and vegetation_density > 0.1

func should_spawn_flowers() -> bool:
	# Flowers can spawn in any terrain cell, but not in special cells
	return not (camp or path or water) and vegetation_density > 0.1