package org.jeecg.modules.qrcodeutil.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.jeecg.modules.qrcodeutil.utils.QRCodeUtil;
import org.jeecg.modules.qrcodeutil.utils.R;
import org.jeecg.modules.sensor.entity.BuSensor;
import org.jeecg.modules.sensor.service.IBuSensorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.URLDecoder;
import java.net.UnknownHostException;
import java.nio.file.FileSystems;
import java.util.HashMap;


/**
 * <p>
 * QRCodeController
 * </p>
 *
 * @author rcbb.cc
 * @date 2022/8/28
 */
@RestController
@RequestMapping("/qrcode")
public class QRCodeController {
    @Autowired
    private IBuSensorService buSensorService;
    //二维码扫描
    @GetMapping(value = "/getCodeData")
    public String getCodeData(@RequestParam(name = "sensorId") String sensorId) {
        BuSensor buSensor = buSensorService.getCodeData(sensorId);
        StringBuilder sb = new StringBuilder();
        sb.append("设备编号:");
        sb.append(buSensor.getDeviceCode());
        sb.append("、");
        sb.append("设备名称:");
        sb.append(buSensor.getDeviceName());
        sb.append("、");
        sb.append("传感器类型:");
        sb.append(buSensor.getTypeId());
        sb.append("、");
        sb.append("传感器名称:");
        sb.append(buSensor.getName());
        sb.append("、");
        sb.append("安装位置:");
        sb.append(buSensor.getLocation());
        String text = sb.toString();
        return text;
    }
    /**
     * 根据 content 生成二维码
     *
     * @param content
     * @param width
     * @param height
     * @return
     */
    @GetMapping("/getQRCodeBase64")
    public R getQRCode(@RequestParam("content") String content,
                       @RequestParam(value = "logoUrl", required = false) String logoUrl,
                       @RequestParam(value = "width", required = false) Integer width,
                       @RequestParam(value = "height", required = false) Integer height) throws UnsupportedEncodingException, UnknownHostException {
        logoUrl = QRCodeController.class.getClassLoader().getResource("images/code.png").getPath();
        String logoPath = URLDecoder.decode(logoUrl, "utf-8");
        BuSensor buSensor = buSensorService.getById(content);
        InetAddress localHost = InetAddress.getLocalHost();
        String ipAddress = localHost.getHostAddress();
        String text ="http://"+ipAddress+":8080/jeecg-boot/qrcode/getCodeData?sensorId="+buSensor.getId();
//        String text = this.getCodeData(buSensor.getId());
//        String text1 = URLDecoder.decode(text, "utf-8");
        String base64QRCode = QRCodeUtil.getBase64QRCode(text, logoPath);

//        buSensor.setCode(base64QRCode);
//        buSensorService.updateById(buSensor);
        return R.ok(base64QRCode);
    }

    /**
     * 根据 content 生成二维码
     */
    @GetMapping(value = "/getQRCode")
    public void getQRCode(HttpServletResponse response,
                          @RequestParam("content") String content,
                          @RequestParam(value = "logoUrl", required = false) String logoUrl) throws Exception {
        ServletOutputStream stream = null;
        try {
            stream = response.getOutputStream();
            QRCodeUtil.getQRCode(content, logoUrl, stream);
        } catch (Exception e) {
            e.getStackTrace();
        } finally {
            if (stream != null) {
                stream.flush();
                stream.close();
            }
        }
    }
}
