extends CanvasLayer

var active_quest_color = Color("ffff00")
var completed_quest_color = Color("00ff00")
var selectors = []

onready var tween = $dialogue_box/Tween

func _input(event):
	if event as InputEventKey:
		if event.scancode == 16777217 and event.is_pressed():
			ui_paused()
			
func ui_paused():
	Global.paused = !Global.paused 
	var paused = Global.paused
	Global.game.get_node("effects/blur").visible = paused
	$pause_menu.visible = paused
	get_tree().paused = paused
			
func ui_quest_list(quests):
	var lines = $quests_list.get_children()
	for l in lines:
		l.text = ""
	for i in quests.size():
		var color
		if quests[i]["completed"]:
			color = completed_quest_color
		else:
			color = active_quest_color
		lines[i].add_color_override("font_color", color)
		lines[i].text = quests[i]["body"]["description"] + " " + str(quests[i]["body"]["objectives"][0]["progress"])+"/"+str(quests[i]["body"]["objectives"][0]["required"]) 

func ui_show_dialogue_box(text, arrow, anim):
	var label = $dialogue_box/Label
	var time = 0

	$dialogue_box.visible = true
	$compass.visible = false
	ui_show_message("", Color())
	tween.stop_all()
	Interaction.is_ready(false) 
	if anim:
		label.percent_visible = 0
		label.text = text 
		time = range_lerp(text.length(), 0, 400, 0, 20)
		tween.playback_speed = 1
		tween.interpolate_property(label, "percent_visible", 0, 1, time, Tween.TRANS_LINEAR,Tween.EASE_IN_OUT)
		tween.start()
		yield(tween,"tween_completed")
	else:
		label.percent_visible = 1
		label.text = text
	Interaction.is_ready(true) 
	ui_show_next_arrow(arrow)
	$dialogue_box/selectors.visible = !arrow
	
func ui_force_text_anim():
	tween.playback_speed = 10

func ui_hide_dialogue_box():
	$dialogue_box.visible = false
	$compass.visible = true
	
func ui_show_next_arrow(state):
	var arrow_anim = $dialogue_box/Polygon2D/AnimationPlayer
	if state:
		arrow_anim.play("anim")
	else:
		arrow_anim.stop()
		arrow_anim.seek(0, true)
	
func ui_set_selector(i):
	for s in selectors:
		if s.get_index() == i:
			s.visible = true
		else:
			s.visible = false 
	
func ui_show_message(mes, color):
	$messages/Label.add_color_override("font_color", color)
	$messages/Label.text = mes
	
func ui_inventory(inventory):
	var slots = $inventory/slots.get_children()  
	for s in slots:
		s.texture = null
	for i in inventory.size():
		var alias = inventory[i]["body"]["alias"]
		var texture = load("res://sprites/items/" + alias + ".png")
		slots[i].texture = texture
	
func ui_fade_in():
	$fader.visible = true
	$fader/AnimationPlayer.play("fade_out")
	
func ui_update_health(new_value):
	$stats/health_bar/ProgressBar.value = new_value
	
func ui_update_exp(new_value):
	$stats/expiriens_bar/ProgressBar.value = new_value
	
func ui_update_strength(new_value):
	$stats/strength_bar/ProgressBar.value = new_value

func _on_slot_mouse_entered(n):
	if n < Interaction.inventory.size():
		var item = Interaction.inventory[n]
		var item_real_name = item["body"]["real_name"]
		var number = item["number"]
		$inventory/item_name.text = item_real_name + " x" + str(number) 
		ui_show_message("кликните, чтобы использовать предмет", Color("ffffff"))
	else:
		$inventory/item_name.text = "Пусто"

func _on_slot_mouse_exited():
	$inventory/item_name.text = ""
	ui_show_message("", Color())

func _on_slot_gui_input(event, n):
	if n < Interaction.inventory.size():
		if event as InputEventMouseButton:
			if event.pressed:
				Interaction.use_item(n)
	
func _ready():
	Global.gui = self
	selectors = $dialogue_box/selectors.get_children()
