using Com.Bocom.OpenApi;
using Com.Bocom.OpenApi.Utils;
using Kivii;
using Kivii.Finances.Entities;
using Kivii.Linq;
using Kivii.Text;
using Kivii.Web;
using Org.BouncyCastle.Asn1.Ocsp;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using static Com.Bocom.OpenApi.AddContractOrderRequestV2.AddContractOrderRequestV2Biz;

namespace Njzj.Bocoms.Openapis.Transforms
{
    
    public class AddContractOrderResponseV2Test : RestfulExecution<AddContractOrderResponseV2>
    {
        public string BizId { get; set; }
        public override object OnExecution(IRequest req, IResponse res)
        {
            DefaultBocomClient client = new DefaultBocomClient(Configs.appIdZhjz, Configs.priKeyZhjz, Configs.pubKeyZhjz, BocomConstants.ENCRYPT_TYPE_RSA_AND_AES);
            /**
   * 测试环境可以忽略SSL证书告警，生产环境不可忽略
   */
            client.IgnoreSslHostnameVerifier();

            var request = new AddContractOrderRequestV2();
            request.SetServiceUrl(Configs.basicUrlZhjz + Configs.AddContractOrderRequestV2);

            var bizId = Guid.NewGuid().ToString();
            var bizContent = new AddContractOrderRequestV2.AddContractOrderRequestV2Biz();
            bizContent.org_id = Configs.ORG_ID;
            bizContent.bat_id = BizId;
            bizContent.cont_order_list = new List<ContOrder>();
            var contorder = new ContOrder();
            contorder.cont_id = bizId;
            contorder.org_id = bizContent.org_id;
            contorder.ori_payer = "业务测试公司";
            contorder.ori_ac = "仅记录";
            contorder.ori_addr = "仅记录";
            contorder.ori_tel_no = "仅记录";
            contorder.ori_other_info1 = "仅记录";
            contorder.ori_other_info2 = "仅记录";
            contorder.ori_other_info3 = "仅记录";
            contorder.ori_itm_nme = "仅记录";
            contorder.ori_amt = "5210.48";
            contorder.ori_offset_amt = "210.48";
            contorder.ori_act_amt = "5000";
            contorder.attach_code = "";
            contorder.remark = "测试";
            contorder.offset_remark = "测试2";
            contorder.payment_period = $"{DateTime.Now.AddMonths(-2).ToString("yyyy-MM-dd")}~{DateTime.Now.AddMonths(2).ToString("yyyy-MM-dd")}";
            contorder.agreement_type = "-1";
            bizContent.cont_order_list.Add(contorder);

            request.SetBizContent(bizContent);
            var json = JsonSerializer.SerializeToString(request);
            var response = client.Execute(request, Guid.NewGuid().ToString().Replace("-", ""));

            var rtns = new RestfulExecutionResponse<AddContractOrderResponseV2>();
            rtns.Results = new List<AddContractOrderResponseV2>();
            if (response.IsSuccess())
            {
                var resp = response as AddContractOrderResponseV2;
                rtns.Results.Add(resp);
            }
            else
            {
                throw new Exception($"ReturnCode:{response.GetRspCode()}{Environment.NewLine}ReturnMsg:{response.GetRspMsg()}");
            }
            return rtns;
        }
    }

    public class DelContractOrderResponseV1Test : RestfulExecution<DelContractOrderResponseV1>
    {
        public string BillId { get; set; }
        public override object OnExecution(IRequest req, IResponse res)
        {
            DefaultBocomClient client = new DefaultBocomClient(Configs.appIdZhjz, Configs.priKeyZhjz, Configs.pubKeyZhjz, BocomConstants.ENCRYPT_TYPE_RSA_AND_AES);
            /**
   * 测试环境可以忽略SSL证书告警，生产环境不可忽略
   */
            client.IgnoreSslHostnameVerifier();
            var request = new DelContractOrderRequestV1();
            request.SetServiceUrl(Configs.basicUrlZhjz + Configs.DelContractOrderRequestV1);

            var bizContent = new DelContractOrderRequestV1.DelContractOrderRequestV1Biz();
            bizContent.org_id = Configs.ORG_ID;
            bizContent.bill_id = BillId;
            request.SetBizContent(bizContent);
            var response = client.Execute(request, Guid.NewGuid().ToString().Replace("-", ""));

            var rtns = new RestfulExecutionResponse<DelContractOrderResponseV1>();
            rtns.Results = new List<DelContractOrderResponseV1>();
            if (response.IsSuccess())
            {
                var resp = response as DelContractOrderResponseV1;
                rtns.Results.Add(resp);
            }
            else
            {
                throw new Exception($"ReturnCode:{response.GetRspCode()}{Environment.NewLine}ReturnMsg:{response.GetRspMsg()}");
            }
            return rtns;
        }
    }


