package massim.monitor;

import massim.protocol.WorldData;
import massim.protocol.scenario.city.data.DynamicCityData;
import massim.protocol.scenario.city.data.StaticCityData;

import org.webbitserver.WebServers;
import org.webbitserver.WebServer;
import org.webbitserver.handler.StaticFileHandler;
import org.webbitserver.handler.HttpToWebSocketHandler;

import org.json.JSONObject;
import org.json.JSONArray;

import java.util.concurrent.ExecutionException;

/**
 * The web monitor for the MASSim server.
 */
public class Monitor {

    private final SocketHandler socketHandler = new SocketHandler();

    /**
     * Constructor.
     * Used by the massim server to create the "live" monitor.
     */
    public Monitor() throws ExecutionException, InterruptedException {
        WebServer server = WebServers.createWebServer(7777)
            .add("/socket", new HttpToWebSocketHandler(this.socketHandler))
            .add(new StaticFileHandler("webmonitor/www"))
            .start()
            .get();
    }

    /**
     * Creates a new monitor to watch replays with.
     * @param replayPath the path to a replay file
     */
    Monitor(String replayPath){
        // TODO
    }

    /**
     * Updates the current state of the monitor.
     * Called by the massim server after each step.
     */
    public void updateState(WorldData worldData){
        if (worldData instanceof StaticCityData) {
            updateStaticCityData((StaticCityData) worldData);
        } else if (worldData instanceof DynamicCityData){
            updateDynamicCityData((DynamicCityData) worldData);
        } else{
            System.out.println("Monitor: wrong scenario");
            return;
        }
    }

    private void updateStaticCityData(StaticCityData data) {
        JSONObject d = new JSONObject();
        d.put("simId", data.simId);
        d.put("steps", data.steps);
        d.put("map", data.map);
        d.put("seedCapital", data.seedCapital);
        d.put("teams", data.teams);
        // TODO: roles
        // TODO: items
        this.socketHandler.broadcast(d.toString());
    }

    private void updateDynamicCityData(DynamicCityData data) {
        JSONObject d = new JSONObject();
        d.put("workshops", data.workshops);
        d.put("chargingStations", data.chargingStations);
        d.put("shops", data.shops);
        d.put("dumps", data.dumps);
        d.put("resourceNodes", data.resourceNodes);
        d.put("storages", data.storages);
        this.socketHandler.broadcast(d.toString());
    }
}

@@ -1600,18 +1600,19 @@
   %7D else
+ 
 %7B%0A
-
         
@@ -2001,22 +2001,35 @@
     
-// TODO: 
+d.put(%22roles%22, data.
 roles
+);
 %0A   
@@ -2037,22 +2037,35 @@
     
-// TODO: 
+d.put(%22items%22, data.
 items
+);
 %0A   
@@ -2446,24 +2446,24 @@
 urceNodes);%0A
-
         d.pu
@@ -2488,24 +2488,66 @@
 .storages);%0A
+        d.put(%22entities%22, data.entities);%0A
         this

send more data to the client