package sgc.portal.base.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.pdf417.PDF417Writer;
import com.google.zxing.qrcode.QRCodeWriter;
import sgc.portal.base.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.nio.charset.StandardCharsets;

/**
 * <pre>
 * 시스템명   : 공통
 * 프로그램ID : QRUtil
 * 프로그램명  : QR 코드 생성 유틸
 * </pre>
 *
 * @author 이범근
 * @version 2019.12.05
 * @since JDK1.8
 */
@Slf4j
public class QRUtil {

    /**
     * QR Code 생성
     *
     * @param text   QR 코드 생성 String
     * @param width  QR 코드 넓이
     * @param height QR 코드 높이
     * @return base64 String
     */
    public static String makeQRBase64String(String text , int width , int height){
        String imgString;
        String formatName = "png";
        String tempText;
        try{

            QRCodeWriter writer = new QRCodeWriter();
            tempText = new String(text.getBytes(StandardCharsets.UTF_8) , StandardCharsets.ISO_8859_1);
            BitMatrix matrix = writer.encode(tempText, BarcodeFormat.QR_CODE, width, height);
            MatrixToImageConfig config = new MatrixToImageConfig();
            BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(matrix , config);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            ImageIO.write(qrImage ,  formatName ,  baos);
            baos.flush();

            String changeString =  new String( Base64.encodeBase64( baos.toByteArray() ) );
            imgString = "data:image/" + formatName + ";base64, " + changeString;

            baos.close();
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException("-1", "QR Code Create Fail !!", e);
        }

        return imgString;
    }

    /**
     * Make pdf 417 base 64 string string.
     *
     * @param text   the text
     * @param width  the width
     * @param height the height
     * @return the string
     */
    public static String makePDF417Base64String(String text , int width , int height){
        String imgString;
        String formatName = "png";
        String tempText;
        try{

            PDF417Writer pdf417Writer = new PDF417Writer();
            tempText = new String(text.getBytes(StandardCharsets.UTF_8) , StandardCharsets.ISO_8859_1);
            BitMatrix matrix = pdf417Writer.encode(tempText, BarcodeFormat.PDF_417, width, height);
            MatrixToImageConfig config = new MatrixToImageConfig();
            BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(matrix , config);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            ImageIO.write(qrImage ,  formatName ,  baos);
            baos.flush();

            String changeString =  new String( Base64.encodeBase64( baos.toByteArray() ) );
            imgString = "data:image/" + formatName + ";base64, " + changeString;

            baos.close();
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException("-1", "QR Code Create Fail !!", e);
        }

        return imgString;
    }

    /**
     * QR Code 생성 하여 파일로 저장 한다.
     *
     * @param text     QR 코드 생성 String
     * @param width    QR 코드 넓이
     * @param height   QR 코드 높이
     * @param filePath the file path
     * @param fileName the file name
     */
    public static void makeQR(String text , int width , int height , String filePath , String fileName){
        String formatName = "png";
        String tempText;
        try{
            File file = FileUtils.getFile(filePath, fileName);

            if(!file.exists()){
                file.mkdirs();
            }
            QRCodeWriter writer = new QRCodeWriter();
            tempText = new String(text.getBytes(StandardCharsets.UTF_8) , StandardCharsets.ISO_8859_1);
            BitMatrix matrix = writer.encode(tempText, BarcodeFormat.QR_CODE, width, height);
            MatrixToImageConfig config = new MatrixToImageConfig();
            BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(matrix , config);

            ImageIO.write(qrImage ,  formatName , file);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }


}
