Code/UI/NodePanelRoot.cs
namespace Nodebox.UI;

[Title("Nodebox Node Panel Root")]
public sealed class NodePanelRoot : PanelComponent, IGraphStyle {
	[Property, ReadOnly]
	[JsonIgnore]
	[Change(nameof(OnNodeChanged))]
	public Node Node { get; set; } = null;

	[Property]
	[JsonInclude]
	public GraphStyle GraphStyle { get; set; }


	[JsonIgnore]
	public Action<PinRef> PinClicked { get; set; }
	[JsonIgnore]
	public Action<PinRef> PinDown { get; set; }
	[JsonIgnore]
	public Action<PinRef> PinUp { get; set; }

	[JsonIgnore]
	public NodePanel NodePanel { get; private set; }
	public void OnNodeChanged(Node oldNode, Node newNode) {
		if (NodePanel.IsValid()) {
			NodePanel.Delete();
			NodePanel = null;
		}

		if (newNode.IsValid()) {
			Assert.True(newNode.GetType() != typeof(Node), $"what in the actual fuck {newNode.GetType()}");
			NodePanel = PanelAttribute.CreatePanelFor(newNode);
			NodePanel.Node = Node;
			NodePanel.Parent = Panel;

			NodePanel.PinClicked += (node, pinRef) => PinClicked?.Invoke(pinRef);
			NodePanel.PinDown += (node, pinRef) => PinDown?.Invoke(pinRef);
			NodePanel.PinUp += (node, pinRef) => PinUp?.Invoke(pinRef);
		}
	}

	protected override void OnTreeFirstBuilt() {
		base.OnTreeFirstBuilt();
		Assert.IsValid(Panel);
		NodePanel.Parent = Panel;
	}

	protected override int BuildHash() => HashCode.Combine(
		GraphStyle,
		Node,
		Node?.Generation
	);
}