extends HBoxContainer

# HIGHLIGHT
@onready var label: Label = $DetectionBox/PanelContainer/MarginContainer/Label;
@onready var description: RichTextLabel = $Description/DescriptionLabel;
@onready var label_bg: Control = $DetectionBox/PanelContainer/LabelBackground;
@onready var detection_box: Control = $DetectionBox/CenterContainer/BoundBox;

# HINT
@onready var hint: Control = $DetectionBox/CenterContainer/Control/HintRect;

@export var margin: Vector2;

var is_highlighted: bool = false;
var ss_offset: Vector2 = Vector2.ZERO;
var box_size: Vector2 = Vector2.ZERO;

func _ready() -> void:
	minimize();

func set_properties(res: NodeInteractableBase):
	label.text = res.name;
	description.text = res.description;
	ss_offset += Vector2(0, label.size.y);

func set_detection_box_size(bsize: Vector2) -> void:
	box_size = bsize + margin;
	detection_box.custom_minimum_size = bsize + margin;
	var tmp: Vector2 = size;
	tmp.y = label.size.y + margin.y + bsize.y;
	size = tmp;

func set_screen_position(wPos: Vector3, cam: Camera3D) -> bool:
	# Taken directly from Camera3D documentation
	# https://docs.godotengine.org/en/stable/classes/class_camera3d.html#class-camera3d-method-unproject-position
	if (cam.is_position_behind(wPos)):
		set_visibility_highlight(false);
		set_visibility_hint(false);
		return false;
	if (is_highlighted):
		position = cam.unproject_position(wPos) - ss_offset - (box_size / 2);
	else:
		position = cam.unproject_position(wPos);
	return true;

func set_visibility_highlight(b: bool) -> void:
	label.visible = b;
	description.visible = b;
	label_bg.visible = b;
	detection_box.visible = b;

func set_visibility_hint(b: bool) -> void:
	hint.visible = b;

func highlight() -> void:
	is_highlighted = true;
	set_visibility_highlight(true);
	set_visibility_hint(false);

func minimize() -> void:
	is_highlighted = false;
	set_visibility_highlight(false);
	set_visibility_hint(true);
