package net.acomputerdog.map.overlay;

import ar.com.hjg.pngj.ImageLineInt;
import net.acomputerdog.map.image.ImageUtils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class ShapeOverlay extends Overlay {

    private static final int RECTANGLE  = 0;
    private static final int OVAL = 1;
    private static final int RECTANGLE_FILLED = 2;
    private static final int OVAL_FILLED = 3;

    private static final Color COLOR_DEFAULT = Color.BLACK;

    private final BufferedImage image;

    private int currRow = 0;

    public ShapeOverlay(File source) throws IOException {
        super(source);
        int type = getType(source);
        Color color = getColor(source);
        this.image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics = image.createGraphics();
        graphics.setColor(color);
        try {
            switch (type) {
                case RECTANGLE:
                    graphics.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
                    break;
                case OVAL:
                    graphics.drawOval(0, 0, getWidth() - 1, getHeight() - 1);
                    break;
                case RECTANGLE_FILLED:
                    graphics.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
                    break;
                case OVAL_FILLED:
                    graphics.fillOval(0, 0, getWidth() - 1, getHeight() - 1);
                    break;
                default:
                    throw new RuntimeException("Invalid shape type!");
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            //shouldn't happen, but there in case of future changes
            throw new RuntimeException("Invalid arguments for shape!");
        }
    }

    private int getType(File source) throws IOException {
        try (BufferedReader reader = new BufferedReader(new FileReader(new File(source, "shape.txt")))) {
            String type = reader.readLine().toLowerCase();
            switch (type) {
                case "rectangle":
                    return RECTANGLE;
                case "oval":
                    return OVAL;
                case "rectangle_filled":
                    return RECTANGLE_FILLED;
                case "oval_filled":
                    return OVAL_FILLED;
                default:
                    throw new IllegalArgumentException("Unknown shape: " + type);
            }
        } catch (Exception e) {
            throw new RuntimeException("Exception reading shape type!", e);
        }
    }

    private Color getColor(File source) {
        File colorFile = new File(source, "color.txt");
        if (colorFile.isFile()) {
            try (BufferedReader reader = new BufferedReader(new FileReader(colorFile))) {
                int col = Integer.parseInt(reader.readLine());
                return new Color(col);
            } catch (Exception e) {
                throw new RuntimeException("Exception reading shape color!", e);
            }
        } else {
            return COLOR_DEFAULT;
        }
    }

    @Override
    public void apply(int row, ImageLineInt line) {
        if (currRow >= image.getHeight()) {
            throw new IllegalStateException("Attempted to draw overlay too many times: " + currRow);
        }
        ImageUtils.copyImageToPng(image, currRow, line, getXLoc(), true);
        currRow++;
    }
}

@@ -187,17 +187,106 @@
 java.io.
-*
+BufferedReader;%0Aimport java.io.File;%0Aimport java.io.FileReader;%0Aimport java.io.IOException
 ;%0A%0Apubli
@@ -325,16 +325,45 @@
 rlay %7B%0A%0A
+    //todo replace with enum%0A
     priv
@@ -534,16 +534,55 @@
 LED = 3;
+%0A    private static final int LINE = 4;
 %0A%0A    pr
@@ -1049,26 +1049,8 @@
 r);%0A
-        try %7B%0A    
     
@@ -1073,36 +1073,32 @@
 ) %7B%0A            
-    
 case RECTANGLE:%0A
@@ -1097,20 +1097,16 @@
 TANGLE:%0A
-    
         
@@ -1183,39 +1183,31 @@
             
-    
 break;%0A
-    
             
@@ -1225,36 +1225,32 @@
                 
-  
-  
 graphics.drawOva
@@ -1299,36 +1299,32 @@
                 
-    
 break;%0A         
@@ -1318,36 +1318,32 @@
 ak;%0A            
-   
- 
 case RECTANGLE_F
@@ -1341,36 +1341,32 @@
 CTANGLE_FILLED:%0A
-    
                 
@@ -1415,36 +1415,32 @@
 tHeight() - 1);%0A
-    
                 
@@ -1450,36 +1450,32 @@
 ak;%0A            
-    
 case OVAL_FILLED
@@ -1468,36 +1468,32 @@
 se OVAL_FILLED:%0A
-    
                 
@@ -1558,36 +1558,32 @@
                 
-    
 break;%0A         
@@ -1581,35 +1581,33 @@
             
-    default
+case LINE
 :%0A          
@@ -1616,60 +1616,63 @@
     
-    throw new RuntimeException(%22Invalid shape type!%22
+graphics.drawLine(0, 0, getWidth() - 1, getHeight() - 1
 );%0A 
@@ -1686,131 +1686,44 @@
     
-%7D%0A
     
-    %7D catch (ArrayIndexOutOfBoundsException e) %7B%0A            //shouldn't happen, but there in case of future changes%0A
+break;%0A            default:%0A    
     
@@ -1770,25 +1770,16 @@
 lid 
-arguments for sha
+shape ty
 pe!%22
@@ -2297,32 +2297,32 @@
  %22oval_filled%22:%0A
-
                 
@@ -2337,32 +2337,94 @@
 rn OVAL_FILLED;%0A
+                case %22line%22:%0A                    return LINE;%0A
                 

Add support for lines to ShapeOverlay