using System;
using System.Threading.Tasks;
using DiplomovaPrace.Contracts.Security.ViewModels;
using Fluxor.Blazor.Web.Components;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;

namespace DiplomovaPrace.Web.Admin.Pages
{
	public class PrivateRoute : FluxorComponent
	{
		[CascadingParameter]
		public Task<AuthenticationState> AuthenticationStateTask { get; set; }

		[Inject]
		protected NavigationManager NavigationManager { get; set; }

		protected override async Task OnInitializedAsync()
		{
			await base.OnInitializedAsync().ConfigureAwait(false);

			AuthenticationState authenticationState = await AuthenticationStateTask.ConfigureAwait(false);
			System.Security.Claims.ClaimsPrincipal user = authenticationState.User;

			if (!user.IsInRole(Role.Admin))
			{
				NavigationManager.NavigateTo($"login?returnUrl={Uri.EscapeDataString(NavigationManager.Uri)}");
				//NavigationManager.NavigateTo($"login?returnUrl={NavigationManager.ToBaseRelativePath(NavigationManager.Uri)}");
			}
		}
	}
}
