tool
extends HBoxContainer

var editor_reference
var image_node

func _ready():
	$ButtonDelete.icon = get_icon("Remove", "EditorIcons")


func _on_ButtonDelete_pressed():
	if $NameEdit.text == 'Default':
		$PathEdit.text = ''
		update_preview('')
	else:
		queue_free()


func _on_ButtonSelect_pressed():
	editor_reference.godot_dialog("*.png, *.svg")
	editor_reference.godot_dialog_connect(self, "_on_file_selected")


func _on_file_selected(path, target):
	update_preview(path)
	$PathEdit.text = path
	if $NameEdit.text == '':
		$NameEdit.text = DialogicResources.get_filename_from_path(path)


func _on_focus_entered():
	if $PathEdit.text != '':
		update_preview($PathEdit.text)


func update_preview(path):
	var l_path = path.to_lower()
	if path == '':
		image_node.texture = null
	else:
		if '.png' in l_path or '.svg' in l_path:
			image_node.texture = load(path)
			return true
	return false
