extends Node2D

signal back_title

onready var roulette = $CharacterList/Roulette
onready var characters = DLC.get_content_list()
var counter = 0
var actual = 0
var white_shader = false

var camera = null
var activated = false

var on_move = false


func reset_screen():
	pass

func _ready():
	$Coins.text = str(MemoryCard.get_coins()) + " Coins"
	$NoCharacter.hide()
	
	var posX = 613
	var posY = 248
	var character_filename = ""
	var character_slot = null
	
	for dlc in characters:
		character_filename = "res://@content/"+dlc+"/character/Character.tscn"
		if Utils.has_file(character_filename):
			character_slot = null
			character_slot = Utils.load_file(character_filename).instance()
			
			character_slot.character = DLC.get_content_character(dlc)
			character_slot.price = DLC.get_content_character_price(dlc)
			character_slot.difficult = DLC.get_content_difficult(dlc)
			character_slot.author = DLC.get_content_author(dlc)
			character_slot.dlc = dlc
			
			character_slot.get_node("character").texture = Utils.load_file("res://@content/"+dlc+"/character/girl.png")
			character_slot.get_node("level").texture = Utils.load_file("res://@content/"+dlc+"/character/level.jpg")
			
			if counter < 10: character_slot.name = "character0" + str(counter)
			else: character_slot.name = "character" + str(counter)
			
			counter = counter + 1
			
			if not MemoryCard.is_character_unlocked(dlc):
				Utils.interpolate(character_slot, "modulate", Color(1.0, 1.0, 1.0, 1.0), Color(0.3, 0.3, 0.3, 0.9), 0.1)
			
			character_slot.position = Vector2(posX, posY)
			posX = posX + 373
			
			roulette.add_child(character_slot)
	
	if characters:
		update_info()
	else:
		$NoCharacter.show()
		$CharacterList.hide()
	

func get_actual_item():
	return get_item(actual)


func get_item(number):
	var node_name = "character"
	if number < 10: node_name = node_name + "0" + str(number)
	else: node_name = node_name + str(number)
	return get_node("CharacterList/Roulette/" + node_name)


func update_info():
	if not characters: return
	var item = self.get_actual_item()
	$CharacterList/CharacterInfo.text = "Character: " + item.character + "\nDifficult: " + item.difficult + "\nCharacter from " + item.author
	if item.price == 0 or MemoryCard.is_character_unlocked(item.dlc):
		$CharacterList/CharacterPrice.text = "UNLOCKED"
		if not MemoryCard.is_character_unlocked(item.dlc):
			MemoryCard.unlock_character(item.dlc)
	else:
		$CharacterList/CharacterPrice.text = "Price: " + str(item.price) + " Coins"
	
	
	$CharacterList/Hiscore.text = "HI-SCORE:  " + Utils.seconds_to_timer( int( MemoryCard.get_hiscore(item.dlc).split(',')[0] ) )
	
	var coins_score = int(MemoryCard.get_hiscore(item.dlc).split(',')[1])
	if coins_score < 10: coins_score = "000" + str(coins_score)
	elif coins_score < 100: coins_score = "00" + str(coins_score)
	elif coins_score < 1000: coins_score = "0" + str(coins_score)
	$CharacterList/HiscoreCoin.text = coins_score
	
	if MemoryCard.is_character_unlocked(item.dlc):
		Utils.interpolate(item, "modulate", Color(0.3, 0.3, 0.3, 0.9), Color(1.0, 1.0, 1.0, 1.0), 0.1)


func activate():
	activated = true
	
	$CharacterList/Roulette.position.x = 0
	
	DLC.load_dlc()
	characters = DLC.get_content_list()
	
	$NoCharacter.hide()
	$CharacterList.show()
	
	counter = 0
	actual = 0
	white_shader = false
	
	for character in roulette.get_children():
		roulette.remove_child(character)
		character.queue_free()
	
	_ready()
	
	input_connect()

