package com.jeequan.jeepay.thirdparty.device.printer.bsjPrinter;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.interfaces.device.IPrinterService;
import com.jeequan.jeepay.core.model.device.BsjParams;
import com.jeequan.jeepay.core.model.device.DeviceParams;
import com.jeequan.jeepay.core.model.device.PayOrderInfo4Device;
import com.jeequan.jeepay.thirdparty.device.speaker.bsjSpeaker.BsjUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/*
* 云打印厂商： 智谷物联
*
* @author zhuxiao
*
* @date 2021/6/8 18:10
*/
@Slf4j
@Service
public class BsjPrinterService implements IPrinterService {

    private static final String REQ_URL = "https://ioe.car900.com/v1/openApi/dev/customPrint.json";

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

        JSONObject resJSON = print(deviceParams, payOrderInfo);
        if (resJSON == null) {
             throw new BizException("请求失败");
        }
        if (resJSON.getIntValue("code") != 0) {
            throw new BizException(resJSON.getString("msg"));
        }
    }

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

        BsjParams bsjParams = getBsjParams(deviceParams);

        // 设置播报、打印并播报才执行自定义播报
        if (bsjParams.getBizConfigParams().getPrintMode() == DeviceParams.MODE_SPEAK || bsjParams.getBizConfigParams().getPrintMode() == DeviceParams.MODE_PRINT_AND_SPEAK) {
            JSONObject reqParams = new JSONObject();
            reqParams.put("actWay", DeviceParams.MODE_SPEAK);
            reqParams.put("voiceJson", BsjUtil.getSpeakData(bsjParams.getProviderParams().getCustomContent(), payOrderInfo).toJSONString());

            JSONObject resJSON = BsjUtil.post(REQ_URL, bsjParams, reqParams);
            if (resJSON == null) {
                throw new BizException("请求失败");
            }
            if (resJSON.getIntValue("code") != 0) {
                throw new BizException(resJSON.getString("msg"));
            }
        }else {
            throw new BizException("打印机未设置播报模式");
        }
    }

    @Override
    public String addPrinter(JSONObject deviceParams) {
        return null;
    }

    @Override
    public String editPrinter(JSONObject deviceParams) {
        return null;
    }

    @Override
    public String clearPrinter(JSONObject deviceParams) {
        return null;
    }


    /**
     * 发起请求
     */
    private JSONObject print(JSONObject deviceParams, PayOrderInfo4Device payOrderInfo) throws BizException {

        BsjParams bsjParams = getBsjParams(deviceParams);

        // 打印联数
        int printNum = bsjParams.getBizConfigParams().getPrintNum() != null ? bsjParams.getBizConfigParams().getPrintNum() : 1;

        JSONObject reqParams = new JSONObject();
        reqParams.put("actWay", bsjParams.getBizConfigParams().getPrintMode());

        // 仅播报
        if (bsjParams.getBizConfigParams().getPrintMode() == DeviceParams.MODE_SPEAK) {
            reqParams.put("voiceJson", BsjUtil.getSpeakData(bsjParams.getProviderParams().getCustomContent(), payOrderInfo).toJSONString());
            return BsjUtil.post(REQ_URL, bsjParams, reqParams);

        }
        // 仅打印
        else if (bsjParams.getBizConfigParams().getPrintMode() == DeviceParams.MODE_PRINT) {

            reqParams.put("data", getPrintData(payOrderInfo));
            reqParams.put("cn", printNum); // 打印联数
            return BsjUtil.post(REQ_URL, bsjParams, reqParams);

        }
        // 打印并播报
        else {

            reqParams.put("data", getPrintData(payOrderInfo));
            reqParams.put("voiceJson", BsjUtil.getSpeakData(bsjParams.getProviderParams().getCustomContent(), payOrderInfo).toJSONString());
            reqParams.put("cn", printNum); // 打印联数
            return BsjUtil.post(REQ_URL, bsjParams, reqParams);
        }
    }



    /** 获取打印内容 */
    public String getPrintData(PayOrderInfo4Device payOrderInfo) {

        String storeName = StringUtils.isNotBlank(payOrderInfo.getStoreName()) ? payOrderInfo.getStoreName() : "门店名称";

        return  "<RS:2><C><F>"+storeName+"</F></C><BR>" +
                "<RS:2><C>----------------</C><BR>" +
                "<S>订单编号: "+payOrderInfo.getPayOrderId()+"</S><BR>" +
                "<S>下单时间: "+DateUtil.format(payOrderInfo.getCreatedAt(), DatePattern.NORM_DATETIME_FORMAT)+"</S><BR>" +
                (StringUtils.isNotBlank(payOrderInfo.getRemark()) ? "<S>备  注: "+payOrderInfo.getRemark()+"</S><BR>" : "") +
                "实付: ￥"+BsjUtil.covertMoney(payOrderInfo.getAmount())+"<BR>" +
                "<CUT>";
    }

    private BsjParams getBsjParams(JSONObject deviceParams) {
        JSONObject bsjJSON = new JSONObject();
        bsjJSON.put("providerParams", JSON.parseObject(deviceParams.getString("providerParams")));
        bsjJSON.put("deviceParams", JSON.parseObject(deviceParams.getString("deviceParams")));
        bsjJSON.put("bizConfigParams", JSON.parseObject(deviceParams.getString("bizConfigParams")));

        return JSON.parseObject(bsjJSON.toJSONString(), BsjParams.class);
    }

}
