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

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.interfaces.device.ISpeakerService;
import com.jeequan.jeepay.core.model.device.PayOrderInfo4Device;
import com.jeequan.jeepay.core.model.device.PsSpeakerParams;
import com.jeequan.jeepay.core.utils.AmountUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/*
* 云喇叭厂商： 品生
*
* @author zx
* 
* @date 2021/6/8 18:10
*/
@Slf4j
@Service
public class PsSpeakerService implements ISpeakerService {

    @Override
    public void send(JSONObject deviceParams, PayOrderInfo4Device payOrderInfo) throws BizException {

        /*PsSpeakerParams psSpeakerParams = getPsSpeakerParams(deviceParams);

        speak(psSpeakerParams, payOrderInfo);*/

        sendCustomMsg(deviceParams, payOrderInfo);
    }

    @Override
    public void sendCustomMsg(JSONObject deviceParams, PayOrderInfo4Device payOrderInfo) throws BizException {

        PsSpeakerParams psSpeakerParams = getPsSpeakerParams(deviceParams);

        JSONObject reqParams = new JSONObject();
        reqParams.put("devid", psSpeakerParams.getDeviceParams().getDeviceNo()); // 设备ID
        reqParams.put("reqid", IdUtil.fastSimpleUUID()); // 请求ID
        reqParams.put("time", DateUtil.format(payOrderInfo.getCreatedAt(), DatePattern.PURE_DATETIME_PATTERN));

        if (PayOrderInfo4Device.BOARD_TYPE_CANCEL.equals(payOrderInfo.getWayCodeType())) {
            reqParams.put("message", "客户取消付款" + AmountUtil.convertCent2Dollar(payOrderInfo.getAmount()) + "元");
        }else if (PayOrderInfo4Device.BOARD_TYPE_MEMBER.equals(payOrderInfo.getWayCodeType())) {
            reqParams.put("message", "会员余额支付" + AmountUtil.convertCent2Dollar(payOrderInfo.getAmount()) + "元");
        }else {
            // 未配置 自定义播报内容，走默认播报语音
            if (StringUtils.isBlank(psSpeakerParams.getProviderParams().getCustomContent())) {
                speak(psSpeakerParams, payOrderInfo);
            }else {
                reqParams.put("message", psSpeakerParams.getProviderParams().getCustomContent() + AmountUtil.convertCent2Dollar(payOrderInfo.getAmount())+ "元");
            }
        }

        PsUtil.post("/message/sendMsg", reqParams, PsUtil.getToken(psSpeakerParams));
    }

    /**
     * 发起请求
     */
    private JSONObject speak(PsSpeakerParams psSpeakerParams, PayOrderInfo4Device payOrderInfo) throws BizException {

        JSONObject reqParams = new JSONObject();
        reqParams.put("devid", psSpeakerParams.getDeviceParams().getDeviceNo()); // 设备ID
        reqParams.put("reqid", IdUtil.fastSimpleUUID()); // 请求ID
        reqParams.put("time", DateUtil.format(payOrderInfo.getCreatedAt(), DatePattern.PURE_DATETIME_PATTERN));
        reqParams.put("payment", payOrderInfo.getAmount());  // 金额，分
        reqParams.put("type", getPayTypeCode(payOrderInfo.getWayCodeType()));  //1-收款成功   2-支付宝收款成功  3-微信收款成功

        return PsUtil.post("/message/sendMsg", reqParams, PsUtil.getToken(psSpeakerParams));
    }

    private PsSpeakerParams getPsSpeakerParams(JSONObject deviceParams) {
        JSONObject paramsJSON = new JSONObject();
        paramsJSON.put("providerParams", JSON.parseObject(deviceParams.getString("providerParams")));
        paramsJSON.put("deviceParams", JSON.parseObject(deviceParams.getString("deviceParams")));

        // 飞鹅云打印参数
        return JSON.parseObject(paramsJSON.toJSONString(), PsSpeakerParams.class);
    }


    /**
     * 支付方式
     */
    private int getPayTypeCode(String wayCodeType) {
        if (CS.PAY_WAY_CODE_TYPE.ALIPAY.equals(wayCodeType)) { // 支付宝
            return 2;
        }else if (CS.PAY_WAY_CODE_TYPE.WECHAT.equals(wayCodeType)) { // 微信
            return 3;
        }else if (CS.PAY_WAY_CODE_TYPE.UNIONPAY.equals(wayCodeType)) { // 银联
            return 5;
        }else {
            return 1;
        }
    }

}
