using Aop.Api.Request;
using Aop.Api.Response;
using BaseModel.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WayneCloud.PaymentProcessors.Alipay.FromSDK;

namespace Aop.Api.Business
{
    public class Refund
    {
        private static volatile IAopClient client;

        private static volatile Dictionary<AliPayConfig, IAopClient> clients = new Dictionary<AliPayConfig, IAopClient>();
        public static IAopClient Clients(AliPayConfig config)
        {
            if (config != null)
            {
                if (client == null)
                {
                    client = new DefaultAopClient(config.serverUrl,
                                                         config.appId,
                                                         config.merchant_private_key,
                                                         "json",
                                                         config.version,
                                                         config.sign_type,
                                                         config.alipay_public_key,
                                                         config.charset);
                }
                return client;
            }
            if (clients.ContainsKey(config) != null)
            {
                var newClient = new DefaultAopClient(config.serverUrl,
                                                                 config.appId,
                                                                 config.merchant_private_key,
                                                                 "json",
                                                                 config.version,
                                                                 config.sign_type,
                                                                 config.alipay_public_key,
                                                                 config.charset);
                clients.Add(config, newClient);
                return newClient;
            }
            else
            {
                return clients[config];
            }
        }

        public static async Task<AopResponse> Run(ElectronicOrderModel order)
        {
            //set the default trade_status to PAYERROR
            order.TradeStatus = TradeStatus.PAYERROR;

            string out_trade_no = order.BillNumber;
            string refund_amount = order.NetAmount.ToString();
            string out_request_no = out_trade_no + DateTime.Now.ToString();

            StringBuilder sb = new StringBuilder();
            sb.Append("{\"out_trade_no\":\"" + out_trade_no + "\",");
            sb.Append("\"refund_amount\":\"" + refund_amount + "\",");
            sb.Append("\"out_request_no\":\"" + out_request_no + "\",");
            sb.Append("\"operator_id\":\"" + (order.OperatorId ?? "") + "\",\"store_id\":\"" + order.SiteId + "\",\"terminal_id\":\"" + (order.TerminalId ?? "") + "\"}");
            
            var refundResponse = await RequestRefund(sb.ToString(), (AliPayConfig)order.Config);

            //支付成功的情形
            if (refundResponse != null && refundResponse.Code == ResultCode.SUCCESS)
            {
                order.TradeStatus = TradeStatus.SUCCESS;
            }
            
            return refundResponse;
        }

        private static async Task<AlipayTradeRefundResponse> RequestRefund(string biz_content, AliPayConfig config)
        {
            AlipayTradeRefundRequest refundRequst = new AlipayTradeRefundRequest();
            refundRequst.BizContent = biz_content;

            AlipayTradeRefundResponse refundResponse = await Clients(config).Execute(refundRequst);
            return refundResponse;
        }
    }
}
