using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Entities.OP;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Domain.Msg;
using APT.Infrastructure.Api;
using APT.Infrastructure.Api.Redis;
using APT.Infrastructure.Core;
using APT.Migrations;
using APT.OP.WebApi.Models;
using APT.Utility;
using CSRedis;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace APT.OP.WebApi.Controllers.Api
{
    [Route("api/OP/Wx")]
    public partial class WxController : APTApiController<T_OP_TENANT>
    {
        /// <summary>
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>

        [HttpGet, Route("GetAccessToken")]
        public JsonActionResult<string> GetAccessToken()
        {

            return base.SafeExecute(() =>
            {
                var client = CsRedisManager.GetClient();
                var appId = LibUtils.ToString(ConfigurationManager.WexinSettings["AppId"]);
                return GetAcToken(client, appId);
            });
        }

        /// <summary>
        /// url地址
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>

        [HttpGet, Route("GetSignature")]
        public JsonActionResult<WxSignModel> GetSignature(string url)
        {

            return SafeExecute<WxSignModel>(() =>
            {
                var client = CsRedisManager.GetClient();
                var appId = LibUtils.ToString(ConfigurationManager.WexinSettings["AppId"]);
                var redisKey = "WX_SIGNATER_" + appId;
                var ticket = string.Empty;
                if (client.Exists(redisKey))
                {
                    ticket = client.Get(redisKey);
                }
                else
                {
                    var wxAuthUrl = LibUtils.ToString(ConfigurationManager.WexinSettings["WxTicketUrl"]);
                    var accToken = GetAcToken(client, appId);
                    var realUrl = wxAuthUrl + $"?access_token={accToken}&type=jsapi";
                    var res = Utility.HttpMethods.Get(realUrl);
                    var model = JsonConvert.DeserializeObject<WxTicket>(res);
                    if (model != null && model.errcode == 0)
                    {
                        client.Set(redisKey, model.ticket, model.expires_in);// 微信token过期时间是7200s
                        ticket = model.ticket;
                    }
                    else
                    {
                        throw new Exception($"微信返回错误,错误代码{model?.errcode}：,错误信息:{model?.errmsg}");
                    }
                }
                //通过ticket生成签名
                var timestamp = Convert.ToInt64((DateTime.Now - new DateTime(1970, 1, 1)).TotalSeconds).ToString();
                var randomStr = GetRandomString(16);
                var singnStr = $"jsapi_ticket={ticket}&noncestr={randomStr}&timestamp={timestamp},&url={url}";
                var sign = SHA1(singnStr);
                WxSignModel signModel = new WxSignModel
                {
                    appId = appId,
                    nonceStr = randomStr,
                    signature = sign,
                    timestamp = timestamp
                };
                return signModel;
            });
        }


        private static string GetAcToken(CSRedisClient client, string appId)
        {
            var redisKey = "WX_ACCESSTOKEN_" + appId;
            if (client.Exists(redisKey))
            {
                return client.Get(redisKey);
            }
            var appSecret = LibUtils.ToString(ConfigurationManager.WexinSettings["AppSecret"]);
            var wxAuthUrl = LibUtils.ToString(ConfigurationManager.WexinSettings["WxAccessTokenUrl"]);
            var realUrl = wxAuthUrl + $"?appid={appId}&secret={appSecret}&grant_type=client_credential";
            var res = Utility.HttpMethods.Get(realUrl);
            var model = JsonConvert.DeserializeObject<WxAccessToken>(res);
            if (model != null)
            {
                client.Set(redisKey, model.access_token, model.expires_in);// 微信token过期时间是7200s
                return model.access_token;
            }
            else
            {
                throw new Exception($"微信返回错误");
            }
        }
        /// <summary>
        /// 从字符串里随机得到，规定个数的字符串.
        /// </summary>
        /// <param name="allChar"></param>
        /// <param name="CodeCount"></param>
        /// <returns></returns>
        private static string GetRandomString(int CodeCount)
        {
            string allChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
            string[] allCharArray = allChar.Split(',');
            string RandomCode = "";
            int temp = -1;
            Random rand = new Random();
            for (int i = 0; i < CodeCount; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
                }
                int t = rand.Next(allCharArray.Length - 1);
                while (temp == t)
                {
                    t = rand.Next(allCharArray.Length - 1);
                }
                temp = t;
                RandomCode += allCharArray[t];
            }

            return RandomCode;
        }
        /// <summary>  
        /// SHA1 加密，返回大写字符串  
        /// </summary>  
        /// <param name="content">需要加密字符串</param>  
        /// <returns>返回40位UTF8 大写</returns>  
        public static string SHA1(string content)
        {
            return SHA1(content, Encoding.UTF8);
        }
        /// <summary>  
        /// SHA1 加密，返回大写字符串  
        /// </summary>  
        /// <param name="content">需要加密字符串</param>  
        /// <param name="encode">指定加密编码</param>  
        /// <returns>返回40位大写字符串</returns>  
        public static string SHA1(string content, Encoding encode)
        {
            try
            {
                SHA1 sha1 = new SHA1CryptoServiceProvider();
                byte[] bytes_in = encode.GetBytes(content);
                byte[] bytes_out = sha1.ComputeHash(bytes_in);
                sha1.Dispose();
                string result = BitConverter.ToString(bytes_out);
                result = result.Replace("-", "");
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception("SHA1加密出错：" + ex.Message);
            }
        }


    }
}
