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

namespace OnlineAssessment
{
    public class OnlySuperAdminRequirement : IAuthorizationRequirement
    {
        public OnlySuperAdminRequirement(int roleId)
        {
            RoleId = roleId;
        }

        protected int RoleId { get; set; }
    }

    public class OnlySuperAdminHandler : AuthorizationHandler<OnlySuperAdminRequirement>
    {
        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, OnlySuperAdminRequirement requirement)
        {
            if (!context.User.HasClaim(c => c.Type == ClaimTypes.DateOfBirth && c.Issuer == "http://contoso.com"))
            {
                return Task.FromResult(0);
            }

            if (requirement.Equals(1))
            {
                context.Succeed(requirement);
            }

            //var dateOfBirth = Convert.ToDateTime(context.User.FindFirst(
            //    c => c.Type == ClaimTypes.DateOfBirth && c.Issuer == "http://contoso.com").Value);

            //int calculatedAge = DateTime.Today.Year - dateOfBirth.Year;
            //if (dateOfBirth > DateTime.Today.AddYears(-calculatedAge))
            //{
            //    calculatedAge--;
            //}

            //if (calculatedAge >= requirement.MinimumAge)
            //{
            //    context.Succeed(requirement);
            //}
            return Task.FromResult(0);
        }
    }
    //public class MinimumAgeRequirement : IAuthorizationRequirement
    //{
    //    public MinimumAgeRequirement(int age)
    //    {
    //        MinimumAge = age;
    //    }

    //    protected int MinimumAge { get; set; }
    //}


    //public class MinimumAgeHandler : AuthorizationHandler<MinimumAgeRequirement>
    //{
    //    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, MinimumAgeRequirement requirement)
    //    {
    //        if (!context.User.HasClaim(c => c.Type == ClaimTypes.DateOfBirth &&
    //                                   c.Issuer == "http://contoso.com"))
    //        {
    //            return Task.FromResult(0);
    //        }

    //        var dateOfBirth = Convert.ToDateTime(context.User.FindFirst(
    //            c => c.Type == ClaimTypes.DateOfBirth && c.Issuer == "http://contoso.com").Value);

    //        int calculatedAge = DateTime.Today.Year - dateOfBirth.Year;
    //        if (dateOfBirth > DateTime.Today.AddYears(-calculatedAge))
    //        {
    //            calculatedAge--;
    //        }

    //        if (calculatedAge >= requirement.MinimumAge)
    //        {
    //            context.Succeed(requirement);
    //        }
    //        return Task.FromResult(0);
    //    }
    //}
    //public class CustomAuthorizeFilter : IAsyncAuthorizationFilter
    //{
    //    public AuthorizationPolicy Policy { get; }

    //    public CustomAuthorizeFilter(AuthorizationPolicy policy)
    //    {
    //        Policy = policy ?? throw new ArgumentNullException(nameof(policy));
    //    }

    //    public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
    //    {
    //        if (context == null)
    //        {
    //            throw new ArgumentNullException(nameof(context));
    //        }

    //        // Allow Anonymous skips all authorization
    //        if (context.Filters.Any(item => item is IAllowAnonymousFilter))
    //    {
    //            return;
    //        }

    //        var policyEvaluator = context.HttpContext.RequestServices.GetRequiredService<IPolicyEvaluator> ();
    //        var authenticateResult = await policyEvaluator.AuthenticateAsync(Policy, context.HttpContext);
    //        var authorizeResult = await policyEvaluator.AuthorizeAsync(Policy, authenticateResult, context.HttpContext, context);

    //        if (authorizeResult.Challenged)
    //        {
    //            // Return custom 401 result
    //            context.Result = new CustomUnauthorizedResult("Authorization failed.");
    //        }
    //        else if (authorizeResult.Forbidden)
    //        {
    //            // Return default 403 result
    //            context.Result = new ForbidResult(Policy.AuthenticationSchemes.ToArray());
    //        }
    //    }
    //}
    //public class CustomUnauthorizedResult : JsonResult
    //{
    //    public CustomUnauthorizedResult(string message)
    //        : base(new CustomError(message))
    //    {
    //        StatusCode = StatusCodes.Status401Unauthorized;
    //    }
    //}
    //public class CustomError
    //{
    //    public string Error { get; }

    //    public CustomError(string message)
    //    {
    //        Error = message;
    //    }
    //}
}
