using Hncore.Infrastructure.Service;
using Hncore.Pass.PaymentCenter.Domain;
using Hncore.Pass.PaymentCenter.Domain.Refund;
using Hncore.Pass.PaymentCenter.Model;
using Hncore.Pass.PaymentCenter.Response;
using Hncore.Pass.PaymentCenter.WeiFuTong;
using Hncore.Pass.PaymentCenter.WeiFuTong.ClientExtension;
using Hncore.Pass.PaymentCenter.WeiFuTong.QueryOrder;
using Hncore.Pass.PaymentCenter.WeiFuTong.Refund;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Hncore.Pass.PaymentCenter.Service
{
    public class WeiFuTongService:IFindService
    {
        private WeiFuTongClient _weiFuTongClient;

        public WeiFuTongService(WeiFuTongClient weiFuTongClient)
        {
            _weiFuTongClient = weiFuTongClient;
        }

        /// <summary>
        /// 订单查询
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<QueryOrderResponse> QueryOrderAsync(PaymentRecord paymentRecord
            , MchInfo mchInfo)
        {
            string message = "";

            var res = await _weiFuTongClient.QueryOrderAsync(new WeiFuTongQueryOrderRequest()
            {
                MchInfo = mchInfo,
                OutTradeNo = paymentRecord.Id.ToString(),
                TransactionId = paymentRecord.TransactionId,
                PaymentChannel = paymentRecord.PaymentChannel
            });

            if (res.IsPaySuccess())
            {
                message = "支付成功";
            }
            else if (res.IsPayFailed())
            {
                message = "支付失败";
            }
            else
            {
                message = "支付中";
            }

            if (res.IsPaySuccess() && (paymentRecord.PaymentStatus != PaymentStatus.OkPay ||
                                       paymentRecord.PaySuccessTime == null))
            {
                paymentRecord.SetPaySuccessed(res.TransactionId, res.GetPaySuccessTime());
            }

            if (res.IsPayFailed() && paymentRecord.PaymentStatus != PaymentStatus.Fail)
            {
                paymentRecord.SetPayFailed();
            }

            if ((paymentRecord.PaymentType == PaymentType.None || paymentRecord.PaymentMethod == PaymentMethod.None)
                && res.IsPaySuccess())
            {
                paymentRecord.SetPaymentTypeFromTradeType(res.TradeType);
            }

            return new QueryOrderResponse()
            {
                PaymentStatus = paymentRecord.PaymentStatus,
                PaymentMessage = message,
                PayType = paymentRecord.PaymentType
            };
        }

        /// <summary>
        /// 退款查询
        /// </summary>
        /// <param name="refundRecord"></param>
        /// <param name="mchInfo"></param>
        /// <returns></returns>
        public async Task<QueryRefundResponse> QueryRefundAsync(RefundRecord refundRecord, MchInfo mchInfo)
        {
            string message = "";

            var res = await _weiFuTongClient.QueryRefundAsync(new WeiFuTongRefundQueryRequest()
            {
                MchInfo = mchInfo,
                OutTradeNo = refundRecord.PaymentRecordId.ToString(), //商户订单号
                TransactionId = refundRecord.TransactionId, //平台订单号
                RefundId = refundRecord.TransactionRefundId, //平台退款单号
                OutRefundNo = refundRecord.RefundId, //商户退款单号
                PaymentChannel = refundRecord.PaymentChannel
            });

            if (res.IsRefundSuccess())
            {
                message = "退款成功";
            }
            else if (res.IsRefundFail() || res.IsNotExists())
            {
                message = "退款失败";
            }
            else if (res.IsCancel())
            {
                message = "已撤销";
            }
            else
            {
                message = "退款中";
            }

            if (res.IsRefundSuccess() && (refundRecord.RefundStatus != RefundStatus.Success
                                          || refundRecord.RefundSuccessTime == null))
            {
                refundRecord.SetRefundSuccessed(res.RefundQueryItems.First().RefundId, DateTime.Now);
            }

            if (res.IsRefundSuccess() && refundRecord.CancelStatus == CancelStatus.Canceling)
            {
                refundRecord.SetCancelFailed();
            }

            if ((res.IsRefundFail() || res.IsNotExists()) && refundRecord.RefundStatus != RefundStatus.Fail)
            {
                refundRecord.SetRefundFailed();
            }

            if (res.IsCancel())
            {
                if (refundRecord.CancelStatus == CancelStatus.Canceling)
                {
                    refundRecord.SetCancelSuccessed();
                }
                else
                {
                    refundRecord.SetRefundFailed();
                }
            }

            return new QueryRefundResponse()
            {
                CancelStatus = refundRecord.CancelStatus,
                PayType = refundRecord.PaymentType,
                RefundMessage = message,
                RefundStatus = refundRecord.RefundStatus,
                RefundId = refundRecord.RefundId,
                OperatorName = refundRecord.OperatorName,              
                RefundReason = refundRecord.RefundReason
            };
        }
    }
}