extends RefCounted
class_name ModManifest
## Object for easier json manifest operations.

#region declaration
var id: StringName ## Mod unique id. Internal usage only.
var name: String ## Displayed mod name
var version: String ## Displayed mod version
var desc: String ## Displayed mod description
var author:String ## Mod Author
var dependencies := [] ## Mod dependecies, optional.
var tags:= [] ## ["units", "vehicles", "buildings", "textures", "maps", "quests"]
var packs := [] ## Optional. [br] By default [param mod_id.pck] is loaded.

var _validated = false ## true if the manifest as a correct syntax
var path:StringName ## set if the mode is loaded
#endregion

# === Init ===        # Initialization logic, if needed


func _init(_id:String,_name:=_id,_desc:="",_dependencies:=[],_packs:=[],_author := "",_version:="1.0",_tags:=[]) -> void:
	if _id == "":
		push_warning("Mod id cannot be null, fallback to "+_name)
		self.id = StringName(_name.strip_edges())
	else:
		self.id = StringName(_id)
	self.name= _name
	self.version = _version
	self.desc=_desc
	self.author = _author
	self.dependencies = _dependencies
	self.tags= _tags
	self.packs =_packs


# === PUBLIC FUNCTIONS ===
func is_valid() -> bool:
	return _validated


## Return [param null] if the json is not parsed correctly
static func new_from_file(_manifest_file:FileAccess) -> ModManifest:
	var man_dic:Dictionary = JSON.parse_string(_manifest_file.get_as_text())
	if !man_dic:
		push_warning("Invalid manifest format: %s" % _manifest_file.get_path())
		return null
	var _id: String

	_id = man_dic.get("id",_manifest_file.get_path().get_file().get_basename().strip_edges()) # ugly. But should fallback on a generated one.
	return ModManifest.new(
		_id,
		man_dic.get("name", _id),
		man_dic.get("desc", ""),
		man_dic.get("dependencies", []),
		man_dic.get("packs", []),
		man_dic.get("author", ""),
		str(man_dic.get("version", "1.0")),
		man_dic.get("tags", [])
	)


## Check if the array is empty, if not, return the packs and the main one if it's not on the list.
func get_mod_packs() -> Array[String]:
	if packs.size() > 0:
		# check if the main pack is in the list
		if packs.has(self.id):
			return self.packs
		else:
			var pck = self.packs
			pck.append(id)
			return pck
	return [id]

# === PRIVATE FUNCTIONS ===        # use _underscore() to make the difference between private and public functions

# ====================
