package com.jeequan.jeepay.thirdparty.device.speaker.lklsSpeaker;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.device.BsjParams;
import com.jeequan.jeepay.core.model.device.LklsParams;
import com.jeequan.jeepay.core.model.device.PayOrderInfo4Device;
import com.jeequan.jeepay.core.utils.AmountUtil;
import com.jeequan.jeepay.core.utils.DateKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;

/**
 * TODO
 * 拉卡拉音箱发起请求
 * @author crystal
 * @date 2023/12/28 17:41
 */
@Slf4j
public class LklsUtil {
    /**
     * 发起请求
     * @param reqUrl
     * @param lklsParams
     * @param bizData
     * @return
     */
    public static String req(String reqUrl, LklsParams lklsParams, JSONObject bizData) {
        log.info("【拉卡拉音箱】请求业务参数：{}",bizData);
        JSONObject request = packageCommonParams(lklsParams, bizData);
        log.info("【拉卡拉音箱】请求地址：{}，请求参数：{}",reqUrl,request);
        String response = HttpUtil.post(reqUrl, request.toString());
        log.info("【拉卡拉音箱】返回参数：{}",response);
        if (StringUtils.isBlank(response)) {
            throw new BizException("【拉卡拉音箱】播报接口请求失败");
        }
        JSONObject respData = JSONObject.parseObject(response);
        if(respData.getInteger("status") == null){
            return response;
        }
        if (respData.getIntValue("status") != HttpStatus.OK.value()) {
            throw new BizException(respData.getString("message"));
        }
        if(StringUtils.isEmpty(respData.getString("data"))){
            return null;
        }
        String data = Base64.decodeStr(respData.getString("data"));
        log.info("【拉卡拉音箱】返回参数,解密结果:{}",data);
        return data;
    }

    private static JSONObject packageCommonParams(LklsParams lklsParams, JSONObject bizData) {
        JSONObject reqParams = new JSONObject();
        reqParams.put("appId", lklsParams.getProviderParams().getAppId());
        reqParams.put("signType", "md5");
        String base64BizData = Base64.encode(bizData.toString());
        reqParams.put("reqData",base64BizData);
        reqParams.put("sign", SecureUtil.md5( base64BizData + lklsParams.getProviderParams().getAppSecret()));
        return reqParams;
    }

    public static JSONObject getSpeakData(LklsParams lklsParams, PayOrderInfo4Device payOrderInfo) {
        JSONObject bizData = new JSONObject();
        bizData.put("sn",lklsParams.getDeviceParams().getDeviceNo());
        bizData.put("messageId",payOrderInfo.getPayOrderId());
        bizData.put("reqTimeStamp",System.currentTimeMillis());
        // 防逃单播报走自定义模式
        if (PayOrderInfo4Device.BOARD_TYPE_CANCEL.equals(payOrderInfo.getWayCodeType())) {
            bizData.put("voiceMsg", "客户取消付款" + AmountUtil.convertCent2Dollar(payOrderInfo.getAmount()) + "元");
            return bizData;
        }
        // 会员余额支付
        if (PayOrderInfo4Device.BOARD_TYPE_MEMBER.equals(payOrderInfo.getWayCodeType())) {
            bizData.put("voiceMsg", "会员余额支付" + AmountUtil.convertCent2Dollar(payOrderInfo.getAmount()) + "元");
            return bizData;
        }
        // 配置了自定义播报内容
        if (StringUtils.isNotBlank(lklsParams.getProviderParams().getCustomContent())) {
            bizData.put("voiceMsg", lklsParams.getProviderParams().getCustomContent() + AmountUtil.convertCent2Dollar(payOrderInfo.getAmount())+ "元");
        }else {
            bizData.put("voiceMsg", "收款成功" + AmountUtil.convertCent2Dollar(payOrderInfo.getAmount())+ "元");
        }
        return bizData;
    }
}
