extends Node

var base_path = "user://"
var data_path = "user://"

func _ready():
	if OS.has_feature("release"):
		base_path=OS.get_executable_path().path_join("data")
	data_path=base_path+"data/"
	if !DirAccess.dir_exists_absolute(data_path):
		DirAccess.make_dir_recursive_absolute(data_path)

func game_exists(game)->bool:
	return DirAccess.dir_exists_absolute(base_path+game)

func game_config_exists(game)->bool:
	if !game_exists(game):
		return false
	
	return DirAccess.dir_exists_absolute(data_path+game+"/config.cfg")

func create_game_folder(game):
	DirAccess.make_dir_recursive_absolute(data_path+game+"/profiles")
	
	var file := ConfigFile.new()
	file.set_value("Data","game_name","Dummy")
	file.set_value("Data","game_icon_path","icon.svg")
	file.save(data_path+game+"/config.cfg")
	
	var script := FileAccess.open(data_path+game+"/game.lua",FileAccess.WRITE)
	script.store_string(
		"""
function get_launch_cmd()
	return "echo Hello gdScript!"
end
"""
	)

func get_game_config(game)->ConfigFile:
	var file := ConfigFile.new()
	if !game_config_exists(game):
		file.set_value("Data","game_name","Dummy")
		file.set_value("Data","game_icon_path","icon.svg")
		file.save(data_path+game+"/config.cfg")
		return file
	file.load(data_path+game+"/config.cfg")
	return file

func game_script_exists(game)->bool:
	if !game_exists(game):
		return false
	
	return DirAccess.dir_exists_absolute(data_path+game+"/game.lua")


func profile_exists(game,profile)->bool:
	if !game_exists(game):
		return false
	return DirAccess.dir_exists_absolute(data_path+game+"/profiles/"+profile)

func profile_config_exists(game,profile)->bool:
	if !game_exists(game):
		return false
	
	return DirAccess.dir_exists_absolute(data_path+game+"/profiles/"+profile+"/config.cfg")

func create_profile_folder(game,profile):
	DirAccess.make_dir_recursive_absolute(data_path+game+"/profiles/"+profile+"/profiles")
	
	var file := ConfigFile.new()
	file.set_value("Data","game_name","Dummy")
	file.set_value("Data","game_icon_path","icon.svg")
	file.save(data_path+game+"/profiles/"+profile+"/config.cfg")

func get_profile_config(game,profile)->ConfigFile:
	var file := ConfigFile.new()
	if !game_config_exists(game):
		file.set_value("Data","profile_name","Dummy")
		file.set_value("Data","profile_icon_path","icon.svg")
		file.save(data_path+game+"/profiles/"+profile+"/config.cfg")
		return file
	file.load(data_path+game+"/profiles/"+profile+"/config.cfg")
	return file
