package com.qmth.themis.business.util;

import com.qmth.themis.business.domain.SysDomain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @Description: Qrcode
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2022/3/30
 */
@Component
public class QrCodeUtil {
    private final static Logger log = LoggerFactory.getLogger(QrCodeUtil.class);

    private SysDomain sysDomain;

    @Bean
    public SysDomain sysDomainEnv(SysDomain sysDomain) {
        this.sysDomain = new SysDomain(sysDomain.getDatacenterId(),
                sysDomain.isOss(),
                sysDomain.getAttachmentType(),
                sysDomain.getServerUpload(),
                sysDomain.getFileHost(),
                sysDomain.getServerHost(),
                sysDomain.getAdminLogoUrl(),
                sysDomain.getQrExpireTime());
        return this.sysDomain;
    }

    public SysDomain getSysDomain() {
        return sysDomain;
    }

    //    /**
//     * 生成一个二维码图片
//     *
//     * @param width
//     * @param height
//     * @param content
//     * @return
//     * @throws WriterException
//     * @throws IOException
//     */
//    public static byte[] createQRCode(int width, int height, String content) throws WriterException, IOException {
//        // 二维码基本参数设置
//        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
//        hints.put(EncodeHintType.CHARACTER_SET, SystemConstant.CHARSET_NAME);// 设置编码字符集utf-8
//        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);// 设置纠错等级L/M/Q/H,纠错等级越高越不易识别，当前设置等级为最高等级H
//        hints.put(EncodeHintType.MARGIN, 0);// 可设置范围为0-10，但仅四个变化0 1(2) 3(4 5 6) 7(8 9 10)
//        //生成图片类型为QRCode
//        BarcodeFormat format = BarcodeFormat.QR_CODE;
//        // 创建位矩阵对象
//        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, format, width, height, hints);
//        // 设置位矩阵转图片的参数
//        //MatrixToImageConfig config = new MatrixToImageConfig(Color.black.getRGB(), Color.white.getRGB());
//        //位矩阵对象转流对象
//        ByteArrayOutputStream os = new ByteArrayOutputStream();
//        MatrixToImageWriter.writeToStream(bitMatrix, "png", os);
//        return os.toByteArray();
//    }
//
//    public static void main(String[] args) throws WriterException, IOException {
//        byte[] b = createQRCode(300, 300, "遇见最好的自己！123");
//        InputStream inputStream = new ByteArrayInputStream(b);
//        String md5 = DigestUtils.md5Hex(inputStream);
//        System.out.println("md5:" + md5);
////        String filePath = "upload" + File.separator + sdf.format(new Date()) + File.separator + uuid() + "." + suffix;
////        InputStream in = null;
////        try {
////            ossUtil.upload(true, filePath, inputStream, BinaryUtil.toBase64String(HexUtils.decodeHex(md5)));
////        }
//        OutputStream os = new FileOutputStream("/Users/king/Downloads/1.png");
//        os.write(b);
//        os.close();
//        inputStream.close();
//    }
}