    public class WriteOffResultNotifyCallBack2 : RestfulExecution<WriteOffResultNotify>
    {
        public override object OnExecution(IRequest req, IResponse res)
        {

            //Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------");
            //Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------");
            var rtns = new RestfulExecutionResponse<WriteOffResultNotify>();
            rtns.Results = new List<WriteOffResultNotify>();

            var conn = KiviiContext.GetOpenedDbConnection<WriteOffResultNotify>();
            conn.InitEntityType<EntityLog<WriteOffResultNotify>>();
            var log = conn.Single<EntityLog<WriteOffResultNotify>>(o => true);
            if (log == null) return rtns;

            Console.WriteLine("------------------------------------WriteOffResultNotifyCallBack-----------------------------------------");
            Console.WriteLine("------------------------------------Start-----------------------------------------");
            try
            {
                var result = ApiUtils.ParseCommunicationJsonWithBocomSign(log.Metadata, "utf-8", Configs.priKeyZhjz, Configs.pubKeyZhjz);
                ;
                result.BillId = result.biz_content.bill_id;
                result.BizKvid = Guid.Parse(result.biz_content.cont_id);
                conn.Insert(result);

                //var rspBizContent = "{\"rsp_head\":{\"response_msg\":\"非白名单客户\",\"response_code\":\"CIPP0004MT0017\"},\"rsp_body\":{\"context_no\":\"P21201026566300000019\",\"account_exist_flag\":\"EF00\"}";
                var resp = "{\"rsp_code\":\"0\",\"biz_state\":\"S\",\"rsp_msg\":\"核销处理成功\"}";
                var rsp = ApiUtils.SignRspBizContent(resp, result.encrypt_key, Configs.priKeyZhjz);
                rtns.Results.Add(result);
                return rsp;
            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                Console.WriteLine("------------------------------------WriteOffResultNotifyCallBack-----------------------------------------");
                Console.WriteLine("------------------------------------End-----------------------------------------");
                Console.WriteLine("-----------------------------------------------------------------------------------------------------");
                Console.WriteLine("-----------------------------------------------------------------------------------------------------");
            }

        }
    }


    public class MPNG210001ResponseV1Test : RestfulExecution<MPNG210001ResponseV1>
    {
        public string BizId { get; set; }
        public string TotalAmount { get; set; }

        public string Remark { get; set; }

        public override object OnExecution(IRequest req, IResponse res)
        {
            DefaultBocomClient client = new DefaultBocomClient(Configs.appIdQrCode, Configs.priKeyQrCode, Configs.pubKeyQrCode, BocomConstants.ENCRYPT_TYPE_RSA_AND_AES);
            /**
   * 测试环境可以忽略SSL证书告警，生产环境不可忽略
   */
            client.IgnoreSslHostnameVerifier();

            var request = new MPNG210001RequestV1();
            request.SetServiceUrl(Configs.basicUrlQrCode + Configs.MPNG210001RequestV1);
            var bizContent = new MPNG210001RequestV1.MPNG210001RequestV1Biz();
            bizContent.req_head = new MPNG210001RequestV1.MPNG210001RequestV1Biz.ReqHead();
            bizContent.req_head.trans_time = DateTime.Now.ToString("yyyyMMddHHmmss");
            bizContent.req_head.version = "V-1.0";
            bizContent.req_body = new MPNG210001RequestV1.MPNG210001RequestV1Biz.ReqBody();
            bizContent.req_body.tran_scene = "B2C-API-DISPLAYCODE";
            bizContent.req_body.mer_ptc_id = Configs.mer_ptc_id;//"301310063009501";//商户号,需要配置信息到位
            bizContent.req_body.mer_trade_time = DateTime.Now.ToString("HHmmss");
            bizContent.req_body.mer_trade_date = DateTime.Now.ToString("yyyyMMdd");
            //bizContent.req_body.fee_split_info = new MPNG210001RequestV1.MPNG210001RequestV1Biz.ReqBody.FeeSplitInfo();
            //bizContent.req_body.fee_split_info.partner_amount = "";
            //bizContent.req_body.fee_split_info.mer_amount = "";
            //bizContent.req_body.pay_mer_tran_no = "121809690390929234836";
            bizContent.req_body.pay_mer_tran_no = Guid.NewGuid().ToString();
            bizContent.req_body.total_amount = TotalAmount;
            bizContent.req_body.location = "ONLINE";
            bizContent.req_body.currency = "CNY";
            //bizContent.req_body.jump_url = "http://k5.kivii.org";
            bizContent.req_body.tran_content = BizId;
            bizContent.req_body.mer_memo = Remark;
            request.SetBizContent(bizContent);
            var response = client.Execute(request, Guid.NewGuid().ToString().Replace("-", ""));

            var rtns = new RestfulExecutionResponse<MPNG210001ResponseV1>();
            rtns.Results = new List<MPNG210001ResponseV1>();
            if (response.IsSuccess())
            {
                var resp = response as MPNG210001ResponseV1;
                rtns.Results.Add(resp);
            }
            else
            {
                throw new Exception($"ReturnCode:{response.GetRspCode()}{Environment.NewLine}ReturnMsg:{response.GetRspMsg()}");
            }
            return rtns;
        }
    }

