extends Control

var selection_object: Control
var infobox_position := "left"


onready var hover_tween = $HoverTween
onready var info_panel = $Control/InfoPanel


func _ready():
	info_panel.modulate = Color(1, 1, 1, 0)
	connect("mouse_entered", self, '_on_mouse_entered')
	connect("mouse_exited", self, '_on_mouse_exited')
	hover_tween.connect("tween_completed", self, "_on_HoverTween_tween_completed")


func _on_mouse_exited():
	hover_tween.interpolate_property(selection_object, "modulate", null, Color(1, 1, 1, 1), 0.3, Tween.TRANS_CUBIC, Tween.EASE_IN_OUT)
	hover_tween.interpolate_property(info_panel, "modulate",  null, Color(1, 1, 1, 0), 0.3, Tween.TRANS_CUBIC, Tween.EASE_IN_OUT)
	hover_tween.start()
	
  
func _on_mouse_entered():
	set_infobox_position()
	info_panel.show()
	hover_tween.stop_all()
	hover_tween.interpolate_property(selection_object, "modulate",  null, Color("#615ea4"), 0.3, Tween.TRANS_CUBIC, Tween.EASE_IN_OUT)
	hover_tween.interpolate_property(info_panel, "modulate",  null, Color(1, 1, 1, 1), 0.3, Tween.TRANS_CUBIC, Tween.EASE_IN_OUT)
	hover_tween.start()


func set_infobox_position():
	var s = rect_size
	var info_s = info_panel.rect_size
	var offset = 30
	match infobox_position:
		"top":
			info_panel.rect_position = Vector2((s.x - info_s.x)/2, -(info_s.y + offset))
		"bottom":
			info_panel.rect_position = Vector2((s.x - info_s.x)/2, s.y + offset)
		"left":
			info_panel.rect_position = Vector2(-(info_s.x + offset), (s.y - info_s.y)/2)
		"right":
			info_panel.rect_position = Vector2(s.x + offset, (s.y - info_s.y)/2)


func _on_HoverTween_tween_completed(object: Object, key: NodePath):
	if object == info_panel and info_panel.modulate == Color(1, 1, 1, 0):
		info_panel.hide()
		
