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

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
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.IPrinterService;
import com.jeequan.jeepay.core.model.device.DeviceParams;
import com.jeequan.jeepay.core.model.device.PayOrderInfo4Device;
import com.jeequan.jeepay.core.model.device.YlyParams;
import com.jeequan.jeepay.core.utils.AmountUtil;
import com.jeequan.jeepay.thirdparty.device.printer.ylyPrinter.sdk.RequestMethod;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/*
 * 云打印厂商： 易联云
 *
 * @author xiaoyu
 *
 * @date 2023/4/11 16:36
 */
@Slf4j
@Service
public class YlyPrinterService implements IPrinterService {

    @Override
    public void send(JSONObject deviceParams, PayOrderInfo4Device payOrderInfo) throws BizException {
        YlyParams ylyParams = getParams(deviceParams);
        // 设备信息
        YlyParams.DeviceParams printConfig = ylyParams.getDeviceParams();
        // 厂商信息
        YlyParams.ProviderParams providerParams = ylyParams.getProviderParams();

        String content = "";
        // 打印内容
        String contentPrint = createMessage(payOrderInfo, providerParams);

        if (PayOrderInfo4Device.BOARD_TYPE_CANCEL.equals(payOrderInfo.getWayCodeType())) {
            content = "<VI>039</VI><VI>043</VI><VD>"+AmountUtil.convertCent2Dollar(payOrderInfo.getAmount())+"</VD><VI>032</VI>";
        }else {
            content = getPayTypeCode(payOrderInfo.getWayCodeType()) + "<VI>041</VI><VI>031</VI><VD>"+AmountUtil.convertCent2Dollar(payOrderInfo.getAmount())+"</VD><VI>032</VI>";
        }

        // 仅播报
        if (ylyParams.getBizConfigParams().getPrintMode() == DeviceParams.MODE_SPEAK) {
            RequestMethod.getInstance().printIndex(providerParams, printConfig, content, payOrderInfo.getPayOrderId());
        }
        // 仅打印
        else if (ylyParams.getBizConfigParams().getPrintMode() == DeviceParams.MODE_PRINT) {
            RequestMethod.getInstance().printIndex(providerParams, printConfig, contentPrint, payOrderInfo.getPayOrderId());
        }
        // 打印并播报
        else {
            // 播报+打印结果
            content = content + contentPrint;
            RequestMethod.getInstance().printIndex(providerParams, printConfig, content, payOrderInfo.getPayOrderId());
        }

        // 打印联数
        int printNum = ylyParams.getBizConfigParams().getPrintNum() != null ? ylyParams.getBizConfigParams().getPrintNum() : 1;
        if (printNum > 1) {
            // 遍历打印
            for (int i = 1; i < printNum; i++) {
                RequestMethod.getInstance().printIndex(providerParams, printConfig, contentPrint, payOrderInfo.getPayOrderId());
            }
        }

    }

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

    @Override
    public String addPrinter(JSONObject deviceParams) {
        YlyParams ylyParams = getParams(deviceParams);
        // 设备信息
        YlyParams.DeviceParams printConfig = ylyParams.getDeviceParams();
        // 厂商信息
        YlyParams.ProviderParams providerParams = ylyParams.getProviderParams();

        // 解绑&取消授权
        RequestMethod.getInstance().printerDeletePrinter(providerParams, printConfig);

        // 绑定设备
        RequestMethod.getInstance().addPrinter(providerParams, printConfig);

        // 设置打印logo (配置参数不为空)
        if (StringUtils.isNotEmpty(providerParams.getPrintSetIcon())) {
            // logo删除
            RequestMethod.getInstance().printDeleteIcon(providerParams, printConfig);
            // logo设置
            RequestMethod.getInstance().printSetIcon(providerParams, printConfig);
        }
        // 设置K8设备关键词
        if (StringUtils.isNotEmpty(providerParams.getKeywords())) {
            JSONArray array = new JSONArray();
            array.add(providerParams.getKeywords());
            RequestMethod.getInstance().printerSetKeywords(providerParams, printConfig, "order_payment", "white_list", array.toJSONString());
        }

        return "success";
    }

    @Override
    public String editPrinter(JSONObject deviceParams) {
        addPrinter(deviceParams);
        return "success";
    }

    @Override
    public String clearPrinter(JSONObject deviceParams) {
        YlyParams ylyParams = getParams(deviceParams);
        // 设备信息
        YlyParams.DeviceParams printConfig = ylyParams.getDeviceParams();
        // 厂商信息
        YlyParams.ProviderParams providerParams = ylyParams.getProviderParams();
        // 调用
        RequestMethod.getInstance().printCancelAll(providerParams, printConfig);
        return null;
    }


    private YlyParams getParams(JSONObject deviceParams) {
        JSONObject params = new JSONObject();
        params.put("providerParams", JSON.parseObject(deviceParams.getString("providerParams")));
        params.put("deviceParams", JSON.parseObject(deviceParams.getString("deviceParams")));
        params.put("bizConfigParams", JSON.parseObject(deviceParams.getString("bizConfigParams")));
        return JSON.parseObject(params.toJSONString(), YlyParams.class);
    }

    /**
     * 支付方式
     */
    public static String getPayTypeCode(String wayCodeType) {
        if (CS.PAY_WAY_CODE_TYPE.WECHAT.equals(wayCodeType)) { // 微信
            return "<VI>033</VI>";
        }else if (CS.PAY_WAY_CODE_TYPE.ALIPAY.equals(wayCodeType)) { // 支付宝
            return "<VI>034</VI>";
        }else if (CS.PAY_WAY_CODE_TYPE.YSFPAY.equals(wayCodeType)) { // 云闪付
            return "<VI>050</VI>";
        }else {
            return "";
        }
    }

    /**
     *  根据订单  生成云打印机打印内容
     *
     * */
    public String createMessage(PayOrderInfo4Device printPayOrderInfo, YlyParams.ProviderParams providerParams) {

        String payOrderId = printPayOrderInfo.getPayOrderId();

        //  标签说明：
        //  文档地址：https://www.kancloud.cn/ly6886/oauth-api/3170341

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

        // 打印内容
        String content = "";
        content = "<FS2><center>----------</center></FS2>\n"+
                "<FS2>"+storeName+"</FS2>\n\n" +
                "下单时间: "+ DateUtil.format(printPayOrderInfo.getCreatedAt(), DatePattern.NORM_DATETIME_PATTERN)+"\n" +
                "订单号: "+payOrderId+"\n" +
                "订单金额: "+AmountUtil.convertCent2Dollar(printPayOrderInfo.getAmount())+"\n" +
                "<FS2><center>----------</center></FS2>";
        return content;
    }


}
