package com.gree.mall.manager.utils;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;

public class CreateQrcodeUtil {

    private static final int BLACK = 0xff000000;
    private static final int WHITE = 0xFFFFFFFF;

    static Logger logger = LoggerFactory.getLogger(CreateQrcodeUtil.class);


    public static String createQrcode(String url, String path) {

        String filePostfix = "png";
        String id = "";
        String newFilePath = path + "/qrcode/";
        String fileName = id + ".png";


        File checkFile = new File(newFilePath);
        if (!checkFile.exists() && !checkFile.isDirectory()) {
            checkFile.mkdirs();
        }

        String filePath = newFilePath + fileName;

        File file = new File(filePath);
        encode(url, file, filePostfix, BarcodeFormat.QR_CODE, 500, 500, null);


        return "/qrcode/" + fileName;


    }

    public static void encode(String contents, File file, String filePostfix, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) {
        try {
            contents = new String(contents.getBytes("UTF-8"), "ISO-8859-1");
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, format, width, height);
            writeToFile(bitMatrix, filePostfix, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        ImageIO.write(image, format, file);
    }

    public static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE);
            }
        }
        return image;
    }


    public static boolean generateCode(String path,String promotionId,String drawType) throws WriterException, IOException {
        // 这里是URL，扫描之后就跳转到这个界面
        String text = "https://www.taobao.com/?promotionid=" + promotionId+"&promotionType="+drawType;

        // 图片生成的位置
        String filePostfix = "png";
        String id = "";
        String newFilePath = path + "/qrcode/";
        String fileName = id + ".png";

        File checkFile = new File(newFilePath);
        if (!checkFile.exists() && !checkFile.isDirectory()) {
            checkFile.mkdirs();
        }

        String filePath = newFilePath + fileName;

        File file = new File(filePath);

        int width = 400;
        int height = 400;
        // 二维码图片格式
        String format = "jpg";
        // 设置编码，防止中文乱码
        Hashtable<EncodeHintType, Object> ht = new Hashtable<EncodeHintType, Object>();
        ht.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        // 设置二维码参数(编码内容，编码类型，图片宽度，图片高度,格式)
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, ht);


        int b_width = bitMatrix.getWidth();
        int b_height = bitMatrix.getHeight();
        // 建立图像缓冲器
        BufferedImage image = new BufferedImage(b_width, b_height, BufferedImage.TYPE_3BYTE_BGR);
        for (int x = 0; x < b_width; x++) {
            for (int y = 0; y < b_height; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE);
            }
        }
        // 生成二维码
        ImageIO.write(image, format, file);
        // 二维码的名称
        // code.jpg

        return true;
    }


}