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


namespace PTMedicalInsurance.Helper
{
    class EncryptHelper
    {
        /// <summary>
        /// 移动支付加密
        /// </summary>
        /// <param name="data"></param>
        /// <param name="signText"></param>
        /// <returns></returns>
        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(Global.inf.appId, Global.inf.AK, Global.inf.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 encrypt(string data)
        {
            string encryptData = "";
            try
            {
                encryptData = GMUtilLib.SignUtil.encryptMsg(Global.inf.appId, Global.inf.AK, Global.inf.SK, data);
                return encryptData;
            }
            catch (Exception ex)
            {
                encryptData = ex.Message;
                return encryptData;
            }
            finally
            {
                Global.writeLog("ak:" + Global.inf.AK + ";sk:" + Global.inf.SK + ";appid:" + Global.inf.appId, data, encryptData);
            }
        }

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

        public int verify(string data,string encryptData)
        {
            string error ="";
            try
            {
                if (GMUtilLib.SignUtil.verifySm3WithSm2(data, Global.inf.AK, encryptData, Global.inf.SK)) return 0; else return -1;
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return -1;
            }
            finally
            {
                Global.writeLog("ak:" + Global.inf.AK + ";sk:" + Global.inf.SK + ";appid:" + Global.inf.appId, data + ";" + encryptData, error);
            }
        }

        public bool verify(JObject signDto, string signData)
        {
            string error = "";
            try
            {
                bool b = SMUtil.verify(signDto, Global.inf.AK, Global.inf.publicKey, signData);
                if (b)
                {
                    error = "成功";
                }
                else
                {
                    error = "失败";
                }
                return b;
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return false;
            }
            finally
            {
                Global.writeLog("验签：ak:" + Global.inf.AK + ";publicKey:" + Global.inf.publicKey + ";appid:" + Global.inf.appId, signDto.ToString() + ";" + signData, error);
            }
        }

        public string decrypt(string encryptData)
        {
            string data = "";
            try
            {
                data = GMUtilLib.SignUtil.decryptMsg(Global.inf.AK, Global.inf.SK, encryptData);
                return data;
            }
            catch (Exception ex)
            {
                data = ex.Message;
                return data;
            }
            finally
            {
                Global.writeLog("ak:" + Global.inf.AK + ";sk:" + Global.inf.SK + ";appid:" + Global.inf.appId, encryptData, data);
            }
        }

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