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

namespace Aop.Api.Business
{
    public class OrderQuery
    {
        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)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("{\"out_trade_no\":\"" + order.BillNumber + "\"}");

            var payRequst = new AlipayTradeQueryRequest {BizContent = sb.ToString()};
            AlipayTradeQueryResponse queryResponse = await Clients((AliPayConfig)order.Config).Execute(payRequst);

            if (string.Compare(queryResponse.Code, ResultCode.SUCCESS, false) == 0)
            {
                if (queryResponse.TradeStatus == "TRADE_FINISHED"
                    || queryResponse.TradeStatus == "TRADE_SUCCESS")
                {
                    order.TradeStatus = TradeStatus.SUCCESS;
                }
                else if (queryResponse.TradeStatus == "TRADE_CLOSED")
                {
                    order.TradeStatus = TradeStatus.CLOSED;
                }
            }
            return queryResponse;
        }
    }
}
