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

namespace Knoks.Api.Filters
{
    public class OperatorActionFilter : Attribute, IActionFilter
    {
        private readonly ILogger _logger;
        public OperatorActionFilter(ILogger<OperatorActionFilter> logger)
        {
            _logger = logger;
        }

        public void OnActionExecuted(ActionExecutedContext context)
        {
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            var operatorIdClaim = context.HttpContext.User.FindFirst(OperatorIdClaim);
            if (operatorIdClaim != null)
                context.GetApiBaseController().ContextOperatorId = int.Parse(operatorIdClaim.Value);
        }
    }
}