extends Node

const PATH: String = "user://.config"

var tracks: Array[Track] = [
	Track.new(2000, 40),    # Track 1 - 0.2km, 40sec
	Track.new(3000, 50),    # Track 2 - 0.3km, 50sec
	Track.new(5000, 80),    # Track 3 - 0.5km, 80sec
	Track.new(7000, 100),   # Track 4 - 0.7km, 100sec
	Track.new(10000, 125),  # Track 5 - 1.0km, 125sec
	Track.new(12000, 140),  # Track 6 - 1.2km, 140sec
	Track.new(15000, 160),  # Track 7 - 1.5km, 160sec
	Track.new(18000, 175),  # Track 8 - 1.8km, 175sec
	Track.new(20000, 190),  # Track 9 - 2.0km, 190sec
							# 		    9.2km, 17min 40sec
]
var current_track: int = 1

var current_player_specs: PlayerSpecs = null

var player_lives: int = 3:
	set(value):
		player_lives = mini(value, 9)

var high_score: int = 0
var total_time: float = 0.0:
	set(value):
		total_time = minf(value, 5999.9)
var total_score: int = 0:
	set(value):
		total_score = mini(value, 99999)

var raw_score: float = 0.0:
	set(value):
		raw_score = value
		total_score = int(roundf(value))

var _config: ConfigFile = ConfigFile.new()


func _ready() -> void:
	var err: Error = _config.load(PATH)
	if not err:
		load_high_score()
	elif err == ERR_FILE_NOT_FOUND:
		save_high_score()


func get_current_track() -> Track:
	return tracks[current_track - 1]


func get_time_string(time: float) -> String:
	return "%d:%04.1f" % [int(floorf(time / 60.0)), int(time * 10) % 600 / 10.0]


func load_high_score() -> void:
	high_score = _config.get_value("save", "high_score", 0)


func save_high_score() -> void:
	_config.set_value("save", "high_score", high_score)
	_config.save(PATH)


class Track:
	var distance: int = 0
	var time: float = 0

	func _init(level_distance: int, time_goal: float):
		distance = level_distance
		time = time_goal
