package net.itarray.automotion.internal;

import net.itarray.automotion.Element;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.json.simple.JSONObject;
import util.validator.ResponsiveUIValidator;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import static util.validator.Constants.*;

public class DrawableScreenshot {

    public final File screenshot;
    private final DrawingConfiguration drawingConfiguration;
    private BufferedImage img;
    private TransformedGraphics graphics;

    public DrawableScreenshot(DriverFacade driver, SimpleTransform transform, DrawingConfiguration drawingConfiguration) {
        screenshot = driver.takeScreenshot();
        this.drawingConfiguration = drawingConfiguration;
        try {
            img = ImageIO.read(screenshot);
            Graphics2D g = img.createGraphics();

            graphics = new TransformedGraphics(g, transform);
        } catch (Exception e) {
            throw new RuntimeException("Failed to create screenshot file: " + screenshot, e);
        }
    }

    public void drawScreenshot(Element rootElement, String rootElementReadableName, Errors errors, OffsetLineCommands offsetLineCommands) {
        drawRootElement(rootElement);

        offsetLineCommands.draw(graphics, img, rootElement, drawingConfiguration);

        for (Object obj : errors.getMessages()) {
            JSONObject det = (JSONObject) obj;
            JSONObject details = (JSONObject) det.get(REASON);
            JSONObject numE = (JSONObject) details.get(ELEMENT);

            if (numE != null) {
                int x = (int) (float) numE.get(X);
                int y = (int) (float) numE.get(Y);
                int width = (int) (float) numE.get(WIDTH);
                int height = (int) (float) numE.get(HEIGHT);

                drawingConfiguration.setHighlightedElementStyle(graphics);
                graphics.drawRectByExtend(x, y, width, height);
            }
        }

        try {
            ImageIO.write(img, "png", screenshot);
        } catch (IOException e) {
            throw new RuntimeException("Writing file failed for " + screenshot , e);
        }
        File file = new File(TARGET_AUTOMOTION_IMG + rootElementReadableName.replace(" ", "") + "-" + screenshot.getName());
        try {
            FileUtils.copyFile(screenshot, file);
        } catch (IOException e) {
            throw new RuntimeException("copying file failed " + screenshot + " to " + file, e);
        }
    }

    private void drawRootElement(Element rootElement) {
        drawingConfiguration.setRootElementStyle(graphics);
        int x = rootElement.getX();
        int y = rootElement.getY();
        graphics.drawRectByExtend(x, y, rootElement.getWidth(), rootElement.getHeight());
    }
}

@@ -2062,202 +2062,8 @@
  %7D%0A%0A
-        try %7B%0A            ImageIO.write(img, %22png%22, screenshot);%0A        %7D catch (IOException e) %7B%0A            throw new RuntimeException(%22Writing file failed for %22 + screenshot , e);%0A        %7D%0A
     
@@ -2195,55 +2195,89 @@
     
-try %7B%0A            FileUtils.copyFile(screenshot
+file.getParentFile().mkdirs();%0A        try %7B%0A            ImageIO.write(img, %22png%22
 , fi
@@ -2359,12 +2359,12 @@
 on(%22
-copy
+Writ
 ing 
@@ -2375,16 +2375,20 @@
  failed 
+for 
 %22 + scre
@@ -2398,23 +2398,8 @@
 hot 
-+ %22 to %22 + file
 , e)

write drawed on screenshot to desired location directly