@using Ardalis.GuardClauses
@using global::Photino.Blazor
@using SharpIDE.Photino.Models

@inject PhotinoBlazorApp PhotinoBlazorApp
@inject AppState AppState

<MudStack Class="mx-4">
	<MudStack Row="true">
		<MudTextField T="string" Label="Solution File" Value="@_solutionFilePath" ReadOnly="true" />
		<MudButton OnClick="@PickSolution">Pick Solution</MudButton>
	</MudStack>
	<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@Proceed" Disabled="@(string.IsNullOrWhiteSpace(_solutionFilePath))">Open</MudButton>
</MudStack>

@code {
	[CascadingParameter]
    public IMudDialogInstance MudDialog { get; set; } = null!;

	[Parameter]
    public bool AutoOpenLastSolution { get; set; }

	private string? _solutionFilePath;

	protected override void OnInitialized()
	{
		_solutionFilePath = AppState.SolutionFilePath;
	}

	protected override void OnAfterRender(bool firstRender)
	{
		if (firstRender)
		{
			if (AutoOpenLastSolution && !string.IsNullOrWhiteSpace(_solutionFilePath))
			{
				MudDialog.Close(_solutionFilePath);
			}
		}
	}

	private async Task PickSolution()
	{
		var files = await PhotinoBlazorApp.MainWindow.ShowOpenFileAsync("Choose Solution File", filters: [("Solution File", [".sln", ".slnx"])]);
		var slnFile = files.SingleOrDefault();
		_solutionFilePath = slnFile;
	}

	private void Proceed()
	{
		Guard.Against.NullOrWhiteSpace(_solutionFilePath);
		AppState.SolutionFilePath = _solutionFilePath;
		MudDialog.Close(_solutionFilePath);
	}
}
