extends Node

var time_dilated = false 
var paused = false
var win_death = false 
var pressed = false
var hide_mouse = false
var fullscreen = false
var player_pos = Vector2()
var display_resolution = Vector2()
var stats = IntArray()
var viewport
var current_level = null
var main_scene = null
var curent_level_path = ""
var show_missions_list = false

var explosion_effect = preload("res://scenes/effects/explosion.scn")
var blood_splash_effect = preload("res://scenes/effects/blood_spalsh.scn")
var destroy_effect = preload("res://scenes/effects/destroy.scn")

var curent_control_type = 0 setget control_changed, get_curent_control_type
var curent_resolution_index = 3 setget resolution_index_changed, get_curent_resolution_index

var input_map = StringArray([])

onready var time = 0.0
onready var screen_resolutions = Vector2Array([Vector2(320,180), Vector2(480,270), Vector2(640,360), Vector2(960,540), Vector2(1280, 720) ,Vector2(1920, 1080)])
onready var control_types = StringArray(["a", "b"])
#onready var control_types = StringArray(["a", "b", "gamepad"])
onready var root = get_tree().get_root()

func resolution_index_changed(newvalue):
	if curent_resolution_index != newvalue:
		window_resize(newvalue)
	curent_resolution_index = newvalue

func get_curent_control_type():
	return curent_control_type

func control_changed(newvalue):
	curent_control_type = newvalue
	var prefix = control_types[curent_control_type] 
	input_map = StringArray([prefix + "_left", prefix +"_right", prefix +"_jump", prefix +"_run", prefix +"_reload", prefix + "_use"])

func get_curent_resolution_index():
	return curent_resolution_index
	
func load_level_firstly(var path):
	root.get_child(root.get_child_count()-1).queue_free()
	main_scene = load("res://scenes/main.scn").instance()
	root.add_child(main_scene)
	curent_level_path = path
	current_level = load(curent_level_path).instance()
	main_scene.add_child(current_level)
	Globals.get("UI").get_node("anim").play("fade_in")
	reset_stats()
	set_fixed_process(true)
	
func restart_level():
	Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
	load_level_firstly(curent_level_path)
	get_tree().set_pause(false)
	win_death = false
	paused = false
	reset_stats()
	set_fixed_process(true)
	
func load_new_level(var path):
	current_level.queue_free()
	current_level = load(path).instance()
	root.add_child(current_level)
	Globals.get("UI").get_node("anim").play("fade_in")
	reset_stats()
	set_fixed_process(true)
	
func finish_level_event():
	Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
	get_tree().set_pause(true)
	Globals.get("UI").get_node("win_screen").show()
	win_death = true
	
func game_over_event():
	Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
	get_tree().set_pause(true)
	Globals.get("UI").get_node("death_screen").show()
	win_death = true

func exit_to_menu():
	var fader = Globals.get("UI").get_node("anim")
	fader.play("fade_out")
	yield(fader, "finished")
	root.get_child(root.get_child_count()-1).queue_free()
	get_tree().change_scene("res://scenes/main_menu.scn")
	get_tree().set_pause(false)

func _process(delta):
	if time_dilated:
		stats[2]-=0.1
	elif stats[2] < 1000:
		stats[2]+=1
	var pause = Input.is_action_pressed("ui_cancel")
	if pause and !pressed:
		pressed = true
		if !paused:
			Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
			pause_setting(true)
		elif paused:
			Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
			pause_setting(false)
	elif !pause:
		pressed = false

func pause_setting(var state):
	set_fixed_process(!state)
	get_tree().set_pause(state)
	Globals.get("UI").show_hide_pause_menu(state)
	paused = state
	
func _fixed_process(delta):
	time += delta
	
func time_dilation(var scale):
	time_dilated = !time_dilated
	OS.set_time_scale(scale)
	audio.set_global_pitch_scale(scale)
	
func apply_powerup(var amount, var type):
	stats[type] = clamp(stats[type] + amount, 0, 1000) 
	
func change_control_type(index_changer):
	var new_control_type = curent_control_type
	new_control_type += index_changer
	new_control_type = fposmod(new_control_type, control_types.size())
	global.curent_control_type = new_control_type
	return(str(global.control_types[curent_control_type]))
	
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:
		global.curent_resolution_index = new_resolution_index
		return(str(screen_resolutions[curent_resolution_index]))
	else:
		return ""
	
func window_resize(var new_resolution_index):
	if screen_resolutions[new_resolution_index] == display_resolution:
		OS.set_window_fullscreen(true)
		fullscreen = true
	else:
		if fullscreen:
			OS.set_window_fullscreen(false)
			fullscreen = false
		OS.set_window_size(screen_resolutions[new_resolution_index])
		window_center(new_resolution_index)
		
func window_center(var resolution_index):
	OS.set_window_position(Vector2((display_resolution.x - screen_resolutions[resolution_index].x)/2, (display_resolution.y - screen_resolutions[resolution_index].y)/2))

func reset_stats():
	stats = [1000,1000,1000,1000]
	
			
func set_input_map():
	var prefix = control_types[curent_control_type]
	input_map = StringArray([prefix + "_left", prefix +"_right", prefix +"_jump", prefix +"_run", prefix +"_reload", prefix + "_use"])	
	
func show_effect(var pos, var type, var mode):
	var effect
	if type == 1:
		effect = explosion_effect.instance()
	elif type == 2:
		effect = blood_splash_effect.instance()
	elif type == 3:
		effect = destroy_effect.instance()
	if mode == 2:
		effect.mode = 2
	effect.set_pos(pos)
	add_child(effect)
	
func _ready():
	if hide_mouse:
		Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)	
	display_resolution = OS.get_screen_size(0)
	if screen_resolutions[screen_resolutions.size() - 1] != display_resolution:
		screen_resolutions.set(screen_resolutions.size() - 1, display_resolution)
	window_center(curent_resolution_index)
	set_pause_mode(2)
	set_input_map()
	reset_stats()
	set_process(true)
	set_fixed_process(true)
	
func _init():
	pass
	
	






