package com.jeequan.jeepay.pay.service.openapi.impl;

import cn.hutool.core.util.URLUtil;
import com.alibaba.fastjson.JSON;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.exception.AccessMsgException;
import com.jeequan.jeepay.core.interfaces.paychannel.IPaymentService;
import com.jeequan.jeepay.core.model.QRCodeParams;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.oauth2.WxpayOauth2Params;
import com.jeequan.jeepay.core.model.openapi.AccessError;
import com.jeequan.jeepay.core.model.openapi.AccessReq;
import com.jeequan.jeepay.core.model.openapi.AccessResp;
import com.jeequan.jeepay.core.model.openapi.biz.PayResultResp;
import com.jeequan.jeepay.core.model.openapi.biz.app.AppBizResp;
import com.jeequan.jeepay.core.model.openapi.biz.h5.H5BizReq;
import com.jeequan.jeepay.core.model.openapi.biz.h5.H5BizResp;
import com.jeequan.jeepay.core.model.openapi.biz.micro.MicroBizReq;
import com.jeequan.jeepay.core.model.rqrs.payorder.UnifiedOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.UnifiedOrderRS;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.AliAppOrderRS;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.AliWapOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.AliWapOrderRS;
import com.jeequan.jeepay.core.utils.JeepayKit;
import com.jeequan.jeepay.db.entity.PayOrder;
import com.jeequan.jeepay.pay.model.OpenApiExt;
import com.jeequan.jeepay.pay.service.openapi.AbstractPayOpenApiService;
import com.jeequan.jeepay.service.impl.PayOrderService;
import com.jeequan.jeepay.service.impl.SysConfigService;
import com.jeequan.jeepay.thirdparty.service.ConfigContextQueryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * TODO
 * jsapi支付逻辑
 * @author crystal
 * @date 2024/4/8 11:49
 */
@Service("h5OpenApiService")
public class H5OpenApiService extends AbstractPayOpenApiService {

    @Autowired
    private PayOrderService payOrderService;

    /**
     * jsapi支付
     * @param request
     * @return
     * @throws AccessMsgException
     */
    @Override
    public AccessResp execute(AccessReq request) throws AccessMsgException {
        H5BizReq bizReq = JSON.parseObject(request.getBizData(), H5BizReq.class);
        UnifiedOrderRQ req = bizReq.preCheck().buildBizRQ().setIsAppletCheck(true);
        OpenApiExt apiExt = checkMchInfo(req, request.getAppInfo());
        checkPay(apiExt.getConfigContext(),req, request.getAppInfo());
        //获取支付接口
        IPaymentService paymentService = getPayRoute(apiExt.getConfigContext());
        PayOrder payOrder = genPayOrder(req, apiExt.getConfigContext(),apiExt.getRouteManage());
        //校验参数
        paymentService.preCheck(req,payOrder);
        payOrderService.saveOrUpdate(payOrder);
        String payUrl = null;
        try {
            if(req.getIsReqChannel()){
                UnifiedOrderRS rs = (UnifiedOrderRS)paymentService.pay(req, payOrder, apiExt.getConfigContext());
                this.processChannelMsg(rs,rs.getChannelRetMsg(),payOrder);
                payUrl = rs.getPayData();
            }else{
                payUrl = getH5payUrl(payOrder);
            }
            H5BizResp resp = new H5BizResp(payOrder,payUrl);
            return AccessResp.success(request,resp);
        } catch (Exception e) {
            throw new AccessMsgException(AccessError.commonError(e.getMessage()));
        }
    }
}
