package com.fastbee.data.controller.printer;

import com.fastbee.common.core.controller.BaseController;
import com.fastbee.common.core.domain.AjaxResult;
import com.fastbee.common.exception.ServiceException;
import com.fastbee.data.controller.printer.yilianyun.GenerateQRCodeImage;
import com.fastbee.data.controller.printer.yilianyun.YiLianYunPrintService;
import com.fastbee.iot.domain.DeviceReportInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
@RequestMapping("/printer")
public class PrinterController extends BaseController {

    @Autowired
    private YiLianYunPrintService printerService;

    /**
     * 文本内容打印
     */
    @PostMapping("/text/do")
    public AjaxResult textPrint(@RequestBody String content){
        printerService.textPrint(content);
       return AjaxResult.success();
    }

    /**
     * 图片打印
     */
    @PostMapping("/image/do")
    public AjaxResult imagePrint(@RequestBody String imageUrl,@Param("printNum") Integer printNum) throws InterruptedException {
        if(Objects.isNull(printNum)){
            throw new ServiceException("打印次数不能为空!");
        }
//        if(printNum>2){
//            throw new ServiceException("打印次数不能超过2!");
//        }
        for (int i = 0; i < printNum; i++) {
            printerService.imagePrint(imageUrl);
            Thread.sleep(200);
        }
       return AjaxResult.success();
    }
    /**
     * 生成待打印图片
     */
    @PostMapping("/image/msg/generate")
    public AjaxResult generateImage(@RequestBody DeviceReportInfo deviceReportInfo ){
        System.err.println(deviceReportInfo);
        String qrCodeUrl = GenerateQRCodeImage.generateQRCodeUrl(deviceReportInfo);
        return AjaxResult.success("操作成功",qrCodeUrl);
    }
}
