class_name TreeColorDefault
extends RefCounted

var tree_name: String = ""
var current_season: String = "summer"
var mesh_instances: Array[MeshInstance3D] = []

func set_tree_info(name: String, season: String):
	tree_name = name
	current_season = season

func set_leaf_color(model_instance: Node3D):
	#Log.pr("Setting leaf color to: %s" % color)
	pass

func set_trunk_color(model_instance: Node3D):
	#Log.pr("Setting trunk color to: %s" % color)
	pass

# Update a specific material by name with a new color
func update_material_by_name(root_node: Node, material_name: String, new_color: Color) -> bool:
	var updated = false

	for mesh_instance in mesh_instances:
		if update_material_in_mesh_instance(mesh_instance, material_name, new_color):
			updated = true
	
	return updated

# Update multiple materials at once
func update_materials_by_names(root_node: Node, material_updates: Dictionary) -> Dictionary:
	var results = {}

	# Initialize results
	for material_name in material_updates.keys():
		results[material_name] = false
	
	for mesh_instance in mesh_instances:
		for material_name in material_updates.keys():
			var new_color = material_updates[material_name]
			if update_material_in_mesh_instance(mesh_instance, material_name, new_color):
				results[material_name] = true
	
	return results

# Get all material names from a model
func get_all_material_names(root_node: Node) -> Array[String]:
	var material_names: Array[String] = []
	
	for mesh_instance in mesh_instances:
		var mesh = mesh_instance.mesh
		if mesh:
			for i in range(mesh.get_surface_count()):
				var material = mesh.surface_get_material(i)
				if material and material.resource_name:
					if not material.resource_name in material_names:
						material_names.append(material.resource_name)
	
	return material_names

func update_material_by_name_with_material(root_node: Node, material_name: String, new_material: StandardMaterial3D) -> bool:
	var updated = false

	for mesh_instance in mesh_instances:
		var mesh = mesh_instance.mesh
		if mesh:
			for i in range(mesh.get_surface_count()):
				var current_material = mesh.surface_get_material(i)
				if current_material and current_material.resource_name == material_name:
					apply_material_with_queue(mesh_instance, i, new_material)
					#mesh_instance.set_surface_override_material(i, new_material)
					updated = true
					#Log.pr("Updated material '%s' with pre-made material" % material_name)
	
	return updated


# Helper function - find all mesh instances recursively
func find_all_mesh_instances(node: Node) -> Array[MeshInstance3D]:
	if node is MeshInstance3D:
		mesh_instances.append(node)
	
	for child in node.get_children():
		mesh_instances += find_all_mesh_instances(child)
	
	return mesh_instances

# Helper function - update material in a specific mesh instance
func update_material_in_mesh_instance(mesh_instance: MeshInstance3D, material_name: String, new_color: Color) -> bool:
	var updated = false
	var mesh = mesh_instance.mesh
	
	if mesh:
		for i in range(mesh.get_surface_count()):
			var material = mesh.surface_get_material(i)
			if material and material.resource_name == material_name:
				var new_material = material.duplicate()
				new_material.albedo_color = new_color
				mesh_instance.set_surface_override_material(i, new_material)
				#Log.pr("Updated material '%s' to color: %s" % [material_name, new_color])
				updated = true
	
	return updated


func apply_material_with_queue(mesh_instance: MeshInstance3D, index: int, material: StandardMaterial3D):
	MaterialManager.queue_material_application(mesh_instance, index, material)
	return
