package model.areas;

import config.ConfigModel;
import javafx.scene.paint.Color;
import model.GVector;
import model.Position;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by suter1 on 06.10.2016.
 */
public class Obstacle extends Area {
    public static final double EDGE_EXTENDER = 15.0;
    private Position position;
    private ArrayList<Position> corners;
    private ArrayList<Position> vertices = new ArrayList<>();

    /**
     * Created by suter1 on 06.10.2016.
     */
    public Obstacle(Double... points) {
        this.corners = new ArrayList<>();
        this.vertices = new ArrayList<>();

        for (int i = 0; i < points.length; i += 2) {
            this.corners.add(new Position(points[i], points[i + 1]));
        }
        calculateVertices();

        this.position = new Position(points[0], points[1]);
        this.setFill(Color.BLACK);
        this.getPoints().addAll(points);
    }

    public List<Position> getCorners() {
        return this.corners;
    }

    /**
     * Created by suter1 on 06.10.2016.
     */
    public void calculateVertices() {
	    ConfigModel config = ConfigModel.getInstance();
	    Position a, b;
        for (int i = 0; i < this.corners.size(); i++) {
            if (i + 1 >= this.corners.size()) {
                b = this.corners.get(0);
            } else {
                b = this.corners.get(i + 1);
            }
            a = this.corners.get(i);
            GVector c = new GVector(a.getXValue(), a.getYValue(), b.getXValue(), b.getYValue());
            GVector unitVector = c.norm();
            a = unitVector.invert().getEndPosition().multiply(EDGE_EXTENDER).add(a);
            b = unitVector.getEndPosition().multiply(EDGE_EXTENDER).add(b);
            /**
             * check if position in room
             */
	        if (a.getXValue() > 0 && a.getXValue() < config.getRoomWidth() && a.getYValue() > 0 && a.getYValue() < config.getRoomHeight())
		        this.vertices.add(a);
	        if (b.getXValue() > 0 && b.getXValue() < config.getRoomWidth() && b.getYValue() > 0 && b.getYValue() < config.getRoomHeight())
		        this.vertices.add(b);
        }
    }

    public List<Position> getVertices() {
        return this.vertices;
    }
}

@@ -305,10 +305,10 @@
 R = 
-15
+20
 .0;%0A
@@ -518,17 +518,17 @@
 bstacle(
-D
+d
 ouble...
@@ -534,24 +534,47 @@
 . points) %7B%0A
+        super(points);%0A
         this
@@ -909,49 +909,8 @@
 K);%0A
-        this.getPoints().addAll(points);%0A
     
@@ -1050,21 +1050,22 @@
 */%0A    p
-ublic
+rivate
  void ca
@@ -1084,17 +1084,20 @@
 ces() %7B%0A
-%09
+    
     Conf
@@ -1715,32 +1715,33 @@
 TENDER).add(b);%0A
+%0A
             /**%0A
@@ -1740,20 +1740,123 @@
     
-/**%0A        
+if (includes(a)) this.vertices.add(a);%0A            if (includes(b)) this.vertices.add(b);%0A        %7D%0A    %7D%0A%0A    /**%0A
     
@@ -1888,24 +1888,16 @@
 oom%0A
-        
      */%0A
 %09   
@@ -1896,185 +1896,122 @@
  */%0A
-%09
     
-    if (a.getXValue() %3E 0 && a.getXValue() %3C config.getRoomWidth() && a.getYValue() %3E 0 && a.getYValue() %3C config.getRoomHeight())%0A%09%09        this.vertices.add(a
+private boolean includes(Position position) %7B%0A        ConfigModel config = ConfigModel.getInstance(
 );%0A
-%09
         
 if (
@@ -2010,13 +2010,24 @@
     
-if (b
+return (position
 .get
@@ -2037,26 +2037,49 @@
 lue() %3E 0 &&
- b
+%0A                position
 .getXValue()
@@ -2109,111 +2109,116 @@
 ) &&
- b.getYValue() %3E 0 && b.getYValue() %3C config.getRoomHeight())%0A%09%09        this.vertices.add(b);%0A        %7D
+%0A                position.getYValue() %3E 0 &&%0A                position.getYValue() %3C config.getRoomHeight());
 %0A   

remove code duplication, extract to method