using Microsoft.AspNetCore.Mvc.Filters;
using NLog;
using System;
using System.Collections.Generic;
using System.Text;

namespace System.Common
{
    public class ActionFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var request = context.HttpContext.Request;

            var log = LogManager.GetCurrentClassLogger();
            string path = context.ActionDescriptor.AttributeRouteInfo.Template;
            if (!string.IsNullOrEmpty(path))
            {
                log = LogManager.GetLogger(path);
            }

            bool islog = false;
            try
            {
                if (request.Method == "GET")
                {
                    islog = request.Query.ContainsKey("log");
                }
                else
                {
                    islog = request.Form.ContainsKey("log");
                }
            }
            catch
            {
                //log.Error("ContentType：" + request.ContentType);
            }
            if (islog)
            {
                var ip = request.Headers["X-Forwarded-For"];
                if (string.IsNullOrEmpty(ip))
                {
                    ip = context.HttpContext.Connection.RemoteIpAddress.ToString();
                }
                string userinfo = string.Empty;
                var claims = context.HttpContext.User.Claims.GetEnumerator();
                while (claims.MoveNext())
                {
                    if (claims.Current.Type.IndexOf("/") >= 0)
                    {
                        userinfo += claims.Current.Type.Substring(claims.Current.Type.LastIndexOf('/') + 1) + ":" + claims.Current.Value + ";";
                    }
                }

                var param = string.Empty;
                if (request.Method == "GET")
                {
                    param = request.Query.ToJson();
                }
                else
                {
                    param = request.Form.ToJson();
                }
                log.Error("IP：" + ip.ToString() + "|||UserInfo：" + userinfo + "|||ContentType：" + request.ContentType + "|||Params：" + param);

            }
            base.OnActionExecuting(context);
        }

        public override void OnActionExecuted(ActionExecutedContext context)
        {
            var request = context.HttpContext.Request;
            bool islog = false;
            try
            {

                if (request.Method == "GET")
                {
                    islog = request.Query.ContainsKey("log");
                }
                else
                {
                    if (!context.Result.ToJson().Contains("{\\\"state\\\":\\\"success\\\""))
                        islog = request.Form.ContainsKey("log");
                }


            }
            catch
            {
                islog = true;
            }
            if (islog)
            {
                var log = LogManager.GetCurrentClassLogger();
                string path = context.ActionDescriptor.AttributeRouteInfo.Template;
                if (!string.IsNullOrEmpty(path))
                {
                    log = LogManager.GetLogger(path);
                }

                log.Error(context.Result.ToJson());
            }

            base.OnActionExecuted(context);
        }
    }
}
