extends Node

func initialize():
	var database_manager = load(gddb_constants.c_addon_main_path + "core/db_man.gd").new()
	var db_id =  database_manager.load_database(save_database_in())
	
	check_invalid_type(db_id)
	check_invalid_version(db_id)
	
	return database_manager.get_db_by_id(db_id)

func check_invalid_type(db_id):
	if db_id == gddb_types.e_db_invalid_file:
		print("[Database#check_invalid_type] Error invalid database file !")
		OS.set_exit_code(1)

func check_invalid_version(db_id):
	if db_id == gddb_types.e_db_invalid_ver:
		print("[Database#check_invalid_version] Error invalid database version !")
		OS.set_exit_code(1)

func save_database_in():
	if OS.get_name() == "Android":
		copy_database()
		return android_path()
	else:
		return normal_path()

func android_path():
	return "user://database.json"

func normal_path():
	return "res://db/ahog.json"

func copy_database():
	var database_copy = File.new()
	
	if !database_copy.file_exists(android_path()):
		var database_file = File.new()
		
		database_file.open(normal_path(), File.READ)
		database_copy.open(android_path(), File.WRITE)
		database_copy.store_string(database_file.get_as_text())
		database_copy.close()
		
		database_file.close()

