package org.joow.codeelevator;

import com.google.common.base.Optional;
import static spark.Spark.*;

import org.joow.elevator.Direction;
import org.joow.elevator.ElevatorController;
import spark.*;

public class CodeElevator {
    private static final String DEFAULT_PORT = "9000";

    private static String lastCall;

    public static void main(String[] args) {
        final int port = Integer.valueOf(Optional.fromNullable(System.getenv("PORT")).or(DEFAULT_PORT));
        setPort(port);

        final ElevatorController elevatorController = new ElevatorController();

        get(new Route("/call") {
            @Override
            public Object handle(Request request, Response response) {
                lastCall = request.url() + " " + request.queryString();
                final int atFloor = Integer.parseInt(request.queryParams("atFloor"));
                final Direction to = Direction.of(request.queryParams("to"));
                elevatorController.callAt(atFloor, to);

                return "";
            }
        });

        get(new Route("/go") {
            @Override
            public Object handle(Request request, Response response) {
                lastCall = request.url() + " " + request.queryString();
                final int floorToGo = Integer.parseInt(request.queryParams("floorToGo"));
                elevatorController.go(floorToGo);

                return "";
            }
        });

        get(new Route("/userHasEntered") {
            @Override
            public Object handle(Request request, Response response) {
                lastCall = request.url() + " " + request.queryString();
                return "";
            }
        });

        get(new Route("/userHasExited") {
            @Override
            public Object handle(Request request, Response response) {
                lastCall = request.url() + " " + request.queryString();
                return "";
            }
        });

        get(new Route("/reset") {
            @Override
            public Object handle(Request request, Response response) {
                lastCall = request.url() + " " + request.queryString();
                elevatorController.reset();

                return "";
            }
        });

        get(new Route("/nextCommand") {
            @Override
            public Object handle(Request request, Response response) {
                System.out.println(getLastCall());
                return elevatorController.nextCommand().toString();
            }
        });
    }

    private static String getLastCall() {
        return lastCall;
    }
}

@@ -281,45 +281,8 @@
 %22;%0A%0A
-    private static String lastCall;%0A%0A
     
@@ -662,80 +662,8 @@
 ) %7B%0A
-                lastCall = request.url() + %22 %22 + request.queryString();%0A
     
@@ -1061,80 +1061,8 @@
 ) %7B%0A
-                lastCall = request.url() + %22 %22 + request.queryString();%0A
     
@@ -1392,80 +1392,8 @@
 ) %7B%0A
-                lastCall = request.url() + %22 %22 + request.queryString();%0A
     
@@ -1581,80 +1581,8 @@
 ) %7B%0A
-                lastCall = request.url() + %22 %22 + request.queryString();%0A
     
@@ -1762,80 +1762,8 @@
 ) %7B%0A
-                lastCall = request.url() + %22 %22 + request.queryString();%0A
     
@@ -2010,43 +2010,18 @@
     
-System.out.println(getLastCall());%0A
+try %7B%0A    
     
@@ -2100,94 +2100,147 @@
     
-%7D%0A        %7D);%0A    %7D%0A%0A
+    %7D catch (Exception e) %7B%0A                
     
+e.
 pri
-vate static String getLastCall() %7B%0A        return lastCall
+ntStackTrace();%0A                %7D%0A%0A                return %22%22;%0A            %7D%0A        %7D)
 ;%0A  

Remove logging.