Editor/ChitChat/ControlWidgets/ChoiceControlWidget.cs
using Sandbox;
using Editor;

namespace ChitChat.Editor;

[CustomEditor(typeof(ChitChat.Choice))]
public class ChoiceControlWidget : ControlWidget
{
	public override bool SupportsMultiEdit => false;

	private string _assetPath;
	private Button _openAssetButton;
	private SerializedObject _so;

	public ChoiceControlWidget(SerializedProperty prop) : base(prop)
	{
		Layout = Layout.Column();
		Layout.Spacing = 8;
		Layout.Margin = 5;

		PaintBackground = false;

		SetStyles("background-color: #424242; border-radius: 5px;");

		if (!prop.TryGetAsObject(out _so))
		{
			Log.Error("Could not convert SerializedProperty to SerializedObject!");
			return;
		}

		_so.OnPropertyChanged += OnPropertyChanged;

		if (_so.TryGetProperty(nameof(Choice.Text), out SerializedProperty textProp))
		{
			Widget textContainer = Layout.Add(new Widget(this));
			textContainer.Layout = Layout.Row();
			textContainer.Layout.Alignment = TextFlag.LeftTop;
			Label textLabel = new Label("Text", this) 
			{
				ContentMargins = new Sandbox.UI.Margin(0, 0, 5, 0)
			};
			textContainer.Layout.Add(textLabel);
			TextAreaControlWidget textArea = new TextAreaControlWidget(textProp) 
			{
				FixedHeight = 65
			};
			textArea.SetStyles("background-color: #201F21;");
			textContainer.Layout.Add(textArea);

			Layout.Add(textContainer);
		}

		if (_so.TryGetProperty(nameof(Choice.Data), out SerializedProperty dataProp))
		{
			if (dataProp.TryGetValue(out DialogueData data))
			{
				_assetPath = data.ResourcePath;
			}

			Widget dataContainer = Layout.Add(new Widget(this));
			dataContainer.Layout = Layout.Row();
			dataContainer.Layout.Alignment = TextFlag.LeftTop;
			Label dataLabel = new Label("Data", this)
			{
				ContentMargins = new Sandbox.UI.Margin(0, 0, 5, 0),
				MaximumHeight = 20,
				ToolTip = "Where the choice leads."
			};
			dataContainer.Layout.Add(dataLabel);

			ControlWidget dataWidget = ControlWidget.Create(dataProp);
			dataWidget.ToolTip = "If left empty it will continue on the same data.";
			dataContainer.Layout.Add(dataWidget);
			
			dataContainer.Layout.AddSpacingCell(2);

			_openAssetButton = new Button("Open", "login", this)
			{
				Clicked = OpenAsset,
				MinimumHeight = 20,
				MinimumWidth = 80,
				Tint = new Color(0.25f, 0.47f, 0.3f),
				Visible = data.IsValid()
			};

			dataContainer.Layout.Add(_openAssetButton);

			Layout.Add(dataContainer);
		}
	}

	private void OpenAsset()
	{
		if (!string.IsNullOrEmpty(_assetPath))
			AssetSystem.FindByPath(_assetPath).OpenInEditor();
	}

	private void OnPropertyChanged(SerializedProperty prop)
	{
		if (prop.Name.Equals(nameof(ChitChat.Choice.Data)))
		{
			if(_openAssetButton.IsValid())
			{
				if (!prop.IsNull && prop.TryGetValue(out DialogueData data))
				{
					_assetPath = data.ResourcePath;
					_openAssetButton.Visible = true;
				}
				else
					_openAssetButton.Visible = false;
			}
		}
	}

	public override void OnDestroyed()
	{
		_so.OnPropertyChanged -= OnPropertyChanged;

		base.OnDestroyed();
	}
}