using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YBDevice.NApi.Application.ThirdClient
{
    /// <summary>
    /// 判断访问的客户端类型,抖音/微信
    /// </summary>
    public class ThirdOpenActionFilter : Attribute, IAsyncActionFilter
    {
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var request = context.HttpContext.Request;
            var ua = request.Headers["User-Agent"].ToString();
            string postdata = string.Empty;
            if (request.Body.Length > 0)
            {
                request.EnableBuffering();
                request.Body.Position = 0;
                using (var sr = new StreamReader(request.Body))
                {
                    postdata = await sr.ReadToEndAsync();
                    var a = JsonConvert.DeserializeObject(postdata);
                    //a.add
                }
            }
            int type = UAType(ua);
        }
        /// <summary>
        /// 获取UA类型
        /// </summary>
        /// <param name="ua"></param>
        /// <returns></returns>
        private int UAType(string ua)
          => ua switch
          {
              _ when ua.Contains("micromessenger") => UAConst.WeiXin,
              _ when ua.Contains("bytedancewebview") => UAConst.TouTiao,
              _ => UAConst.Other
          };
    }
}
