using System.Reflection;
using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using OpenAuth.App;
using OpenAuth.App.Interface;
using OpenAuth.Repository.Domain;
using OpenAuth.WebApi.Model.CustomAttribute;

namespace OpenAuth.WebApi.Model
{
    public class OpenAuthFilter : IActionFilter
    {
        private readonly IAuth _authUtil;
        private readonly SysLogApp _logApp;

        public OpenAuthFilter(IAuth authUtil, SysLogApp logApp)
        {
            _authUtil = authUtil;
            _logApp = logApp;
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            var description =
                (Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor;

            var Controllername = description.ControllerName.ToLower();
            var Actionname = description.ActionName.ToLower();

            //匿名标识
            var authorize = description.MethodInfo.GetCustomAttribute(typeof(AllowAnonymousAttribute));
            if (authorize != null)
            {
                return;
            }

            if (!_authUtil.CheckLogin())
            {
                context.HttpContext.Response.StatusCode = 401;
                context.Result = new JsonResult(new Response
                {
                    Code = 401,
                    Message = "认证失败，请提供认证信息"
                });
                return;
            }

            var dataAttr = description.MethodInfo.GetCustomAttribute<AllDataAttribute>();
            if (dataAttr != null)
            {
                var flag = _authUtil.IsSystem();
                if (flag)
                {
                    /*ar paras = description.MethodInfo.GetParameters();*/
                    var paras = context.HttpContext.Request.Query;

                    var routeValues = new RouteValueDictionary(paras.ToDictionary(x => x.Key, x => (object)x.Value.ToString()));

                    context.Result = new RedirectToActionResult(dataAttr.ActionName, dataAttr.ControllerName, routeValues);
                    //return;
                }
            }


            _logApp.Add(new SysLog
            {
                Content = $"用户访问",
                Href = $"{Controllername}/{Actionname}",
                CreateName = _authUtil.GetUserName(),
                CreateId = _authUtil.GetCurrentUser().User.Id,
                CreateTime = DateTime.Now,
                TypeName = "访问日志"
            });
        }

        public void OnActionExecuted(ActionExecutedContext context)
        {
            return;
        }
    }
}
