package httpclient.print;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.mortals.xhx.busiz.req.PrintReq;
import com.mortals.xhx.common.code.*;
import com.mortals.xhx.common.utils.Base64Util;
import com.mortals.xhx.module.print.model.PrintContent;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Slf4j
@RunWith(JUnit4.class)
public class PrintTest {

    private String domain;


    private String baseStr="abcdefghijklmnopqrstuvwxyz123456789现实中我们经常遇到这样的需求寻找某个字是否存在于通规这字中换句话说即判断某字是否属于当今中国大陆的规范汉字文件找起字来费时费力网络上似也没有类似的检索工具且二三级字表中存在较多生僻的扩展区汉字目前不少网页仍无法完整显示这";

    @Before
    public void init(){
        //domain="http://127.0.0.1:8037";
     domain="http://192.168.0.43:8037";
    }

    @Test
    public void testPrintTicket() {
        //测试打印小票
        PrintReq printReq = new PrintReq();
        printReq.setPapertype(PaperTypeEnum.PAPER_TICKET.getValue());
        printReq.setPrintertype(PrintTypeEnum.PRINT_NORMAL.getValue());
       // printReq.setPrintername("lp1");
        printReq.setPrinterpaperwidth("240");

        List<PrintContent> contents = new ArrayList<>();
        PrintContent printContent = new PrintContent();
        printContent.initAttrValue();
        //printContent.setAlign(PrintAlignStyleEnum.PRINT_CENTER.getValue());
        contents.add(printContent);

        for (int i = 0; i < 50; i++) {
            printContent = new PrintContent();
            printContent.initAttrValue();
            //printContent.setAlign(PrintAlignStyleEnum.PRINT_CENTER.getValue());
            //printContent.setContent(RandomUtil.randomString(30));
           printContent.setContent(RandomUtil.randomString(baseStr,20));
           // RandomUtil.randomStringWithoutStr(RandomUtil.randomString(30));
            contents.add(printContent);
        }

        printContent = new PrintContent();
        printContent.initAttrValue();
        //printContent.setAlign(PrintAlignStyleEnum.PRINT_CENTER.getValue());
        printContent.setContent("adfasdfafdasdfasfddddddaaaaddadddsd");
        contents.add(printContent);

        printContent = new PrintContent();
        printContent.initAttrValue();
        printContent.setPrinttype(PrintContentTypeEnum.PRINT_SEPARATE.getValue());
        printContent.setContent("#");
        contents.add(printContent);

        printContent = new PrintContent();
        printContent.initAttrValue();
        printContent.setFontsize("16");
        printContent.setFontstyle(PrintFontStyleEnum.PRINT_BOLD.getValue());
        printContent.setAlign(PrintAlignStyleEnum.PRINT_CENTER.getValue());
        printContent.setContent("人民银行");
        contents.add(printContent);
        printContent = new PrintContent();
        printContent.initAttrValue();
        printContent.setPrinttype(PrintContentTypeEnum.PRINT_SEPARATE.getValue());
        printContent.setContent("=");
        contents.add(printContent);

    /*    //添加条形码
        printContent = new PrintContent();
        printContent.initAttrValue();
        printContent.setPrinttype(PrintContentTypeEnum.PRINT_BARCODE.getValue());
        printContent.setAlign(PrintAlignStyleEnum.PRINT_RIGHT.getValue());
        printContent.setContent("12455454554424");
        contents.add(printContent);

        //添加二维码
        printContent = new PrintContent();
        printContent.initAttrValue();
        printContent.setPrinttype(PrintContentTypeEnum.PRINT_QRCODE.getValue());
        printContent.setAlign(PrintAlignStyleEnum.PRINT_CENTER.getValue());
        printContent.setContent("http://www.baidu.com");
        printContent.setImgwidth("100");
        contents.add(printContent);
        //添加图片
        printContent = new PrintContent();
        printContent.initAttrValue();
        printContent.setPrinttype(PrintContentTypeEnum.PRINT_IMG.getValue());
        printContent.setContent("https://pic1.zhimg.com/v2-a661800d008ddef5569085ee779826f9_r.jpg");
        printContent.setImgwidth("100");
        contents.add(printContent);*/

        //JSON.toJSONString(printReq)
        printReq.setContents(contents);
        String url = domain+"/GetHardWareInfo";
        HashMap<String, Object> params = new HashMap<>();
        params.put("GetType", "71");
        params.put("PrinterJson", JSON.toJSONString(printReq));
        String rest = HttpUtil.post(url, params);
        log.info("打印响应结果:" + rest);


    }


    @Test
    public void testPrintBase64() {

        File file = new File("F:\\test.txt");
        String prefix = "txt";

        // base64文件内容
        String base64String = Base64Util.encodeBase64File(prefix, file);

        PrintReq printReq = new PrintReq();
        printReq.setPapertype(PaperTypeEnum.PAPER_A4.getValue());
        printReq.setPrintertype(PrintTypeEnum.PRINT_BASE64.getValue());

        printReq.setBase64(base64String);
        //printReq.setPrinterpaperwidth("140");

        String url = domain+"/GetHardWareInfo";
        HashMap<String, Object> params = new HashMap<>();
        params.put("GetType", "71");
        params.put("PrinterJson", JSON.toJSONString(printReq));
        String rest = HttpUtil.post(url, params);
        log.info("打印响应结果:" + rest);
    }

}
