using Hncore.Infrastructure.Common;
using Hncore.Infrastructure.Extension;
using Hncore.Infrastructure.Serializer;
using Hncore.Infrastructure.WebApi;
using Hncore.Pass.PaymentCenter.Domain;
using Hncore.Pass.PaymentCenter.Pay.WxPay;
using Hncore.Pass.PaymentCenter.Request.WechatJsPay;
using Hncore.Pass.PaymentCenter.Service;
using Hncore.Pass.PaymentCenter.WxPay.WechatJsPay;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System.Threading.Tasks;

namespace Hncore.Pass.PaymentCenter.Controllers
{
    /// <summary>
    /// 微信小程序、公众号支付
    /// </summary>
    [ApiVersion("1.0")]
    [Route("api/paymentcenter/v{version:apiVersion}/WechatJsPay/[action]")]
    public class WechatJsPayController : PaymentControllerBase
    {
        private PaymentRecordService m_PaymentRecordService;    
        private WxPayClient m_WxPayClient;

        public WechatJsPayController(PaymentRecordService paymentRecordService
            , WxPayClient _WxPayClient, TenantService _TenantService, IConfiguration _Configuration):base(_TenantService, _Configuration)
        {
            m_PaymentRecordService = paymentRecordService;        
            m_WxPayClient = _WxPayClient;
        }
        [HttpPost, InternalApiAuth]
        public async Task<ApiResult> CreateOrder([FromBody] CreateOrderRequest request)
        {           

            LogHelper.Trace($"微信支付，收到支付请求", request.ToJson(true));
            request.AppId = "wx18683d61ce7da361";
            //增加支付记录
            var entity = request.MapTo<PaymentRecord>();
            entity.Openid = request.UserOpenId;           
              PaymentRecord paymentRecord = await m_PaymentRecordService.CreatePaymentRecord(entity);
            paymentRecord.PaymentChannel = PaymentChannel.WxPay;
            //todo
            var mchInfo = await GetMchInfoAsync(paymentRecord.TenantId
                , paymentRecord.StoreId
                , paymentRecord.PaymentChannel);

            //发起支付
            //todo
            //WxJsPayCreateOrderRequest payRequest = new WxJsPayCreateOrderRequest()
            //{
            //    AppId = request.AppId,
            //    Body = request.Body,
            //    MchId = mchInfo.MchId,
            //    NotifyUrl = GetNotifyUrl(),
            //    OutTradeNo = request.OrderId,
            //    StoreId = request.StoreId,
            //    TenantId = request.TenantId,
            //    TotalFee = request.TotalFee,
            //    UserOpenId = request.UserOpenId
            //};
            //var createOrderRes = await m_WxPayClient.JsPayCreateOrderAsync(payRequest, mchInfo);


            var payRequest = new WxScanPayCreateOrderRequest()
            {
                AppId = request.AppId,
                Body = request.Body,
                MchId = mchInfo.MchId,
                NotifyUrl = GetNotifyUrl(),
                OutTradeNo = request.OrderId,
                StoreId = request.StoreId,
                TenantId = request.TenantId,
                TotalFee = request.TotalFee,
                ProductId ="1"
            };

            var createOrderRes = await m_WxPayClient.ScanPayCreateOrderAsync(payRequest, mchInfo);
            

            LogHelper.Trace("PayCenter.CreateOrder", createOrderRes);
            return Success(createOrderRes);
        }
    }
}