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

import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.xhx.busiz.req.PrintReq;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.module.print.model.Printer;
import com.mortals.xhx.module.print.model.PrinterQueue;
import com.mortals.xhx.module.print.service.PrintComponent.BasePrintComponent;
import com.mortals.xhx.module.print.service.PrintComponent.ComponentCons;
import com.mortals.xhx.module.print.service.PrintService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.print.attribute.Attribute;
import javax.print.attribute.AttributeSet;
import java.awt.print.PrinterJob;
import java.util.ArrayList;
import java.util.List;

/**
 * 打印实现类
 *
 * @author:
 * @date: 2022/12/26 14:09
 */
@Service
@Slf4j
public class PrintServiceImpl implements PrintService {
    @Override
    public Rest<Void> executePrint(PrintReq printReq) {
        //根据打印类型
        BasePrintComponent basePrintComponent = BasePrintComponent.createType(printReq.getPrintertype());
        ComponentCons componentCons = new ComponentCons();
        BeanUtils.copyProperties(printReq,componentCons, BeanUtil.getNullPropertyNames(printReq));
        basePrintComponent.print(componentCons);
        //获取打印的类型
        return Rest.ok();
    }

    @Override
    public Rest<List<Printer>> getPrintList() {
        List<Printer> list = new ArrayList<>();
        javax.print.PrintService[] printServices = PrinterJob.lookupPrintServices();
        for (int i = 0; i < printServices.length; i++) {
            log.info(printServices[i].getName());
            Printer printer = new Printer();
            printer.setPinter(printServices[i].getName());
            list.add(printer);
        }
        return Rest.ok(list);
    }

    @Override
    public Rest<List<PrinterQueue>> getPrintQueue(String printerName) {
        ArrayList<PrinterQueue> printerQueues = new ArrayList<>();
        javax.print.PrintService printService = null;
        javax.print.PrintService[] printServices = PrinterJob.lookupPrintServices();
        if (printServices == null || printServices.length == 0) {
            log.info("打印失败，未找到可用打印机，请检查!");
            throw new AppException("打印失败，未找到可用打印机，请检查!");
        }
        for (int i = 0; i < printServices.length; i++) {
            log.info(printServices[i].getName());
            if (printServices[i].getName().contains(printerName)) {
                printService = printServices[i];
                break;
            }
        }
        //队列数量
        int queue = 0;
        if(printServices!=null){
            AttributeSet attributes = printService.getAttributes();
            for (Attribute a : attributes.toArray()) {
                String name = a.getName();
                String value = attributes.get(a.getClass()).toString();
                log.info(name + " : " + value);
                if (name.equals("queued-job-count")) {
                    queue = Integer.parseInt(value);
                }
            }
            for (int i = 0; i <queue ; i++) {
                PrinterQueue printerQueue = new PrinterQueue();
                printerQueue.setQueueName("打印队列"+i);
                printerQueues.add(printerQueue);
            }
           // Object[] obj = attributes.toArray();
        }
        return Rest.ok(printerQueues);
    }
}