func exit():
	#Controller.touchpad.show()
	activated = false
	input_disconnect()
	emit_signal("back_title")
	
# -------- CONTROLLER FUNCTIONS -----
	
func input_connect():
	# warning-ignore:return_value_discarded
	Controller.connect("controller_right", self, "on_right")
	# warning-ignore:return_value_discarded
	Controller.connect("controller_left", self, "on_left")
	# warning-ignore:return_value_discarded
	Controller.connect("controller_enter", self, "on_enter")
	# warning-ignore:return_value_discarded
	Controller.connect("controller_jump", self, "on_enter")
	# warning-ignore:return_value_discarded
	Controller.connect("controller_escape", self, "exit")

func input_disconnect():
	# warning-ignore:return_value_discarded
	Controller.disconnect("controller_right", self, "on_right")
	# warning-ignore:return_value_discarded
	Controller.disconnect("controller_left", self, "on_left")
	# warning-ignore:return_value_discarded
	Controller.disconnect("controller_enter", self, "on_enter")
	# warning-ignore:return_value_discarded
	Controller.disconnect("controller_jump", self, "on_enter")
	# warning-ignore:return_value_discarded
	Controller.disconnect("controller_escape", self, "exit")


func on_right():
	if on_move: return
	if actual == counter - 1: return
	actual = actual + 1
	Utils.fade_position(roulette, Vector2( roulette.position.x - 373, roulette.position.y ), 0.1)
	Utils.execute_after(self, "_moved_character", 0.11)
	Audio.play_sfx("menu_move")
	update_info()
	on_move = true

func on_left():
	if on_move: return
	if actual == 0: return
	actual = actual - 1
	Utils.fade_position(roulette, Vector2( roulette.position.x + 373, roulette.position.y ), 0.1)
	Utils.execute_after(self, "_moved_character", 0.11)
	Audio.play_sfx("menu_move")
	update_info()
	on_move = true

func _moved_character():
	on_move = false

func on_enter():
	var item = get_actual_item()
	
	if MemoryCard.is_character_unlocked(item.dlc):
		start_game()
		return
	
	if item.price < MemoryCard.get_coins():
		MemoryCard.add_coins(0 - item.price)
		MemoryCard.unlock_character(item.dlc)
		Utils.interpolate(item, "modulate", Color(0.3, 0.3, 0.3, 0.9), Color(1.0, 1.0, 1.0, 1.0), 0.2)
		$Coins.text = str(MemoryCard.get_coins()) + " Coins"
		update_info()
		Audio.play_sfx("unlock_treasure")
	else:
		Audio.play_sfx("not_enough_coins")

func start_game():
	input_disconnect()
	Game.character = get_actual_item().dlc
	var hide_all = 0
	while hide_all < counter:
		if hide_all != actual:
			Utils.fade_out(get_item(hide_all), 0.1)
		hide_all = hide_all + 1
	Utils.execute_after(self, "zoom_character", 0.15)
	Audio.fade_out(1.0)

func zoom_character():
	$CharacterList/Hiscore.hide()
	$CharacterList/Timer.hide()
	$CharacterList/HiscoreCoin.hide()
	$CharacterList.rect_clip_content = false
	var character = get_actual_item().get_node("character")
	white_shader = true
		
	Utils.interpolate(character, "scale", Vector2(0.5,0.5), Vector2(5,5), 0.7)
	Utils.interpolate(character, "position", character.position, Vector2(character.position.x - 600, character.position.y - 1400), 0.7)
	Utils.interpolate(camera, "fixed_trauma", 0, 0.5, 0.8)
	Game.effects.fade_in_white(1.0)
	Utils.execute_after(Game, "next_level", 1.1)



# warning-ignore:unused_argument
func _physics_process(delta):
	$Coins.text = str(MemoryCard.get_coins()) + " Coins"
	if white_shader: get_actual_item().get_node("character").get_material().set_shader_param("bright_amount", get_actual_item().get_node("character").get_material().get_shader_param("bright_amount") + 0.01)

