
using Editor;
using Sandbox;
using System.IO;

namespace VeloX;


internal sealed class LayersEditorWidget : ControlObjectWidget
{
	public EngineStream Stream;
	SimulatedEngineWidget SimulatedEngineWidget;
	public LayersEditorWidget( SerializedProperty property, EngineStream stream, SimulatedEngineWidget SimulatedEngine ) : base( property, true )
	{
		SimulatedEngineWidget = SimulatedEngine;
		PaintBackground = false;
		Stream = stream;
		Layout = Layout.Column();
		Layout.SizeConstraint = SizeConstraint.SetMaximumSize;
		Layout.Spacing = 12;

		SetStyles( "background-color:rgba(0,0,0,0);" );

		if ( SerializedObject is not SerializedCollection sc )
			return;
		sc.OnEntryAdded += Rebuild;
		sc.OnEntryRemoved += Rebuild;

		Rebuild();
	}

	private void Rebuild()
	{
		Layout.Clear( true );
		if ( SerializedObject is not SerializedCollection sc )
			return;

		foreach ( var t in sc )
		{
			var id = t.GetKey();
			Layout.Add( new LayerObjectWidget( t, id, sc, SimulatedEngineWidget ) );
		}
		Layout.Add( new Button( "Add Layer" ) ).Clicked = () =>
		{
			sc.Add( "", null );
		};
	}

}
