package com.ycwl.basic.printer.ticket;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.HashMap;
import java.util.Map;

public class FeiETicketPrinter {
    public static final String URL = "https://api.feieyun.cn/Api/Open/";

    public static final String USER = "792602257@qq.com";
    public static final String UKEY = "GdMvD6WwCGTAgM8x";

    public static String doPrint(String sn, String content, int times) {

        HttpRequest post = HttpUtil.createPost(URL);
        post.header("Content-Type", "application/x-www-form-urlencoded");
        Map<String, Object> body = new HashMap<>();
        body.put("user",USER);
        String STIME = String.valueOf(System.currentTimeMillis()/1000);
        body.put("stime",STIME);
        body.put("sig",signature(USER,UKEY,STIME));
        body.put("apiname","Open_printMsg");
        body.put("sn",sn);
        body.put("content",content);
        body.put("times",String.valueOf(times));
        try (HttpResponse response = post.form(body).execute()) {
            if (response.getStatus() != 200) {
                return null;
            }
            return response.body();
        }
    }
    //方法1
    private static String testPrint(String sn){
        //标签说明：
        //单标签:
        //"<BR>"为换行,"<CUT>"为切刀指令(主动切纸,仅限切刀打印机使用才有效果)
        //"<LOGO>"为打印LOGO指令(前提是预先在机器内置LOGO图片),"<PLUGIN>"为钱箱或者外置音响指令
        //成对标签：
        //"<CB></CB>"为居中放大一倍,"<B></B>"为放大一倍,"<C></C>"为居中,<L></L>字体变高一倍
        //<W></W>字体变宽一倍,"<QR></QR>"为二维码,"<BOLD></BOLD>"为字体加粗,"<RIGHT></RIGHT>"为右对齐
        //拼凑订单内容时可参考如下格式
        //根据打印纸张的宽度，自行调整内容的格式，可参考下面的样例格式

        String content;
        content  = "<BR><B>世界再大</B><BR>";
        content += "<B>你永远是这段旅途</B><BR>";
        content += "<B>的焦点</B><BR>";
        content += "━━━━━━━━━━━━━━━━<BR>";
        content += "<B>正片主演：</B><BR>";
        content += "旅途中最靓的你(测试名字很长很长故意不换行)<BR><BR>";
        content += "<B>拍摄地点：</B><BR>";
        content += "泸定桥<BR><BR>";
        content += "<B>拍摄日期：</B><BR>";
        content += "2025年4月4日<BR><BR>";
        content += "<B>大片内容：</B><BR>";
        content += "1.打卡泸定桥专属微电影(2部+)<BR>";
        content += "2.打卡录像集(5条)<BR>";
        content += "3.打卡照片集(5-10张)<BR>";
        content += "━━━━━━━━━━━━━━━━<BR>";
        content += "<C>帧帧皆故事 途途有回忆</C>";
        content += "<C>扫码即可观赏您的照片及视频</C>";
        content += "<BR><QR>https://zhentuai.com</QR>";
        content += "<CB>游后微信扫码查看</CB>";
        content += "<C>精彩指数:★★★★★</C><BR>";

//        content += "┏━━━━━━━━━━━━━━┓<BR>";
//        content += "┏━━━━━━━━━━━━━━┓<BR>";
//        content += "┏━━━━━━━━━━━━━━┓<BR>";
//        content += "┃┉3制67表01符45制89表23符6┉┃<BR>";
//        content += "┣━━★━━♢━━◈━━◉━━┫<BR>";
//        content += "┣━━★━━♢━━◈━━◉━━┫<BR>";
//        content += "┣━━★━━♢━━◈━━◉━━┫<BR>";
//        content += "┣━━★━━♢━━◈━━◉━━┫<BR>";
//        content += "┃123制67表01符45制89表23符678┃<BR>";
//        content += "┗━━━━━━━━━━━━━━┛<BR>";
        return doPrint(sn,content,1);
    }


    //生成签名字符串
    private static String signature(String user,String ukey,String stime){
        return DigestUtils.sha1Hex(user+ukey+stime);
    }

    public static void main(String[] args) {
        System.out.println(testPrint("550519002"));
    }
}
