package com.otognan.driverpete.logic.routes;

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

import com.otognan.driverpete.logic.Location;

public class RoutesFinder {
    
    private List<List<Location>> AtoBRoutes = new ArrayList<List<Location>>();
    private List<List<Location>> BtoARoutes = new ArrayList<List<Location>>();
    private List<Location> currentRoute = new ArrayList<Location>();
    private List<Location> endpoints;
    private double distanceToStartRoute;
    private double continuityThreshold;
    private Integer fromEndpointIndex;
    
    public RoutesFinder(List<Location> endpoints, double distanceToStartRoute, double continuityThreshold) throws Exception
    {
        if (endpoints.size() != 2) {
            throw new Exception("Exactly 2 endpoints are expected");
        }
        this.endpoints = endpoints;
        this.distanceToStartRoute = distanceToStartRoute;
        this.continuityThreshold = continuityThreshold;
        this.fromEndpointIndex = null;
    }
    
    public RoutesFinder(List<Location> endpoints) throws Exception {
        this(endpoints, 200, 60*10);
    }
    
    private void startRoute(Location location, Integer endpointIndex) {
        this.fromEndpointIndex = endpointIndex;
        this.currentRoute.add(location);
    }
    
    private Integer toEndpointIndex() {
        return (this.fromEndpointIndex + 1) % 2;
    }
    
    private void stopRoute() {
        this.currentRoute = new ArrayList<Location>();
        this.fromEndpointIndex = null;
    }
    
    private Integer closestEndpointIndex(Location point) {
        for (int i = 0; i < this.endpoints.size(); i++) {
            if (Location.distance(point, this.endpoints.get(i)) < this.distanceToStartRoute) {
                return i;
            }
        }
        return null;
    }
    
    public void process(Location point) {
        if (this.fromEndpointIndex == null) {
            Integer index = this.closestEndpointIndex(point);
            if (index != null) {
                this.startRoute(point, index);
            }
            return;
        } else {
            double dt = Location.deltaTime(
                    this.currentRoute.get(this.currentRoute.size()-1), point);
            if (dt > this.continuityThreshold) {
                this.stopRoute();
                return;
            }
            
            this.currentRoute.add(point);
            Integer index = this.closestEndpointIndex(point);
            if (index == this.fromEndpointIndex) {
                this.stopRoute();
                this.startRoute(point, index);
            } else if (index == this.toEndpointIndex()) {
                if (this.fromEndpointIndex == 0) {
                    this.AtoBRoutes.add(this.currentRoute);
                    this.stopRoute();
                } else {
                    this.BtoARoutes.add(this.currentRoute);
                    this.stopRoute();
                }
            }
        }
    }
    
    
    public List<List<Location>> getAtoBRoutes() {
        return this.AtoBRoutes;
    }
    
    public List<List<Location>> getBtoARoutes() {
        return this.BtoARoutes;
    }
}

@@ -333,77 +333,8 @@
 ();%0A
-    private List%3CLocation%3E currentRoute = new ArrayList%3CLocation%3E();%0A
     
@@ -452,31 +452,122 @@
 ld;%0A    
-private Integer
+%0A    // Current state%0A    private List%3CLocation%3E currentRoute = new ArrayList%3CLocation%3E();%0A    private int
  fromEnd
@@ -572,24 +572,29 @@
 ndpointIndex
+ = -1
 ;%0A    %0A    p
@@ -1009,36 +1009,34 @@
 EndpointIndex = 
-null
+-1
 ;%0A    %7D%0A    %0A   
@@ -1196,23 +1196,19 @@
 cation, 
-I
+i
 nt
-eger
  endpoin
@@ -1329,23 +1329,19 @@
 private 
-I
+i
 nt
-eger
  toEndpo
@@ -1532,20 +1532,18 @@
 Index = 
-null
+-1
 ;%0A    %7D%0A
@@ -1559,23 +1559,19 @@
 private 
-I
+i
 nt
-eger
  closest
@@ -1820,20 +1820,18 @@
  return 
-null
+-1
 ;%0A    %7D%0A
@@ -1915,20 +1915,18 @@
 ndex == 
-null
+-1
 ) %7B%0A    
@@ -1925,39 +1925,35 @@
 ) %7B%0A            
-I
+i
 nt
-eger
  index = this.cl
@@ -2008,12 +2008,10 @@
  != 
-null
+-1
 ) %7B%0A
@@ -2423,15 +2423,11 @@
     
-I
+i
 nt
-eger
  ind

Integer to int