using Editor;
using Sandbox;
using static Editor.Inspectors.AssetInspector;
using static VeloX.EngineStream;

namespace VeloX;
[Dock( "Editor", "Engine Stream Editor", "local_fire_department" )]
[CanEdit( "asset:engstr" )]
internal sealed class EngineStreamEditor : Widget, IAssetInspector
{

	public EngineStreamEditor( Widget parent ) : base( parent, false )
	{
		CreateEditor();
	}
	public static EngineStreamEditor Instance { get; private set; }
	private EngineStream ActiveStream;
	private SerializedObject StreamSO;
	private ScrollArea StreamTabs;

	//[EditorEvent.Hotload]
	//internal void Reload()
	//{
	//	if ( Instance.IsValid() )
	//		CreateEditor();
	//}

	//[Menu( "Editor", "VeloX/Engine Stream Editor", "time_to_leave" )]
	internal static void Open()
	{
		Instance = new EngineStreamEditor();
	}

	internal EngineStreamEditor()
	{

		Size = new Vector2( 1300, 700 );
		CreateEditor();
		Show();
	}

	private class LayerObject
	{
		public Layer Layer { get; set; }
		public string Id { get; set; }
	}


	private void CreateEditor()
	{
		WindowTitle = "Engine Stream Editor";
		Layout = Layout.Column();
		//Canvas = new( null )
		//{
		//	Layout = Layout,
		//};

		CreateHeader();

		var b = Layout.AddRow();

		CreateTabs( b );
		CreateRightMenu( b );
	}
	private void CreateHeader()
	{

		var a = Layout.AddRow();
		a.Margin = new Sandbox.UI.Margin( 8, 12, 8, 16 );
		a.Alignment = TextFlag.Left;
		a.Spacing = 12;

		var fileButton = a.Add( new Button( "Open File" ), 0 );
		fileButton.Clicked = () =>
		{
			var picker = AssetPicker.Create( this,
				AssetType.FromType( typeof( EngineStream ) ),
				new() { EnableCloud = true, SeparateCloudTab = false }
				);

			picker.OnAssetPicked += asset => CreateStreamEditor( asset[0].LoadResource<EngineStream>() );
			picker.Show();
		};
		var saveButton = a.Add( new Button( "Save File" ), 0 );
		saveButton.Clicked = () =>
		{
			ActiveStream.StateHasChanged();
		};
	}
	Layout RightLayout;
	SimulatedEngineWidget SimulatedEngineWidget;
	private void CreateRightMenu( Layout layout )
	{
		RightLayout = layout.AddColumn( 1 );
		RightLayout.Alignment = TextFlag.Top;
		RebuildRightMenu();
	}

	private void RebuildRightMenu()
	{

		RightLayout.Clear( true );
		RightLayout.Spacing = 12;
		var layoutTop = RightLayout.AddColumn();
		SimulatedEngineWidget = new SimulatedEngineWidget( null, ActiveStream );
		layoutTop.Add( SimulatedEngineWidget );
		if ( StreamSO is not null )
			layoutTop.Add( new ParametersWidget( StreamSO.GetProperty( nameof( EngineStream.Parameters ) ) ) );

	}

	private void CreateTabs( Layout layout )
	{

		StreamTabs = layout.Add( new ScrollArea( this ), 5 );
		StreamTabs.Canvas = new Widget( StreamTabs )
		{
			Layout = Layout.Column(),
			VerticalSizeMode = (SizeMode)3,
		};

		StreamTabs.Canvas.Layout.Margin = new Sandbox.UI.Margin( 8, 8, 16, 8 );
		StreamTabs.Canvas.Layout.Alignment = TextFlag.Top;

	}

	public void RebuildLayers()
	{
		StreamTabs.Canvas.Layout.Clear( true );
		StreamSO = ActiveStream.GetSerialized();

		if ( !StreamSO.TryGetProperty( nameof( EngineStream.Layers ), out var layers ) )
			return;

		StreamTabs.Canvas.Layout.Add( new LayersEditorWidget( layers, ActiveStream, SimulatedEngineWidget ) );
	}

	private void CreateStreamEditor( EngineStream stream )
	{
		ActiveStream = stream;
		StreamSO = ActiveStream.GetSerialized();
		RebuildRightMenu();
		RebuildLayers();
	}
	public void SetAsset( Asset asset )
	{
		CreateStreamEditor( asset.LoadResource<EngineStream>() );
	}
}
