extends Node

const GAME_NAME = "tiny journey"
const VERSION = "1.0"

var score = 0

var gui = null
var player = null
var game = null
var level = null

var default_window_size = Vector2(960, 540)
var screen_size
var fullscreen
var curent_resolution_index = 3 setget resolution_index_changed, get_curent_resolution_index
var current_zone_id = 1
var current_level_id = 1
var total_score = 0
var current_score = 0
var current_locale = 1
var current_user = "player"
var pause_state = false
var final = false

onready var screen_resolutions = Vector2Array([Vector2(320,180), Vector2(480,270), Vector2(640,360), Vector2(960,540), Vector2(1280, 720) ,Vector2(1920, 1080)])

func start_new_game():
	final = false
	current_zone_id = 1
	current_level_id = 1
	total_score = 0
	save_data()
	get_tree().change_scene_to(load("res://scenes/game.scn"))
	
func continue_game():
	load_data()
	get_tree().change_scene_to(load("res://scenes/game.scn"))

func restart():
	gui.ui_fade_in()
	game.reload_current_level()
	current_score = 0
	gui.ui_update_score(total_score)
	yield(game, "complete")
	gui.ui_fade_out()
	
func next_level(l, z):
	gui.ui_fade_in()
	game.load_next_level()
	current_level_id = l
	current_zone_id = z
	total_score += current_score
	current_score = 0
	gui.ui_update_progress()
	yield(game, "complete")
	gui.ui_fade_out()
	save_data()
	
func toogle_scream():
	player.enable_scream = !player.enable_scream
	
func toogle_fullscreen():
	self.fullscreen = !self.fullscreen
	OS.set_window_fullscreen(self.fullscreen)

func toogle_pause():
	pause_state =!pause_state
	get_tree().set_pause(pause_state)
	gui.ui_pause_menu(pause_state)

func _on_time_is_over():
	pass
		
func update_score(a):
	current_score += a
	gui.ui_update_score(total_score + current_score)

func resolution_index_changed(newvalue):
	if curent_resolution_index != newvalue:
		window_resize(newvalue)
	curent_resolution_index = newvalue
	
func get_curent_resolution_index():
	return curent_resolution_index
	
func change_resolution(index_changer):
	var new_resolution_index = curent_resolution_index
	new_resolution_index += index_changer
	new_resolution_index = clamp(new_resolution_index, 0, screen_resolutions.size()-1)
	if new_resolution_index != curent_resolution_index:
		curent_resolution_index = new_resolution_index
		return(str(screen_resolutions[curent_resolution_index]))
	else:
		return ""
		
func cmd_handler(cmd):
	pass
		
func window_resize(var new_size):
	OS.set_window_size(screen_resolutions[new_size])
		
func center_a_window(var resolution_index):
	OS.set_window_position(Vector2((screen_size.x - screen_resolutions[resolution_index].x)/2, (screen_size.y - screen_resolutions[resolution_index].y)/2))

func save_data():
	var key_1 = "zone"
	var key_2 = "level"
	var key_3 = "score"
	var line = "" 
	var file = File.new()
	var err = file.open("user://" + current_user + ".sav",File.READ)
	
	if !err:
		line = file.get_line()
	elif err:
		create_saving_data_file()
		
	var err = file.open("user://" + current_user + ".sav",File.READ)
	if !err:
		line = file.get_line()
	elif err:
		OS.alert("Невозможно создать файл сохранений.", "Ошибка!" )
		return
		
	file.close()
	var dic = {}
	if line!="":
		dic.parse_json(line)
	dic[key_1] = current_zone_id
	dic[key_2] = current_level_id
	dic[key_3] = total_score
	line = dic.to_json()
	var err = file.open("user://" + current_user + ".sav", File.WRITE)
	if(!err):
		file.store_line(line)
	file.close()

func create_saving_data_file():
	var key_1 = "zone"
	var key_2 = "level"
	var key_3 = "score"
	var line = "" 
	var file = File.new()
	file.close()
	file.open("user://" + current_user + ".sav", File.WRITE)
	file.store_line(line)
	file.close()
	var dic = {}
	if line!="":
		dic.parse_json(line)
	dic[key_1] = current_zone_id
	dic[key_2] = current_level_id
	dic[key_3] = total_score
	line = dic.to_json()
	var err = file.open("user://" + current_user + ".sav", File.WRITE)
	if(!err):
		file.store_line(line)
	file.close()

func load_data():
	var key_1 = "zone"
	var key_2 = "level"
	var key_3 = "score"
	var file = File.new()
	var err = file.open("user://" + current_user + ".sav", File.READ)
	if (!err):
		var line = file.get_line()
		file.close()
		var dic = {}
		dic.parse_json(line)
		print(dic.keys())
		if dic.has(key_1):
			current_zone_id = dic[key_1]
		if dic.has(key_2):
			current_level_id = dic[key_2]
		if dic.has(key_3):
			total_score = dic[key_3]
	elif(err):
		create_saving_data_file()

func _ready():
	OS.set_window_title(GAME_NAME + " " +VERSION)
	load_data()
	
	