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

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.FePrinterParams;
import com.jeequan.jeepay.core.model.device.PayOrderInfo4Device;
import com.jeequan.jeepay.core.utils.AmountUtil;
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 FePrinterService implements IPrinterService {

    @Override
    public void send(JSONObject deviceParams, PayOrderInfo4Device printPayOrderInfo) {

        try {

            // 飞鹅云打印参数
            FePrinterParams fePrinterParams = getFePrintParams(deviceParams);

            // 消息体
            String result = FeUtil.print(fePrinterParams, createMessage(printPayOrderInfo));
            if (result == null) {
                throw new BizException("打印失败");
            }

        } catch (Exception e) {
            log.error("飞鹅云打印机打印失败", e);
            throw new BizException(e.getMessage());
        }
    }

    @Override
    public void sendCustomMsg(JSONObject deviceParams, PayOrderInfo4Device printPayOrderInfo) throws BizException {
        return;
    }

    @Override
    public String addPrinter(JSONObject deviceParams) {

        try {
            return FeUtil.addPrinter(getFePrintParams(deviceParams));
        } catch (Exception e) {
            log.error("新增飞鹅云打印机失败", e);
            throw new BizException(e.getMessage());
        }
    }

    @Override
    public String editPrinter(JSONObject deviceParams) {

        try {
            return FeUtil.editPrinter(getFePrintParams(deviceParams));
        } catch (Exception e) {
            log.error("编辑飞鹅云打印机失败", e);
            throw new BizException(e.getMessage());
        }
    }

    @Override
    public String clearPrinter(JSONObject deviceParams) {

        try {
            return FeUtil.clear(getFePrintParams(deviceParams));
        } catch (Exception e) {
            log.error("清空飞鹅打印队列失败", e);
            throw new BizException(e.getMessage());
        }
    }

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

        if (StringUtils.isNotBlank(deviceParams.getString("bizConfigParams"))) {
            paramsJSON.put("bizConfigParams", JSON.parseObject(deviceParams.getString("bizConfigParams")));
        }

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

    /**
     * 根据订单  生成云打印机打印内容
     * messageJSON应包含：
     * tradeOrderId   订单ID，goodsJson为空时，必传
     * storeName    门店名称，非必传
     * goodsJson    商品信息JSON，非必传
     * amount    支付金额，goodsJson为空时，必传
     * storeAreaName    门店区域/桌号名称，非必传
     * createTime    下单时间
     */
    public String createMessage(PayOrderInfo4Device printPayOrderInfo) {

        String payOrderId = printPayOrderInfo.getPayOrderId();

        //标签说明：
        //"<BR>"为换行,"<CUT>"为切刀指令(主动切纸,仅限切刀打印机使用才有效果)
        //"<LOGO>"为打印LOGO指令(前提是预先在机器内置LOGO图片),"<PLUGIN>"为钱箱或者外置音响指令
        //成对标签：
        //"<CB></CB>"为居中放大一倍,"<B></B>"为放大一倍,"<C></C>"为居中,<L></L>字体变高一倍
        //<W></W>字体变宽一倍,"<QR></QR>"为二维码,"<BOLD></BOLD>"为字体加粗,"<RIGHT></RIGHT>"为右对齐

        // 门店名称
        String storeName = printPayOrderInfo.getStoreName();
        storeName = StringUtils.isNotBlank(storeName) ? storeName : "计全科技";

        // 打印内容
        String content = "<CB>" + storeName + "</CB><BR>";

        //金额
        String amount = AmountUtil.convertCent2Dollar(printPayOrderInfo.getAmount());

        content += "--------------------------------<BR>";
        if (StringUtils.isNotBlank(payOrderId)) {
            content += "订单号：" + payOrderId + "<BR>";
        }
        content += "订单金额：" + amount + "元<BR>";
        content += "下单时间：" + DateUtil.format(printPayOrderInfo.getCreatedAt(), DatePattern.NORM_DATETIME_FORMAT) + "<BR>";
        // content += "<BR><QR>https://www.jeequan.com/</QR>";

        return content;
    }

}
