using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using System;
using static Knoks.Api.Authentication.IdentityConsts;

namespace Knoks.Api.Filters
{
    public class UserActionFilter : Attribute, IActionFilter
    {
        private readonly ILogger _logger;

        public UserActionFilter(ILogger<UserActionFilter> logger)
        {
            _logger = logger;
        }

        public void OnActionExecuted(ActionExecutedContext context)
        {
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            var userIdClaim = context.HttpContext.User.FindFirst(UserIdClaim);
            if (userIdClaim != null)
                context.GetApiBaseController().ContextUserId = long.Parse(userIdClaim.Value);
        }
    }
}
