using System;
using Application;
using Infrastructure.Identity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;

namespace Infrastructure
{
  public class HttpCurrentUserProvider : ICurrentUserProvider
  {
    private readonly IHttpContextAccessor _httpContextAccessor;
    private readonly UserManager<User> _userManager;

    public HttpCurrentUserProvider(IHttpContextAccessor httpContextAccessor, UserManager<User> userManager)
    {
      _httpContextAccessor = httpContextAccessor;
      _userManager = userManager;
    }

    public Guid GetCurrentUserId()
    {
      var userPrincipal = _httpContextAccessor.HttpContext.User;
      return Guid.Parse(_userManager.GetUserId(userPrincipal));
    }
  }
}