    public class MPNG020702ResponseV1Test : RestfulExecution<MPNG020702ResponseV1>
    {
        public string PayMerTranNo { get; set; }
        public override object OnExecution(IRequest req, IResponse res)
        {
            DefaultBocomClient client = new DefaultBocomClient(Configs.appIdQrCode, Configs.priKeyQrCode, Configs.pubKeyQrCode, BocomConstants.ENCRYPT_TYPE_RSA_AND_AES);
            /**
   * 测试环境可以忽略SSL证书告警，生产环境不可忽略
   */
            client.IgnoreSslHostnameVerifier();

            var request = new MPNG020702RequestV1();
            request.SetServiceUrl(Configs.basicUrlQrCode + Configs.MPNG020702RequestV1);
            var bizContent = new MPNG020702RequestV1.MPNG020702RequestV1Biz();
            bizContent.req_head = new MPNG020702RequestV1.MPNG020702RequestV1Biz.ReqHead();
            bizContent.req_head.trans_time = DateTime.Now.ToString("yyyyMMddHHmmss");
            bizContent.req_head.version = "1.0";
            bizContent.req_body = new MPNG020702RequestV1.MPNG020702RequestV1Biz.ReqBody();
            bizContent.req_body.tran_scene = "B2C-API-DISPLAYCODE";
            bizContent.req_body.mer_ptc_id = Configs.mer_ptc_id;//"301310063009501";//商户号,需要配置信息到位
            bizContent.req_body.mer_trade_date = DateTime.Now.ToString("yyyyMMdd");
            bizContent.req_body.pay_mer_tran_no = PayMerTranNo;// Guid.NewGuid().ToString();

            bizContent.req_body.require_fields = new List<MPNG020702RequestV1.MPNG020702RequestV1Biz.ReqBody.RequireFields>();
            var require_field = new MPNG020702RequestV1.MPNG020702RequestV1Biz.ReqBody.RequireFields();
            require_field.require_field = "bank_tran_no,third_party,third_party_tran_no,payment_info,refund_info";
            bizContent.req_body.require_fields.Add(require_field);

            request.SetBizContent(bizContent);
            var response = client.Execute(request, Guid.NewGuid().ToString().Replace("-", ""));

            var rtns = new RestfulExecutionResponse<MPNG020702ResponseV1>();
            rtns.Results = new List<MPNG020702ResponseV1>();
            if (response.IsSuccess())
            {
                var resp = response as MPNG020702ResponseV1;
                rtns.Results.Add(resp);
            }
            else
            {
                throw new Exception($"ReturnCode:{response.GetRspCode()}{Environment.NewLine}ReturnMsg:{response.GetRspMsg()}");
            }
            return rtns;
        }
    }

