package me.lenajenichen.voicebot.api;

import org.jetbrains.annotations.NotNull;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class CreateCaptchaImage {

    private static @NotNull String code = "null";

    public static void create() throws IOException {

        int width = 250;
        int height = 125;
        code = Generator.generate();

        // Constructs a BufferedImage of one of the predefined image types.
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        // Create a graphics which can be used to draw into the buffered image
        Graphics2D g2d = bufferedImage.createGraphics();

        g2d.setColor(Color.decode("#3B8EB7"));
        g2d.fillRect(0, 0, width, height);

        g2d.setColor(Color.decode("#327B9F"));
        g2d.setFont(new Font("SansSerif", Font.PLAIN, 43));
        g2d.drawString(code, 65, 75);

        g2d.setColor(Color.black);
        for(int i = 0; i < 6; i++) {
            int y1 = (int) Math.round(Math.random() * (90 - (-20) + 1) + 0);
            int y2 = (int) Math.round(Math.random() * (90 - (-20) + 1) + 0);
            g2d.drawLine(53, y1, 183, y2);
        }

        // Save as PNG
        File file = new File("/var/www/html/images/captcha.png");
        ImageIO.write(bufferedImage, "png", file);
    }

    public static @NotNull String getCode() {
        return code;
    }

}
