extends Control

# MemoryCard is the object for save and load states with slots.

var progress_file = OS.get_executable_path().get_base_dir() + "/saves/game_progress.gts"
var config_file = OS.get_executable_path().get_base_dir() + "/saves/game_config.gts"
var hiscore_file = OS.get_executable_path().get_base_dir() + "/saves/game_hiscores.gts"
#var progress_file = "user://game_progress.gts"
#var config_file = "user://game_config.gts"


# --- PROGRESS Save Stats
var progress_stats = {
	"mode": 1,  #0 - EASY, 1 - NORMAL, 2 - HARD
	"score": 0.0,
	"coins": 0,
	"characters": [
		
	],
	"treasures": [
		
	],
	"easteregg": [
		
	]
}

# --- GAME Save Stats

var config_stats = {
	"fullscreen": false,
	"music":  true,
	"sfx":   true
}


var hiscore_stats = {
	
}


# If going to Arcade mode, get from this variable instead.
# Arcade stats always resets itself when the game is finished or that
# mode is finished. Don't need to worry with saves here.
# warning-ignore:unused_class_variable
var arcade_progress_stats = progress_stats


# This variable is for reset purposes. If someone wants to delete a
# slot, the original slot info must be set and then saved. This backup
# contains the basic backup-ed slot info.
var backup_slot = progress_stats
var backup_config = config_stats


# --------- SLOT HANDLING FUNCTIONS ----------

# simply verify if the save file exists
func has_file(file):
	var save_file = File.new()
	var exists = save_file.file_exists(file)
	save_file.close()
	return exists

func read_file(file, safe_content):
	# ... but if no savefile is found, generate one with empty slots.
	if not has_file(file):
		return save_file(file, safe_content)
	
	# read, setup the slots and return the JSON
	var save_file = File.new()
	save_file.open_encrypted_with_pass(file, File.READ, OS.get_unique_id())
	var file_content = parse_json(save_file.get_line())
	save_file.close()
	
	return file_content


func save_file(file, content):
	var save_file = File.new()
	save_file.open_encrypted_with_pass(file, File.WRITE, OS.get_unique_id())
	save_file.store_line(to_json(content))
	save_file.close()
	return content


func set_config(option, value, save = true):
	config_stats[option] = value
	if save: save_file(config_file, config_stats)

func set_progress(option, value, save = true):
	progress_stats[option] = value
	if save: save_file(progress_file, progress_stats)

func get_config(option):
	if not config_stats: reset()
	if config_stats.has(option): return config_stats[option]
	return false

func get_progress(option):
	if not progress_stats: reset()
	if progress_stats.has(option): return progress_stats[option]
	return false

func save():
	save_file(progress_file, progress_stats)
	save_file(config_file, config_stats)
	save_file(hiscore_file, hiscore_stats)

func reset():
	self.progress_stats = self.backup_slot
	self.config_stats = self.backup_config
	save()
	

# ------------ SPECIFIC FUNCTIONS ------------


func register_hiscore(character, value):
	hiscore_stats[character] = value
	save_file(hiscore_file, hiscore_stats)

func get_hiscore(character):
	if hiscore_stats.has(character):
		return hiscore_stats[character]
	hiscore_stats[character] = "0,0"
	save_file(hiscore_file, hiscore_stats)
	return "0,0"

func get_coins():
	return int(self.get_progress("coins"))

func add_coins(coins):
	coins = int(coins)
	self.set_progress("coins", get_coins() + coins)
	save()

func set_coins(coins):
	coins = int(coins)
	self.set_progress("coins", int(coins))
	save()

func unlock_treasure(treasure, price = 0):
	if self.progress_stats['treasures'].has(treasure): return #already unlocked
	self.progress_stats['treasures'].append(treasure)
	price = int(price)
	if price > 0: self.add_coins(0 - price)
	elif price < 0: self.add_coins(price)


func unlock_character(character, price = 0):
	if self.progress_stats['characters'].has(character): return #already unlocked
	self.progress_stats['characters'].append(character)
	
	price = int(price)
	if price > 0: self.add_coins(0 - price)
	if price < 0:self.add_coins(price)
	self.save()
	
func is_treasure_unlocked(treasure):
	return self.progress_stats['treasures'].has(treasure)

func is_character_unlocked(character):
	return self.progress_stats['characters'].has(character)

func is_easteregg_unlocked(easteregg):
	if not self.progress_stats.has("easteregg"):
		 self.progress_stats['easteregg'] = []
	if not self.progress_stats['easteregg'].has(easteregg):
		self.progress_stats['easteregg'].append(easteregg)
		return false
	return true


# ------------- OTHER FUNCTIONS --------------

# Init all five slots in the Story mode
func _ready():
	
	if OS.get_name() == "HTML5":
		progress_file = "user://game_progress.gts"
		config_file = "user://game_config.gts"
		hiscore_file = "user://game_hiscore.gts"

	# some gamers from old versions has older files, let's move it!
	# copy the old save to the new folder, and then delete it
	var dir = Directory.new()
	
	if Utils.has_file("user://game_progress.gts"):
		self.progress_stats = self.read_file("user://game_progress.gts", self.progress_stats)
		dir.remove("user://game_progress.gts")
	else: self.progress_stats = self.read_file(self.progress_file, self.progress_stats)
		
	if Utils.has_file("user://game_config.gts"):
		self.config_stats = self.read_file("user://game_config.gts", self.config_stats)
		dir.remove("user://game_config.gts")
	else: self.config_stats = self.read_file(self.config_file, self.config_stats)
		
	if Utils.has_file("user://game_hiscore.gts"):
		self.hiscore_stats = self.read_file("user://game_hiscore.gts", self.hiscore_stats)
		dir.remove("user://game_hiscore.gts")
	else: self.hiscore_stats = self.read_file(self.hiscore_file, self.hiscore_stats)
	
	self.backup_slot = self.progress_stats
	self.backup_config = self.config_stats
	
	save()
