using System;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace Editor.ActionGraphs
{
	public interface IUserDataProperty
	{
		void Invalidate();
	}

	public record UserDataProperty<T> : IUserDataProperty
	{
		public JsonObject UserData { get; }
		public string Key { get; }
		public T DefaultValue { get; }

		private T _value;

		public UserDataProperty( JsonObject userData, string key, T defaultValue = default )
		{
			UserData = userData;
			Key = key;
			DefaultValue = defaultValue;

			Invalidate();
		}

		public void Invalidate()
		{
			_value = DefaultValue;

			if ( !UserData.TryGetPropertyValue( Key, out var node ) )
			{
				return;
			}

			try
			{
				_value = node.Deserialize<T>();
			}
			catch ( Exception e )
			{
				Log.Error( e );
			}
		}

		public T Value
		{
			get => _value;
			set
			{
				if ( (_value ?? DefaultValue).Equals( value ) )
				{
					return;
				}

				_value = value;
				UserData[Key] = JsonSerializer.SerializeToNode( value );
			}
		}
	}
}
