using ASHermed.SinoInterface.Api.ContractModel;
using ASHermed.SinoInterface.Api.ContractModel.App;
using ASHermed.SinoInterface.Api.ContractModel.Framework.Login;
using ASHermed.SinoInterface.Api.ContractModel.Tencent;
using ASHermed.SinoInterface.Api.IRepository;
using ASHermed.SinoInterface.Api.IService;
using ASHermed.SinoInterface.ContractModel;
using ASHermed.SinoInterface.IFramework;
using ASHermed.SinoInterface.IFramework.SingleSignOn;
using ASHermed.SinoInterface.Tools;
using Newtonsoft.Json;
using ASHermed.SinoInterface.Tools.Base;
using PoJun.Util;
using PoJun.Util.Maps;
using System;
using System.Security.Cryptography;
using Microsoft.Extensions.Logging;

namespace ASHermed.SinoInterface.Api.Service
{
    /// <summary>
	/// 保险服务
	/// </summary>
	public class CignaService : BaseService, ICignaService
    {
        AppConfigurtaion _appConfigurtaion;
        IOutService _outService;
        ILogger<CignaService> _logger;
        private readonly RSACryptoServiceProvider sinoPublicProvider;
        private readonly RSACryptoServiceProvider sinoPrivateProvider;
        private readonly RSACryptoServiceProvider cignaPublicProvider;
        public CignaService(ILogger<CignaService> logger, AppConfigurtaion appConfigurtaion, IOutService outService)
        {
            _appConfigurtaion = appConfigurtaion;
            _outService = outService;
            _logger = logger;
            sinoPrivateProvider = CignaHelper.GetPrivateKey(_appConfigurtaion.GetSinoPrivateKey, "sino2556");//私钥
            sinoPublicProvider = CignaHelper.GetPublicKey(_appConfigurtaion.GetSinoPublicKey);//公钥
            cignaPublicProvider = CignaHelper.GetPublicKey(_appConfigurtaion.GetCignaPublicKey);//cigna公钥
        }

        public CignaResp SendCignaOrderUrl(string orderNo, string url = "www.baidu.com", string uId = "2626dc0533dd47e19c45324be642ece7")
        {
            var api = "sino/orderDetailNotify";
            try
            {
                ContentModel model = new ContentModel();
                model.orderDetailUrl = url;
                model.uid = uId;
                model.orderNo = orderNo;
                var bizcontent = CignaHelper.Encrypt(model.ToJson(), cignaPublicProvider);

                RequestDTO dto = new RequestDTO();
                dto.systemSource = "sino";
                dto.bizcontent = bizcontent;
                dto.timestamp = DateTime.Now.ToString("yyyyMMddHHmmss");

                string stringTmp = "bizcontent=" + bizcontent + "&systemSource=sino&timestamp=" + dto.timestamp;
                string sign = CignaHelper.Sign(stringTmp, sinoPrivateProvider);
                dto.sign = sign;
                _logger.LogInformation("SendCignaOrderUrl:" + url + "  parms:" + dto.ToJson());
                var res = _outService.HttpPost(_appConfigurtaion.GetCignaAddress + api, dto.ToJson());
                _logger.LogInformation("SendCignaOrderUrl返回:"+ res);
                var resp = JsonConvert.DeserializeObject<CignaResp>(res);

                return resp;
            }
            catch (System.Exception ee)
            {
                return new CignaResp() { code = -1, msg = ee.Message };
            }
        }



    }
}
