using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PTMedicalInsurance.Common;
using Newtonsoft.Json.Linq;
//using GMUtilLib;
using PTMedicalInsurance.Variables;
using Newtonsoft.Json;
using SM2Crypto.Lib;

namespace PTMedicalInsurance.Helper
{
    class EncryptHelper
    {
        //private string ak = "1G94963I20403F60C80A00005FF7A699";
        //private string sk = "APyE9G1D+C8g3qV3Z6VshXztOa55YJBgwN+P4WqU5n0x";
        //private string appid = "1G94963HS03V3F60C80A00000DB330D8";

        ////测试环境
        //private string ak = "1GBJO22LJ00L3F60C80A00003C968398";  //appSecret 数字密钥sm4
        //private string sk = "AM4vzgpILAq9Rzh+uSIi4oXx5hl3jxUjvshjXXuAPggE";  //渠道私密
        //private string appid = "1GBJO22LD00K3F60C80A0000DD00AEFF";  //渠道ID
        //private string publicKey = "BIW3vR29ZhwXhhbEn6fyYV6zo4MHZyKfsOEf8AzJQf7WM+PzLcAiVj1JLqpumOyvAhjTnmEVVeVWpB/iNSHKaLE=";            //平台公钥  验签

        ////正式环境
        //private string ak = "1GFIFVQQK04NE2470B0A00007DAE4B06";  //appSecret 数字密钥sm4
        //private string sk = "ALx/0FBWNQU8oUXXepBwwl9Tj0VREBfNnZxQfeGuAu9q";  //渠道私密
        //private string appid = "1GFIFVQQ104ME2470B0A0000B6E489E1";  //渠道ID
        //private string publicKey = "BK3M7nR1/AvhakqYMK23Kdek9ts2Ov732kL9t1ACoSSLqbAL2+IejtmJeaeTUNqSuctoDCT4Gsd0AGt7ZTIzgqY=";            //平台公钥  验签

        private string ak = "";  //appSecret 数字密钥sm4
        private string sk = "";  //渠道私密
        private string appid = "";  //渠道ID
        private string publicKey = "";            //平台公钥  验签


        public EncryptHelper(string appId, string appSecret, string publicKey, string privateKey)
        {
            this.appid = appId;
            this.ak = appSecret;
            this.sk = privateKey;
            this.publicKey = publicKey;

            Global.writeLog($"appSecret:{ak} ;privateKey:{sk} ;appid:{appid};publicKey:{publicKey}");
        }
        public string encrypt(string data, ref string signText)
        {
            string encryptData = "";
            try
            {
                string strData = JsonConvert.SerializeObject(JObject.Parse(data), Newtonsoft.Json.Formatting.None);
                encryptData = SignUtils.encryptMsg(appid, ak, sk, strData, ref signText);

                return encryptData;
            }
            catch (Exception ex)
            {
                encryptData = ex.Message;
                return encryptData;
            }
            finally
            {
                //Global.writeLog("ak:" + appSecret + ";sk:" + privateKey + ";appid:" + appId, data, encryptData);
            }
        }

        public string sign(string data)
        {
            string encryptData = "";
            try
            {
                encryptData = SignUtils.signSm3WithSm2(ak, sk, data);
                return encryptData;
            }
            catch (Exception ex)
            {
                encryptData = ex.Message;
                return encryptData;
            }
            finally
            {
                Global.writeLog("ak:" + ak + ";sk:" + sk + ";appid:" + appid, data, encryptData);
            }
        }

        public string sortKeys(JObject obj) {

            StringBuilder sb = new StringBuilder();
            Dictionary<string, string> dict = new Dictionary<string, string>();
            foreach (var p in obj.Properties())
            {
                dict.Add(p.Name, p.Value.ToString());
            }
            string[] keys = dict.Keys.ToArray();
            Array.Sort(keys, string.CompareOrdinal);
            foreach (var k in keys)
            {
                if (sb.Length > 1)
                {
                    sb.Append("&");
                }
                sb.Append(k);
                sb.Append("=");
                sb.Append(dict[k]);
            }
            return sb.ToString();
        }
        public bool verify(JObject signDto, string signData)
        {
            string error = "";
            try
            {
                bool b = SMUtil.verify(signDto, ak, publicKey, signData);
                if (b)
                {
                    error = "成功";
                }
                else
                {
                    error = "失败";
                }
                return b;
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return false;
            }
            finally
            {
                Global.writeLog("验签：ak:" + ak + ";publicKey:" + publicKey + ";appid:" + appid, signDto.ToString() + ";" + signData, error);
            }
        }

        public string decrypt(string encryptData)
        {
            string data = "";
            try
            {
                data = SMUtil.decrypt(encryptData, appid, ak);
                return data;
            }
            catch (Exception ex)
            {
                data = ex.Message;
                return data;
            }
            finally
            {
                Global.writeLog("解密：ak:" + ak + ";sk:" + sk + ";appid:" + appid, encryptData, data);
            }
        }
    }
}
