package com.jeequan.jeepay.thirdparty.channel.ryxpay.payway;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.entity.PayOrder;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.exception.ChannelException;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.params.ruipay.RyxpayIsvParams;
import com.jeequan.jeepay.core.model.rqrs.AbstractRS;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRetMsg;
import com.jeequan.jeepay.core.model.rqrs.payorder.UnifiedOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.YsfJsapiOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.YsfJsapiOrderRS;
import com.jeequan.jeepay.thirdparty.channel.ryxpay.RyxpayKit;
import com.jeequan.jeepay.thirdparty.channel.ryxpay.RyxpayPaymentService;
import com.jeequan.jeepay.thirdparty.channel.ryxpay.model.PayRespEntity;
import com.jeequan.jeepay.thirdparty.channel.ryxpay.model.ReqMethod;
import com.jeequan.jeepay.thirdparty.util.ApiResBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 云闪付jsapi
 *
 */
@Service("ryxpayPaymentByYsfJsapiService")
public class YsfJsapi extends RyxpayPaymentService {

    @Override
    public String preCheck(UnifiedOrderRQ rq, PayOrder payOrder) {
        YsfJsapiOrderRQ bizRQ = (YsfJsapiOrderRQ) rq;
        if(StringUtils.isEmpty(bizRQ.getUserId())){
            throw new BizException("[userId]不可为空");
        }
        return null;
    }

    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrder payOrder, MchAppConfigContext mchAppConfigContext) throws Exception {
        YsfJsapiOrderRS res = ApiResBuilder.buildSuccess(YsfJsapiOrderRS.class);
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        res.setChannelRetMsg(channelRetMsg);
        YsfJsapiOrderRQ bizRQ = (YsfJsapiOrderRQ) rq;
        try {
            JSONObject bizContent = new JSONObject();
            // 默认支付参数
            RyxpayKit.setPayParams(mchAppConfigContext, bizContent, payOrder, rq);
            //加载商户的地区信息 瑞银信必要
            loadMchExtInfo(bizContent,mchAppConfigContext);
            long between = DateUtil.between(payOrder.getCreatedAt(), payOrder.getExpiredTime(), DateUnit.MINUTE);
            bizContent.put("timeExpire", between);
            //持卡人ip
            bizContent.put("customerIp","127.0.0.1");
            bizContent.put("userId", bizRQ.getUserId());
            if(StringUtils.isNotEmpty(bizRQ.getFrontUrl())){
                bizContent.put("frontUrl",bizRQ.getFrontUrl());
            }
            if(StringUtils.isNotEmpty(bizRQ.getFrontFailUrl())){
                bizContent.put("frontFailUrl",bizRQ.getFrontFailUrl());
            }
            RyxpayIsvParams isvParams = (RyxpayIsvParams) configContextQueryService.queryIsvParams(mchAppConfigContext.getMchApplyment().getIsvNo(), getIfCode());
            PayRespEntity response = RyxpayKit.payReqApi(ReqMethod.ServiceCode.SMZF017, isvParams, bizContent, payOrder.getPayOrderId(), getNotifyUrl());
            initCommonResult(response,channelRetMsg,payOrder.getWayCodeType());
            if(channelRetMsg.getJsapiMsg() != null){
                res.setRedirectUrl(channelRetMsg.getJsapiMsg().getRedirectUrl());
                res.setPayData(JSON.toJSONString(channelRetMsg.getJsapiMsg()));
            }
            return res;
        }catch (Exception e) {
            throw ChannelException.sysError(e.getMessage());
        }
    }
}
