package com.mortals.xhx.module.print.service.PrintComponent;

import cn.hutool.extra.spring.SpringUtil;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.util.DataUtil;
import com.mortals.xhx.common.code.PaperTypeEnum;
import com.mortals.xhx.common.code.PrintTypeEnum;
import com.mortals.xhx.common.code.TicketTypeWidthEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.standard.Sides;
import java.awt.print.*;

/**
 * 普通行打印
 *
 * @author:
 * @date: 2022/12/27 9:37
 */
@Slf4j
public class NormalPrintComponent extends BasePrintComponent {

    Integer ticketMarginWidth = 30;
    Integer marginTop = 20;
    Integer marginBottom = 10;

    public NormalPrintComponent(String type) {
        super(type);
    }

    @Override
    public Rest<Void> print(ComponentCons cons) {
        String ticketType = SpringUtil.getProperty("print.ticket");
        //创建打印文档
        Book book = new Book();
        //设置
        TicketPrinter ticketPrinter = new TicketPrinter();
        ticketPrinter.setMarginWidth(ticketMarginWidth);
        ticketPrinter.setMarginTop(marginTop);
        ticketPrinter.setPrintContentList(cons.getContents());
        Integer ticketWidth = Integer.parseInt(TicketTypeWidthEnum.TYPE_LARGER.getDesc());
        if (TicketTypeWidthEnum.TYPE_LARGER.getValue().equals(ticketType)) {
            ticketWidth = Integer.parseInt(TicketTypeWidthEnum.TYPE_LARGER.getDesc());
        } else if (TicketTypeWidthEnum.TYPE_SMALL.getValue().equals(ticketType)) {
            ticketWidth = Integer.parseInt(TicketTypeWidthEnum.TYPE_SMALL.getDesc());
            ticketMarginWidth = 22;
        }
        //打印格式
        PageFormat pageFormat = new PageFormat();
        //设置打印起点从左上角开始，从左到右，从上到下打印
        pageFormat.setOrientation(PageFormat.PORTRAIT);
        //打印页面格式设置
        Paper paper = new Paper();
        //设置打印宽度(固定，和具体的打印机有关)和高度(跟实际打印内容的多少有关)
        if (cons.getPapertype().equals(PaperTypeEnum.PAPER_A3.getValue())) {
            paper.setSize(841, 1190);
            //设置打印区域 打印起点坐标、打印的宽度和高度
            paper.setImageableArea(0, 0, 841, 1190);
        } else if (cons.getPapertype().equals(PaperTypeEnum.PAPER_A4.getValue())) {
            paper.setSize(595, 842);
            //设置打印区域 打印起点坐标、打印的宽度和高度
            paper.setImageableArea(0, 0, 595, 842);
        } else if (cons.getPapertype().equals(PaperTypeEnum.PAPER_TICKET.getValue())) {
            paper.setSize(ticketWidth, 576);
            paper.setImageableArea(0, 0, ticketWidth, 566);
            pageFormat.setPaper(paper);
            int actualHeight = ticketPrinter.getActualHeight(pageFormat);
            actualHeight += marginBottom;
            log.info("ticketWidth:{} actualHeight:{}", ticketWidth, actualHeight);
            //actualHeight=1500;
            paper.setSize(ticketWidth, actualHeight);
            //设置打印区域 打印起点坐标、打印的宽度和高度
            paper.setImageableArea(0, 0, ticketWidth, actualHeight);
            pageFormat.setPaper(paper);
/*            paper.setSize(ticketWidth, 1314);
            //设置打印区域 打印起点坐标、打印的宽度和高度
            paper.setImageableArea(0, 0, ticketWidth, 1314);*/
        } else {
            //默认小票高宽 两种纸张宽度宽度  1.164 页边距11  2.227 页边距14
            int actualHeight = ticketPrinter.getActualHeight(pageFormat);
            //actualHeight += marginBottom;

            //actualHeight=1500;
            paper.setSize(ticketWidth, actualHeight);
            //设置打印区域 打印起点坐标、打印的宽度和高度
            paper.setImageableArea(0, 0, ticketWidth, actualHeight);
            log.info("ticketWidth:{} actualHeight:{}", ticketWidth, actualHeight);
/*            paper.setSize(ticketWidth, 566);
            //设置打印区域 打印起点坐标、打印的宽度和高度
            paper.setImageableArea(0, 0, ticketWidth, 566);*/
        }

        pageFormat.setPaper(paper);

        book.append(ticketPrinter, pageFormat);
        //获取默认打印机
        PrintService printService = getPrintService(cons);
        PrinterJob printerJob = PrinterJob.getPrinterJob();
        log.info("page:" + book.getNumberOfPages());
        printerJob.setPageable(book);
        printerJob.setCopies(1);// 设置打印份数
        try {
            if (printService != null) {
                printerJob.setPrintService(printService);
                HashPrintRequestAttributeSet pars = new HashPrintRequestAttributeSet();
                pars.add(Sides.ONE_SIDED); // 设置单双页
                printerJob.print(pars);
                return Rest.ok();
            } else {
                log.info("打印失败，未找到名称为" + cons.getPrintername() + "的打印机，请检查。");
                return Rest.fail("打印失败，未找到名称为" + cons.getPrintername() + "的打印机，请检查。");
//                throw new AppException("打印失败，未找到名称为" + cons.getPrintername() + "的打印机，请检查。");

            }
        } catch (PrinterException e) {
            log.error("打印异常", e);
            return Rest.fail("打印异常");
//            throw new AppException(e.getMessage());
        }
    }

    @Override
    public String getType() {
        return PrintTypeEnum.PRINT_NORMAL.getValue();
    }
}
