extends "res://scripts/Level.gd"

export var version = "PUBLIC BETA"
var actual_scene = "title"

onready var level_select = $Screens/LevelSelect
onready var artworks = $Screens/Artworks
onready var options = $Screens/Options


var easteregg_patreon = {}

# Called when the node enters the scene tree for the first time.
func _ready():

	if Game.effects: Game.effects.fade_out_white(0.8)
	get_node("Camera").target = get_node("PlayerDoll")
	
	level_select.show()
	artworks.show()
	options.show()
	
	Utils.fade_out(level_select, 0.005)
	Utils.fade_out(artworks, 0.005)
	Utils.fade_out(options, 0.005)
	
	level_select.connect("back_title", self, "back_title")
	artworks.connect("back_title", self, "back_title")
	options.connect("back_title", self, "back_title")
	
	var load_art = "res://artworks/opening/intro" + str(Utils.random(7)) + ".jpg"
	if Utils.file_exist(load_art):
		$Artwork.texture = Utils.load_file(load_art)
	
	Audio.play_ost("intro")
	Audio.volume(80)
	input_connect()
	
	$Info.text = self.version + " v" + Game.version
	
	# this is interesting.
	#register_easteregg("up,down,down,left,right,left,down,right", 10000)  # Classic code, avaliable for patrons
	#register_easteregg("down,left,right,right,right,left,down,right,down,up,right", 5000)  # v1.07
	

func register_easteregg(code, coins):
	Controller.register_easteregg(code, self, "spam")
	easteregg_patreon[code] = int(coins)

func execute_easteregg(code):
	if not easteregg_patreon.has(code): return
	if not MemoryCard.is_easteregg_unlocked(code):
		Game.effects.fade_out_white(0.5)
		MemoryCard.add_coins(int(easteregg_patreon[code]))
		MemoryCard.save()
		Audio.play_sfx("unlock_treasure")


func spam(): # eggs
	# lol
	pass



func easteregg_coins(code, coins):
	if not MemoryCard.is_easteregg_unlocked(code):
		MemoryCard.add_coins(int(coins))
		MemoryCard.save()
		Audio.play_sfx("unlock_treasure")



# ----- MENU ITEMS ----

var actual_menu = 0
var max_items = 3
onready var selector_y = $Menu/Selector.get_rect().position.y


func open_menu(new_item):
	update_menu(new_item)
	on_enter()


func change_menu(number_add):
	actual_menu = actual_menu + number_add
	update_menu(actual_menu)


func update_menu(new_item):
	actual_menu = new_item
	if actual_menu < 0: actual_menu = 0
	elif actual_menu > max_items: actual_menu = max_items
	else: Audio.play_sfx("menu_move")
	Utils.fade_position($Menu/Selector, Vector2( $Menu/Selector.get_rect().position.x, selector_y + (50 * actual_menu) ), 0.05)

func back_title():
	level_select.hide()
	artworks.hide()
	options.hide()
	if actual_scene == "levelselect": level_select.show()
	elif actual_scene == "options": options.show()
	else: artworks.show()
	
	actual_scene = "title"
	Utils.fade_out(artworks)
	Utils.fade_out(level_select)
	Utils.fade_out(options)
	Utils.fade_in($Menu)
	input_connect()

# ----------- CONTROLLER FUNCTIONS --------


func input_connect():
	# warning-ignore:return_value_discarded
	Controller.connect("controller_up", self, "on_up")
	# warning-ignore:return_value_discarded
	Controller.connect("controller_down", self, "on_down")
	# warning-ignore:return_value_discarded
	Controller.connect("controller_enter", self, "on_enter")
	# warning-ignore:return_value_discarded
	Controller.connect("controller_escape", self, "on_exit")
	# warning-ignore:return_value_discarded
	Controller.connect("controller_jump", self, "on_enter")
	# warning-ignore:return_value_discarded
	Controller.connect("controller_easteregg", self, "execute_easteregg")
	
func input_disconnect():
	# warning-ignore:return_value_discarded
	Controller.disconnect("controller_up", self, "on_up")
	# warning-ignore:return_value_discarded
	Controller.disconnect("controller_down", self, "on_down")
	# warning-ignore:return_value_discarded
	Controller.disconnect("controller_enter", self, "on_enter")
	# warning-ignore:return_value_discarded
	Controller.disconnect("controller_escape", self, "on_exit")
	# warning-ignore:return_value_discarded
	Controller.disconnect("controller_jump", self, "on_enter")
	# warning-ignore:return_value_discarded
	Controller.disconnect("controller_easteregg", self, "execute_easteregg")

func on_up():
	if actual_scene != "title": return
	change_menu(-1)

func on_down():
	if actual_scene != "title": return
	change_menu(1)

func on_enter():
	if actual_scene != "title": return
	Utils.fade_out($Menu)
	input_disconnect()
	if actual_menu == 0:
		actual_scene = "levelselect"
		level_select.show()
		level_select.camera = $Camera
		level_select.activate()
		Utils.fade_in(level_select)
	elif actual_menu == 1:
		actual_scene = "artworks"
		artworks.show()
		artworks.activate()
		Utils.fade_in(artworks)
	elif actual_menu == 2:
		actual_scene = "options"
		options.show()
		options.activate()
		Utils.fade_in(options)
	else:
		on_exit()


func on_exit():
	if actual_scene != "title": return
	input_disconnect()
	Game.fade_close()
