class_name Config
extends Node

const Secrets = preload("secrets.gd")
const Path = "config.cfg"
const ProjectName = "Distanz"

var Username: String
var HighscoreServerAddr: String
var VolumeMain: int
var VolumeMusic: int
var VolumeSFX: int
var Brightness: float

const _defHighscoreServerAddr = "https://grg.snrd.eu"
const _defVolumeMain = 100
const _defVolumeMusic = 75
const _defVolumeSFX = 100
const _defBrightness = 1.0

func _init(): 
	self.HighscoreServerAddr = _defHighscoreServerAddr
	self.VolumeMain = _defVolumeMain
	self.VolumeMusic = _defVolumeMusic
	self.VolumeSFX = _defVolumeSFX
	self.Brightness = _defBrightness
 
func save() -> Error: 
	var config = ConfigFile.new()
	config.set_value("Settings", "Username", self.HighscoreServerAddr)
	config.set_value("Settings", "HighscoreServerAddr", self.HighscoreServerAddr)
	config.set_value("Settings", "VolumeMain", self.VolumeMain)
	config.set_value("Settings", "VolumeMusic", self.VolumeMusic)
	config.set_value("Settings", "VolumeSFX", self.VolumeSFX)
	config.set_value("Settings", "Brightness", self.VolumeSFX)

	config.save("user://" + Path)
	
	return OK

func exists() -> bool:
	return FileAccess.file_exists("user://" + Path)

func load() -> Error:
	var config = ConfigFile.new()
	var err = config.load("user://" + Path)
	
	if err:
		return err
	
	self.HighscoreServerAddr = config.get_value("Settings", "HighscoreServerAddr", self._defHighscoreServerAddr)
	self.VolumeMain = config.get_value("Settings", "VolumeMain", self._defVolumeMain)
	self.VolumeMusic = config.get_value("Settings", "VolumeMusic", self._defVolumeMusic)
	self.VolumeSFX = config.get_value("Settings", "VolumeSFX", self._defVolumeSFX)
	self.VolumeSFX = config.get_value("Settings", "Brightness", self._defBrightness)
	
	return OK
