package com.jeequan.jeepay.thirdparty.channel.lklspay;

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
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.thirdparty.channel.AbstractPaymentService;
import com.jeequan.jeepay.thirdparty.channel.lklspay.model.LklPayMethod;
import com.jeequan.jeepay.thirdparty.util.PaywayUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * 拉卡拉支付
 * @author crystal
 */
@Slf4j
@Service
public class LklspayPaymentService extends AbstractPaymentService {

    /**
     * 拉卡拉花呗分期参数支持选项
     */
    public static final int[] ALI_HB_FQ_NUM_ARR = {3,6,12};

    @Override
    public String getIfCode() {
        return CS.IF_CODE.LKLSPAY;
    }

    @Override
    public boolean isSupport(String wayCode) {
        return false;
    }

    @Override
    public String preCheck(UnifiedOrderRQ rq, PayOrder payOrder) {
        return PaywayUtil.getRealPaywayService(this, payOrder.getWayCode()).preCheck(rq, payOrder);
    }

    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrder payOrder, MchAppConfigContext mchAppConfigContext) throws Exception {
        return PaywayUtil.getRealPaywayService(this, payOrder.getWayCode()).pay(rq, payOrder, mchAppConfigContext);
    }

    /**
     * 统一返回参数解析
     * @param bizData
     * @param channelRetMsg
     */
    public JSONObject initResult(JSONObject bizData, ChannelRetMsg channelRetMsg) {
        channelRetMsg.setNeedQuery(true);
        channelRetMsg.setChannelBizData(bizData);
        if(LklPayMethod.NeedQuery.NO.getStatus().equals(bizData.getString("need_query"))){
            channelRetMsg.setNeedQuery(false);
            channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_SUCCESS);
        }else{
            channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.WAITING);
        }
        channelRetMsg.setChannelOrderId(bizData.getString("log_no"));
        channelRetMsg.setPlatformOrderNo(bizData.getString("acc_trade_no"));
        channelRetMsg.setPlatformMchOrderNo(bizData.getString("trade_no"));
        JSONObject acc_resp_fields = bizData.getJSONObject("acc_resp_fields");
        if(acc_resp_fields != null){
            String userId = StringUtils.isNotEmpty(acc_resp_fields.getString("user_id")) ? acc_resp_fields.getString("user_id") : acc_resp_fields.getString("open_id");
            channelRetMsg.setChannelUserId(userId);
            ChannelJsapiMsg jsapiMsg = new ChannelJsapiMsg(acc_resp_fields.getString("prepay_id"));
            jsapiMsg.setAppId(acc_resp_fields.getString("app_id"));
            jsapiMsg.setPaySign(acc_resp_fields.getString("pay_sign"));
            jsapiMsg.setTimeStamp(acc_resp_fields.getString("time_stamp"));
            jsapiMsg.setNonceStr(acc_resp_fields.getString("nonce_str"));
            jsapiMsg.setPayPackage(acc_resp_fields.getString("package"));
            jsapiMsg.setSignType(acc_resp_fields.getString("sign_type"));
            jsapiMsg.setRedirectUrl(acc_resp_fields.getString("redirect_url"));
            channelRetMsg.setJsapiMsg(jsapiMsg);
        }
        return acc_resp_fields;
    }

    public void preCheckAli(UnifiedOrderRQ rq) {
        Integer hbFqNum = rq.getHbFqNum();
        if(hbFqNum != null){
            boolean isCheck = Arrays.stream(ALI_HB_FQ_NUM_ARR).anyMatch(num -> num == hbFqNum);
            if(!isCheck){
                throw new BizException("支付宝支付花呗分期参数有误，仅可支持数值为"+StringUtils.join(ALI_HB_FQ_NUM_ARR,",") +"期");
            }
        }
    }
}
