using EasyCaching.Core;
using IRaCIS.Core.Domain.Share;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;


namespace IRaCIS.Core.Application.BusinessFilter;




public class LimitUserRequestAuthorization : IAsyncAuthorizationFilter
{


    private readonly IEasyCachingProvider _provider;

    private readonly IUserInfo _userInfo;

    public LimitUserRequestAuthorization(IEasyCachingProvider provider, IUserInfo userInfo)
    {
        _provider = provider;
        _userInfo = userInfo;
    }


    public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
    {
        if (context.ActionDescriptor.EndpointMetadata.Any(item => item is IAllowAnonymous))
        {
            //匿名访问的不处理
        }
        else
        {
            //1、用户登陆的时候，设置缓存



            //2、在这里取缓存  进行比较  看是否有其他人进行了登陆，如果其他人登陆了，就把之前用户挤掉

            var cacheUserToken = (await  _provider.GetAsync<string>(_userInfo.Id.ToString())).Value;

            if (cacheUserToken == null)
            {
                //设置当前用户最新Token
              await  _provider.SetAsync(_userInfo.Id.ToString(), _userInfo.UserToken, TimeSpan.FromDays(7));

            }
            //是同一个人  
            else if(cacheUserToken == _userInfo.UserToken)
            {

            }
            else
            {
                context.Result = new StatusCodeResult(401);
            }

            var cacheHostToken = (await _provider.GetAsync<string>(_userInfo.IP.ToString()+_userInfo.LocalIp.ToString())).Value;

            if (cacheHostToken == null)
            {
                //设置当前主机最新Token
                await _provider.SetAsync(_userInfo.IP.ToString() + _userInfo.LocalIp.ToString(), _userInfo.UserToken, TimeSpan.FromDays(7));

            }
            //是同主机
            else if (cacheHostToken == _userInfo.UserToken)
            {

            }
            else
            {
                context.Result = new StatusCodeResult(401);
            }

        }
    }
} 














    //public class UserTypeRequirement : IAuthorizationRequirement
    //{
    //}

    //public class UserTypeHandler : AuthorizationHandler<UserTypeRequirement>
    //{

    //    private IUserInfo _userInfo;

    //    public UserTypeHandler(IUserInfo userInfo)
    //    {
    //        _userInfo = userInfo;
    //    }


    //    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, UserTypeRequirement requirement)
    //    {

    //        //if (context.User.Claims.Count() == 0)
    //        //{
    //        //    return Task.CompletedTask;
    //        //}

    //        //string userId = context.User.Claims.First(c => c.Type == "Userid").Value;
    //        //string qq = context.User.Claims.First(c => c.Type == "QQ").Value;

    //        //if (_UserService.Validata(userId, qq))
    //        //{
    //        //    context.Succeed(requirement); //验证通过了
    //        //}
    //        ////在这里就可以做验证

    //        return Task.CompletedTask;
    //    }
    //}

