using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AnHuiMI.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PTMedicalInsurance.Variables;
using GMCrypto.Lib;
using PTMedicalInsurance.Common;

namespace PTMedicalInsurance.Helper
{
    class EncryptHelper
    {

        //private string appSecret = "1G566663V0033F60C80A0000B26913B4";
        //private string privateKey = "DsexH970Mz9RAuu9SZtqdz/JwwZtppfS1lzOZvtO/Q==";
        //private static string appId = "1G566663I0023F60C80A00000A8F1C36";

        //微信测试
        //private string appSecret = "1H1INA1L90OH3F60C80A00008119D616";   //appSecret 数字密钥sm4
        //private string privateKey = "APCIAgJqh3+AcK/IXL1WJD130i2q+6UblRxQzus3+sVw";     //渠道私密
        //private string appId = "1H1INA1L30OG3F60C80A0000DEE43558";    //渠道ID
        //private string publicKey = "BDMsMM2HPRkaKSl2ynBbCRtJodP8Nh4G5IkEnV+7YHaCplkAZbPMsUlvJpWqQ+Q4sT7611xGSZ1/mPsqgqJ49zs=";            //平台公钥

        //正式
        //private string ak = "1H62Q1KH205K76430B0A0000BF149773";
        //private string sk = "YbNObZNMdUgwgLUEyK4ixNSkaCF9OPtCdDth9APWYKU=";
        //private string appid = "1H62Q1KGP05J76430B0A00007144E257";

        //支付宝测试
        //private string appSecret = "1HFEACAP303F3F60C80A0000039D8740";   //appSecret 数字密钥sm4
        //private string privateKey = "e5u2OBbOAofe0MJWFw9gyZXpPKHrNR8SAwhO4VctPdY=";     //渠道私密
        //private string appId = "1HFEACAOT03E3F60C80A000013E2CAE7";    //渠道ID
        //private string publicKey = "BAXEY2R5We2CGLWJEbT7U09IMMvi/yoa2R7qWfoRnmxYHNchjFUNb7OEDe5qEMFasfvoEkZFatqfd3qDLmuxkTg=";            //平台公钥
        // 支付宝正式
        private string appId = "1HIL9529R00S76430B0A0000E404A442";    //渠道ID
        private string appSecret = "1HIL952A300T76430B0A00002E935C4F";   //appSecret 数字密钥sm4
        private string privateKey = "AM6zW7ET7L15vZLz4H53Q9uNoQr83FEfvU0P+uqvdYh4";     //渠道私密
        private string publicKey = "BPJ3dKzwOgGf0rA70YU3cUxtASeI2Arg1IOtUtBHVmUuj0XcZq0gkn2RqxMdzMC2gcAd25g+Uxj6l+GvCMTQCXA=";            //平台公钥


        public EncryptHelper()
        { }

        public EncryptHelper(string appId,string secret,string prvKey,string pubKey)
        {
            this.appId = appId;
            this.appSecret = secret;
            this.privateKey = prvKey;
            this.publicKey = pubKey;
        }


        public string getSignText(string data)
        {
            return "";
            //string ts = DateTime.Now.ToString("yyyyMMddHHmmss");
            //return SignUtils.getSignText(appid, ak, data,StringUtils.CurrentTimeStamp());
        }
        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, appSecret, privateKey, 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(appSecret, privateKey, data);
                return encryptData;
            }
            catch (Exception ex)
            {
                encryptData = ex.Message;
                return encryptData;
            }
            finally
            {
                //Global.writeLog("【签名】ak:" + appSecret + ";sk:" + privateKey + ";appid:" + appId, data, encryptData);
            }
        }

        public bool verify(JObject signDto, string signData)
        {
            string error = "";
            try
            {
                return SMUtil.verify(signDto, appSecret, publicKey, signData);
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return false;
            }
            finally
            {
                //Global.writeLog("【验签】ak:" + appSecret + ";sk:" + privateKey + ";appid:" + appId, signDto.ToString() + ";" + signData, error);
            }
        }

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