package com.jeequan.jeepay.thirdparty.channel.lklspay.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.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.rqrs.AbstractRS;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelJsapiMsg;
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.lklspay.LklspayKit;
import com.jeequan.jeepay.thirdparty.channel.lklspay.LklspayPaymentService;
import com.jeequan.jeepay.thirdparty.channel.lklspay.model.LklPayMethod;
import com.jeequan.jeepay.thirdparty.util.ApiResBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 银联jsapi
 * @author crystal
 */
@Service("lklspayPaymentByYsfJsapiService")
public class YsfJsapi extends LklspayPaymentService {

    @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;
        JSONObject bizContent = new JSONObject();
        LklspayKit.setPayParams(mchAppConfigContext,bizContent,payOrder.getClientIp());
        bizContent.put("out_trade_no",payOrder.getPayOrderId());
        bizContent.put("subject",payOrder.getSubject());
        bizContent.put("remark",payOrder.getBuyerRemark());
        bizContent.put("account_type", "UQRCODEPAY");
        bizContent.put("trans_type","51");
        bizContent.put("total_amount",payOrder.getFindAmt());
        bizContent.put("notify_url",getNotifyUrl());
        bizContent.put("settle_type","0");

        JSONObject acc_busi_fields = new JSONObject();
        String expiredTime = DateUtil.formatBetween(DateUtil.between(payOrder.getCreatedAt(), payOrder.getExpiredTime(), DateUnit.MINUTE)).replace("毫秒", "");
        acc_busi_fields.put("timeout_express",expiredTime);
        acc_busi_fields.put("user_id",bizRQ.getUserId());
        acc_busi_fields.put("front_url",bizRQ.getFrontUrl());
        acc_busi_fields.put("front_fail_url",bizRQ.getFrontFailUrl());
        bizContent.put("acc_busi_fields",acc_busi_fields);
        JSONObject response = LklspayKit.payRequest(LklPayMethod.Api.TRANS_PREORDER, mchAppConfigContext.getMchApplyment(), bizContent);
        JSONObject payInfo = this.initResult(response, channelRetMsg);
        if(payInfo != null){
            res.setRedirectUrl(payInfo.getString("redirect_url"));
            res.setPayData(JSON.toJSONString(channelRetMsg.getJsapiMsg()));
        }
        res.setChannelRetMsg(channelRetMsg);
        return res;
    }

}
