using System.Security.Claims;
using eNavigator.Interfaces;
using eNavigator.Models;
using Microsoft.AspNetCore.Components.Authorization;

namespace eNavigator.Authentication
{
  public class NavigatorAuthenticationStateProvider : AuthenticationStateProvider
  {
    private readonly IStateContainer stateContainer;

    public NavigatorAuthenticationStateProvider(IStateContainer stateContainer)
    {
      this.stateContainer = stateContainer ?? throw new ArgumentNullException(nameof(stateContainer));
    }

    public override async Task<AuthenticationState> GetAuthenticationStateAsync()
    {
        String accessToken = stateContainer.Coalesce<String>(StateStrings.Token, default(String));
        bool authenticated = default == accessToken ? false : true;

        ClaimsIdentity identity = authenticated
            ? new ClaimsIdentity(null, "Basic")
            : new ClaimsIdentity();

        ClaimsPrincipal principal = new ClaimsPrincipal(identity);

        AuthenticationState authenticationState = new AuthenticationState(principal);

        return await Task.FromResult(authenticationState);
    }
  }
}