using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization.Infrastructure;
using System.Threading.Tasks;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;

namespace Api.SignToken
{
    public class PermissionAuthorizationHandler : AuthorizationHandler<PermissionAuthorizationRequirement>
    {
        IPermissionService _permissionService;

        /// <summary>
        /// นนิ์
        /// </summary>
        /// <param name="schemes"></param>
        public PermissionAuthorizationHandler(IPermissionService permissionService)
        {
            _permissionService = permissionService;
        }

        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, PermissionAuthorizationRequirement requirement)
        {
            if (context.User != null)
            {
                string s = _permissionService.GetPermission();
                string v = s;

                if (context.User.IsInRole("admin"))
                {
                    context.Succeed(requirement);
                }
                else
                {
                    //string s=_permissionService.GetPermissionAsync().Result;
                    //string v = s;

                    context.Succeed(requirement);
                  
                    //var userIdClaim = context.User.FindFirst(_ => _.Type == ClaimTypes.NameIdentifier);
                    //if (userIdClaim != null)
                    //{
                    //    if (_userStore.CheckPermission(int.Parse(userIdClaim.Value), requirement.Name))
                    //    {
                    //        context.Succeed(requirement);
                    //    }
                    //}
                }
            }
            return Task.CompletedTask;
        }
    }
}
