extends Node2D

signal back_title

onready var treasure_prefab = preload("res://resources/prefabs/TreasureItem.tscn")
onready var treasure_area = $TreasureArea/TreasureList
onready var treasure_selection = $TreasureArea/TreasureList/Selection

var treasure_list = []
var treasure_total = 0
var initial_selection_pos = 0
var initial_option_pos = 0
var treasure_item = 1

var actual_pose = 0
var image_show = false
var panel_active = false

func activate():
	
	DLC.load_dlc()
	
	treasure_list = []
	treasure_total = 0
	initial_selection_pos = 0
	initial_option_pos = 0
	treasure_item = 1
	actual_pose = 0
	
	for n in treasure_area.get_tree().get_nodes_in_group("ItemSelect"):
		treasure_area.remove_child(n)
		n.queue_free()
	
	self.load_artworks()
	
	panel_active = true
	input_connect()
	treasure_area.position = Vector2(0, 0)
	change_treasure(0)

func exit():
	panel_active = false
	if image_show:
		image_show = false
		Utils.fade_out($TreasureView)
		return
	input_disconnect()
	emit_signal("back_title")


# Called when the node enters the scene tree for the first time.
func load_artworks():
	Utils.fade_out($TreasureView, 0.005)
	if not DLC.has_content:
		return
	
	var content_list = DLC.get_content_list()
	var treasure_mini_array = []
	var treasure_original_array = []
	var treasure_character = []
	var treasure_prices = {}
	
	var counter = 0
	var treasure_x = 0
	var treasure_y = 0
	
	for content in content_list:
		var character_filename = "res://@content/"+content+"/character/Character.tscn"
		if Utils.has_file(character_filename):
			treasure_mini_array = treasure_mini_array + DLC.get_content_treasure_mini(content)
			treasure_original_array = treasure_original_array + DLC.get_content_treasure_original(content)
			Utils.merge_dict(treasure_prices, DLC.get_content_treasure_prices(content))
			for treasure in DLC.get_content_treasure_mini(content):
				treasure_character = treasure_character + [content]
	
	
	self.treasure_total = treasure_mini_array.size()
	

	while counter < treasure_total:
		var new_treasure = treasure_prefab.instance()
		new_treasure.position = Vector2(25 + treasure_x * 200, 35 + treasure_y * 170)
		new_treasure.name = treasure_mini_array[counter].replace(".jpg", "").replace(".import", "")
		
		if treasure_prices.has(new_treasure.name):
			new_treasure.get_node("Price").text = "Price:\n" + str(treasure_prices[new_treasure.name]) + " coins"
			new_treasure.init_treasure(treasure_character[counter], treasure_mini_array[counter], treasure_prices[new_treasure.name])
		else:
			new_treasure.get_node("Price").text = "Price:\n250 coins"
			new_treasure.init_treasure(treasure_character[counter], treasure_mini_array[counter].replace(".import", ""), 250)
		
		if MemoryCard.is_treasure_unlocked(new_treasure.name) or int(new_treasure.price) == 0:
			new_treasure.unlock()
		
		new_treasure.add_to_group("ItemSelect")
		treasure_list.append(new_treasure)
		treasure_area.call_deferred("add_child", new_treasure)
		
		new_treasure.connect("treasure_click", self, "treasure_click", [counter])
		new_treasure.connect("treasure_hover", self, "treasure_hover", [counter])
		
		treasure_x = treasure_x + 1
		if (treasure_x > 5):
			treasure_x = 0
			treasure_y = treasure_y + 1
		counter = counter + 1
	
	change_treasure(1)
	$Coins.text = str(MemoryCard.get_coins()) + " Coins"



# warning-ignore:unused_argument
func treasure_click(counter):
	if not panel_active: return


func treasure_hover(counter):
	if not panel_active: return
	change_treasure(counter + 1)


func input_connect():
	panel_active = true
	# 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_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_jump", self, "on_enter")
	# warning-ignore:return_value_discarded
	Controller.connect("controller_escape", self, "exit")

func input_disconnect():
	panel_active = false
	# 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_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_jump", self, "on_enter")
	# warning-ignore:return_value_discarded
	Controller.disconnect("controller_escape", self, "exit")

func on_right():
	change_treasure(treasure_item + 1)
	Audio.play_sfx("menu_move")
	
func on_left():
	change_treasure(treasure_item - 1)
	Audio.play_sfx("menu_move")

func on_up():
	change_treasure(treasure_item - 6)
	Audio.play_sfx("menu_move")

func on_down():
	change_treasure(treasure_item + 6)
	Audio.play_sfx("menu_move")

func change_treasure(item):
	var rect_pose = 0
	
	if item < 1: item = 1
	if item > treasure_total: item = treasure_total
	treasure_item = item
	
	var itemX = treasure_item
	var itemY = 0
	
	var counter = treasure_item
	while counter > 6:
		rect_pose = rect_pose + 1
		itemY = itemY + 1
		itemX = counter - 6
		counter = counter - 6
	itemX = itemX - 1
	
	treasure_selection.rect_position = Vector2( 20 + itemX * 200, 30 + itemY*170 )
	
	if rect_pose > 2 and rect_pose > actual_pose:
		Utils.fade_position(treasure_area, Vector2( treasure_area.position.x, treasure_area.position.y - 170 ), 0.0001)
	elif rect_pose > 1 and rect_pose < actual_pose:
		Utils.fade_position(treasure_area, Vector2( treasure_area.position.x, treasure_area.position.y + 170 ), 0.0001)
	actual_pose = rect_pose
	
	$Counter.text = str(item) + "/" + str(treasure_total)
	

func on_enter():
	if image_show:
		image_show = false
		Utils.fade_out($TreasureView)
		return
	
	
	if not range(treasure_list.size()).has(treasure_item - 1): return
		
	var coins = MemoryCard.get_coins()
	var treasure = self.treasure_list[treasure_item - 1]
		
	if not treasure.locked:
		$TreasureView/Image.texture = Utils.load_file(treasure.get_original_dir().replace(".import", ""))
		Utils.fade_in($TreasureView)
		image_show =  true
		
	elif treasure.price < coins:
		MemoryCard.unlock_treasure(treasure.name, treasure.price)
		treasure.unlock()
		$Coins.text = str(MemoryCard.get_coins()) + " Coins"
		Audio.play_sfx("unlock_treasure")

	else:
		Audio.play_sfx("not_enough_coins")


# warning-ignore:unused_argument
func _process(delta):
	$Coins.text = str(MemoryCard.get_coins()) + " Coins"
