package ch.fadre.sandhills;

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

public class ImageWriter {

    public BufferedImage generateImage(int[][] grid, int width, int height) throws IOException, InterruptedException {
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        for (int i = 0; i < grid.length; i++) {
            int[] row = grid[i];
            for (int j = 0; j < row.length; j++) {
                bufferedImage.setRGB(j, i, getColor(row[j]).getRGB());
            }
        }
        return bufferedImage;
    }

    public void displayInFrame(BufferedImage bufferedImage) {
        JFrame jFrame = new JFrame();
        jFrame.setSize(1200, 1000);
        jFrame.getContentPane().setLayout(new BorderLayout());
        jFrame.getContentPane().add(new JLabel(new ImageIcon(bufferedImage)));
        jFrame.setVisible(true);
    }

    public void writeToFile(BufferedImage bufferedImage, String prefix) throws IOException {
        String fileName = prefix + System.currentTimeMillis() + ".png";
        ImageIO.write(bufferedImage, "png", new File(fileName));
    }

    private static Color getColor(int i) {
        switch (i) {
            case 0:
                return new Color(39, 111, 161);
            case 1:
                return new Color(26, 151, 82);
            case 2:
                return new Color(224, 143, 18);
            case 3:
                return new Color(176, 52, 39);
            default:
                System.out.println("This should never happen :)");
                return new Color(39, 111, 161);
        }
    }
}

@@ -183,35 +183,495 @@
 on;%0A
-%0Apublic class ImageWriter %7B
+import java.util.HashMap;%0Aimport java.util.Map;%0A%0Apublic class ImageWriter %7B%0A%0A    private Map%3CInteger, Color%3E colorConfig = new HashMap%3C%3E();%0A%0A    public ImageWriter() %7B%0A        colorConfig.put(0, new Color(32, 85, 126));%0A        colorConfig.put(1, new Color(26, 151, 82));%0A        colorConfig.put(2, new Color(224, 143, 18));%0A        colorConfig.put(3, new Color(210, 43, 40));%0A    %7D%0A%0A    public ImageWriter(Map%3CInteger, Color%3E colorConfig) %7B%0A        this.colorConfig = colorConfig;%0A    %7D
 %0A%0A  
@@ -1600,46 +1600,141 @@
 fix 
+ 
 + 
-System.currentTimeMillis() + %22.png%22;
+%22.png%22;%0A        File resultsDir = new File(%22results%22);%0A        if(!resultsDir.exists())%7B%0A            resultsDir.mkdir();%0A        %7D
 %0A   
@@ -1783,16 +1783,27 @@
 ew File(
+resultsDir,
 fileName
@@ -1829,15 +1829,8 @@
 ate 
-static 
 Colo
@@ -1861,316 +1861,69 @@
     
-switch (i) %7B%0A            case 0:%0A                return new Color(39, 111, 161);%0A            case 1:%0A                return new Color(26, 151, 82);%0A            case 2:%0A                return new Color(224, 143, 18);%0A            case 3:%0A                return new Color(176, 52, 39);%0A            default:%0A    
+Color color = colorConfig.get(i);%0A        if(color == null)%7B%0A
     
@@ -1954,36 +1954,47 @@
 ln(%22
-This should never happen :)%22
+Warning: color not found for value%22 + i
 );%0A 
@@ -1992,32 +1992,34 @@
 %22 + i);%0A        
+%7D%0A
         return n
@@ -2021,42 +2021,14 @@
 urn 
-new Color(39, 111, 161);%0A        %7D
+color;
 %0A   

Allow Color Configuration, write to results directory