using Godot;
using System;
using System.Drawing;

public partial class ObjectInteractionGUIManager : HBoxContainer
{
	private Label label;
	private RichTextLabel description;
	private RichTextLabel action;
	private Control labelBG;
	private Control descriptionBG;
	private Control actionBG;
	private Control detectionBox;
	private Control hint;

	[Export] public Vector2 margin;

	public bool isHighlighted = false;
	public bool isEnabled = false;
	private Vector2 offset = Vector2.Zero;
	private Vector2 boxSize = Vector2.Zero;

	public override void _Ready()
	{
		label = GetNode<Label>("DetectionBox/PanelContainer/MarginContainer/Label");
		labelBG = GetNode<Control>("DetectionBox/PanelContainer/LabelBackground");

		description = GetNode<RichTextLabel>("Caption/VBoxContainer/DescriptionContainer/MarginContainer/DescriptionLabel");
		descriptionBG = GetNode<Control>("Caption/VBoxContainer/DescriptionContainer");

		action = GetNode<RichTextLabel>("Caption/VBoxContainer/ActionContainer/MarginContainer/ActionLabel");
		actionBG = GetNode<Control>("Caption/VBoxContainer/ActionContainer");

		hint = GetNode<Control>("DetectionBox/CenterContainer/Control/HintRect");
		detectionBox = GetNode<Control>("DetectionBox/CenterContainer/BoundBox");
		Disable();
	}

	public void setProperties(InteractableObjectResource res)
	{
		label.Text = res.Name + "::" + res.Status;
		description.Text = res.Description;
		action.Text = "/// Action Available [" + res.Action + "]\n" + res.ActionDescription; 
		offset += new Vector2(label.Size.X, label.Size.Y);
	}

	public void setDetectionBoxSize(Vector2 bSize)
	{
		boxSize = bSize + margin;
		detectionBox.CustomMinimumSize = bSize + margin;
		Vector2 tmp = Size;
		tmp.Y = label.Size.Y + margin.Y + bSize.Y;
		Size = tmp;
	}

	public bool setScreenPosition(Vector3 wPos, Camera3D cam)
	{
		// Taken directly from Camera3D documentation
		// https://docs.godotengine.org/en/stable/classes/class_camera3d.html#class-camera3d-method-unproject-position
		if (cam.IsPositionBehind(wPos))
		{
			setVisibilityHighlight(false);
			setVisibilityHint(false);
			return false;
		}

		if (isHighlighted)
		{
			offset = new Vector2(label.Size.X, label.Size.Y);
			Vector2 b_b = new Vector2(-boxSize.X / 2, boxSize.Y / 2) + new Vector2(10, -5);
			Position = cam.UnprojectPosition(wPos) - offset - b_b;
		}
		else
		{
			Position = cam.UnprojectPosition(wPos) - new Vector2(10, -5);
		}
		return true;
	}

	private void setVisibilityHighlight(bool b)
	{
		label.Visible = b;
		description.Visible = b;
		action.Visible = b;
		labelBG.Visible = b;
		descriptionBG.Visible = b;
		actionBG.Visible = b;
		detectionBox.Visible = b;
	}

	private void setVisibilityHint(bool b)
	{
		hint.Visible = b;
	}

	public void Minimize()
	{
		if (!isEnabled) { return; }
		isHighlighted = false;
		setVisibilityHighlight(false);
		setVisibilityHint(true);
	}

	public void Expand()
	{
		if (!isEnabled) { return; }
		isHighlighted = true;
		setVisibilityHighlight(true);
		setVisibilityHint(false);
	}

	public void Disable()
	{
		isHighlighted = false;
		isEnabled = false;
		setVisibilityHighlight(false);
		setVisibilityHint(false);
	}

	public void Enable()
	{
		isEnabled = true;
		Minimize();
	}
}
