using System.Runtime.Intrinsics.X86;
using Microsoft.AspNetCore.Mvc.Filters;
using ASHermed.SinoInterface.Exception;
using ASHermed.SinoInterface.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ASHermed.SinoInterface.IFramework.SingleSignOn;
using ASHermed.SinoInterface.Api.ContractModel.Framework.Login;
using ASHermed.SingleSignOnFramework;
using ASHermed.SinoInterface.Enum;
using Microsoft.Extensions.Logging;
using Ashermed.ApiLogCoreFramework.Service;
using Ashermed.ApiLogCoreFramework.ContractModel;
using PoJun.Util.Helpers;
using ASHermed.SinoInterface.Api.IService;
using ASHermed.SinoInterface.Api.ContractModel.App;
using ASHermed.SinoInterface.Code;

namespace ASHermed.SinoInterface.WebApi.Filters
{
    /// <summary>
    /// 版本号校验
    /// </summary>
    public class VersionAttribute : ActionFilterAttribute, IActionFilter
    {
        /// <summary>
        /// token服务
        /// </summary>
        ITokenService tokenService;

        /// <summary>
        /// 文本日志
        /// </summary>
        ILogger<VersionAttribute> logger;
        AppConfigurtaion _appConfigurtaion;
        /// <summary>
        /// 版本号校验
        /// </summary>
        /// <param name="_tokenService"></param>
        /// <param name="_logger"></param>
        public VersionAttribute(ITokenService _tokenService, ILogger<VersionAttribute> _logger ,AppConfigurtaion appConfigurtaion)
        {
            tokenService = _tokenService;
            logger = _logger;
            _appConfigurtaion = appConfigurtaion;
        }

        /// <summary>
        /// 版本号校验
        /// </summary>
        /// <param name="context"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            if (string.IsNullOrEmpty(SysUtil.GetClientId()))
                throw new ParamErrorException("请传入正确的版本号！");

            if (SysUtil.GetClientType() == ClientType.None)
                throw new ParamErrorException("请传入正确的客户端类型！");
            var version = SysUtil.GetClientId();
            var clientType = SysUtil.GetClientType();
            var andVersion = _appConfigurtaion.GetInterfaceAndVersion;
            var iosVersion = _appConfigurtaion.GetInterfaceIosVersion;
            if (clientType == ClientType.Android)
            {
                var and = System.Convert.ToDouble(andVersion);
                if (and > System.Convert.ToDouble(version))
                {
                    throw new BaseException("软件版本过低，请更新后使用", DetailedStatusCode.ParamsError);
                }
            }
            if (clientType == ClientType.IOS)
            {
                var ios = System.Convert.ToDouble(iosVersion);
                if (ios > System.Convert.ToDouble(version))
                {
                    throw new BaseException("软件版本过低，请更新后使用", DetailedStatusCode.ParamsError);
                }
            }
            await base.OnActionExecutionAsync(context, next);
        }
    }
}
