using System.Reactive.Linq;

using CommunityToolkit.Mvvm.ComponentModel;

using Dock.Model.Mvvm.Controls;

using DynamicData.Binding;

using InkForge.Desktop.Managers;
using InkForge.Desktop.Models;
using InkForge.Desktop.ViewModels.Workspaces;

namespace InkForge.Desktop.ViewModels.Tools;

public partial class WorkspaceTool : Tool
{
	private readonly IWorkspaceViewModelFactory _workspaceViewModelFactory;
	[ObservableProperty] private WorkspaceViewModel? _workspace;

	public WorkspaceTool(WorkspaceManager workspaceManager, IWorkspaceViewModelFactory workspaceViewModelFactory)
	{
		_workspaceViewModelFactory = workspaceViewModelFactory;
		
		Title = "Explorer";
		CanClose = false;
		CanFloat = false;
		CanPin = false;

		workspaceManager.WhenValueChanged(v => v.Workspace).Subscribe(OnWorkspaceManagerWorkspaceChanged);
	}

	private void OnWorkspaceManagerWorkspaceChanged(Workspace? workspace)
	{
		Workspace = workspace switch
		{
			{ } v => _workspaceViewModelFactory.Create(v),
			_ => null
		};
	}
}
