extends Node
class_name ModifierLibrary

var modifiers: Array[Modifier] = []

func _ready() -> void:
	## Check the modifier folder for any scripts that extend the Modifier class
	var modifier_folder = "res://combat/modifiers/modifiers/"

	var dir = DirAccess.open(modifier_folder)

	if dir:
		dir.list_dir_begin()
		var file_name = dir.get_next()
		while file_name != "":
			if file_name.ends_with(".gd"):
				var script_path = modifier_folder + file_name
				var script = load(script_path)
				
				var temp_instance = script.new() if script else null
				var is_modifier = temp_instance is Modifier if temp_instance else false

				if is_modifier and file_name != "modifier.gd":
					modifiers.append(temp_instance)

			file_name = dir.get_next()
		dir.list_dir_end()
	else:
		print("Failed to open directory: ", modifier_folder)

	Log.pr("ModifierLibrary initialized with %d modifiers" % modifiers.size())
	Log.pr("Modifiers: ", modifiers)

func get_modifier_by_id(modifier_id: String) -> Modifier:
	for modifier in modifiers:
		if modifier.id == modifier_id:
			return modifier.duplicate()
	return null

func get_modifiers_by_type(modifier_type: int) -> Array[Modifier]:
	var result: Array[Modifier] = []
	for modifier in modifiers:
		if modifier.modifier_type == modifier_type:
			result.append(modifier)
	return result

func get_modifiers_by_rarity(rarity: int) -> Array[Modifier]:
	var result: Array[Modifier] = []
	for modifier in modifiers:
		if modifier.rarity == rarity:
			result.append(modifier)
	return result

func get_modifiers_by_priority(priority: int) -> Array[Modifier]:
	var result: Array[Modifier] = []
	for modifier in modifiers:
		if modifier.priority == priority:
			result.append(modifier)
	return result

func get_all_modifiers() -> Array[Modifier]:
	return modifiers