    public class MPNG020705ResponseV1Test : RestfulExecution<MPNG020705ResponseV1>
    {
        public string PayMerTranNo { get; set; }
        public override object OnExecution(IRequest req, IResponse res)
        {
            DefaultBocomClient client = new DefaultBocomClient(Configs.appIdQrCode, Configs.priKeyQrCode, Configs.pubKeyQrCode, BocomConstants.ENCRYPT_TYPE_RSA_AND_AES);
            /**
   * 测试环境可以忽略SSL证书告警，生产环境不可忽略
   */
            client.IgnoreSslHostnameVerifier();

            var request = new MPNG020705RequestV1();
            request.SetServiceUrl(Configs.basicUrlQrCode + Configs.MPNG020705RequestV1);
            var bizContent = new MPNG020705RequestV1.MPNG020705RequestV1Biz();
            bizContent.req_head = new MPNG020705RequestV1.MPNG020705RequestV1Biz.ReqHead();
            bizContent.req_head.trans_time = DateTime.Now.ToString("yyyyMMddHHmmss");
            bizContent.req_head.version = "V-1.0";
            bizContent.req_body = new MPNG020705RequestV1.MPNG020705RequestV1Biz.ReqBody();
            bizContent.req_body.mer_ptc_id = Configs.mer_ptc_id;//"301310063009501";//商户号,需要配置信息到位
            bizContent.req_body.mer_trade_date = DateTime.Now.ToString("yyyyMMdd");
            //bizContent.req_body.pay_mer_tran_no = "121809690390929234836";
            //bizContent.req_body.pay_mer_tran_no = BizId;// Guid.NewGuid().ToString();
            bizContent.req_body.pay_mer_tran_no = PayMerTranNo;
            bizContent.req_body.close_mer_tran_no = PayMerTranNo;
            request.SetBizContent(bizContent);
            var response = client.Execute(request, Guid.NewGuid().ToString().Replace("-", ""));

            var rtns = new RestfulExecutionResponse<MPNG020705ResponseV1>();
            rtns.Results = new List<MPNG020705ResponseV1>();
            if (response.IsSuccess())
            {
                var resp = response as MPNG020705ResponseV1;
                rtns.Results.Add(resp);
            }
            else
            {
                throw new Exception($"ReturnCode:{response.GetRspCode()}{Environment.NewLine}ReturnMsg:{response.GetRspMsg()}");
            }
            return rtns;
        }
    }

    public class MPNG020706ResponseV1Test : RestfulExecution<MPNG020706ResponseV1>
    {
        //public string BizId { get; set; }
        public string PayMerTranNo { get; set; }

        public override object OnExecution(IRequest req, IResponse res)
        {
            DefaultBocomClient client = new DefaultBocomClient(Configs.appIdQrCode, Configs.priKeyQrCode, Configs.pubKeyQrCode, BocomConstants.ENCRYPT_TYPE_RSA_AND_AES);
            /**
   * 测试环境可以忽略SSL证书告警，生产环境不可忽略
   */
            client.IgnoreSslHostnameVerifier();

            var request = new MPNG020706RequestV1();
            request.SetServiceUrl(Configs.basicUrlQrCode + Configs.MPNG020706RequestV1);
            var bizContent = new MPNG020706RequestV1.MPNG020706RequestV1Biz();
            bizContent.req_head = new MPNG020706RequestV1.MPNG020706RequestV1Biz.ReqHead();
            bizContent.req_head.trans_time = DateTime.Now.ToString("yyyyMMddHHmmss");
            bizContent.req_head.version = "V-1.0";
            bizContent.req_body = new MPNG020706RequestV1.MPNG020706RequestV1Biz.ReqBody();
            bizContent.req_body.mer_ptc_id = Configs.mer_ptc_id;//"301310063009501";//商户号,需要配置信息到位
            bizContent.req_body.mer_trade_date = DateTime.Now.ToString("yyyyMMdd");
            //bizContent.req_body.pay_mer_tran_no = "121809690390929234836";
            //bizContent.req_body.pay_mer_tran_no = BizId;// Guid.NewGuid().ToString();
            bizContent.req_body.pay_mer_tran_no = PayMerTranNo;
            bizContent.req_body.cancel_mer_tran_no = PayMerTranNo;
            request.SetBizContent(bizContent);
            var response = client.Execute(request, Guid.NewGuid().ToString().Replace("-", ""));

            var rtns = new RestfulExecutionResponse<MPNG020706ResponseV1>();
            rtns.Results = new List<MPNG020706ResponseV1>();
            if (response.IsSuccess())
            {
                var resp = response as MPNG020706ResponseV1;
                rtns.Results.Add(resp);
            }
            else
            {
                throw new Exception($"ReturnCode:{response.GetRspCode()}{Environment.NewLine}ReturnMsg:{response.GetRspMsg()}");
            }
            return rtns;
        }
    }
}
