package valandur.webapi.servlet.info;

import com.fasterxml.jackson.databind.JsonNode;
import org.eclipse.jetty.http.HttpMethod;
import org.spongepowered.api.Platform;
import org.spongepowered.api.Server;
import org.spongepowered.api.Sponge;
import valandur.webapi.api.annotation.WebAPIEndpoint;
import valandur.webapi.api.annotation.WebAPIServlet;
import valandur.webapi.api.servlet.WebAPIBaseServlet;
import valandur.webapi.servlet.ServletData;

import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;

@WebAPIServlet(basePath = "info")
public class InfoServlet extends WebAPIBaseServlet {

    @WebAPIEndpoint(method = HttpMethod.GET, path = "/", perm = "get")
    public void getInfo(ServletData data) {
        Server server = Sponge.getServer();
        Platform platform = Sponge.getPlatform();

        data.addJson("ok", true, false);

        data.addJson("motd", server.getMotd().toPlain(), false);
        data.addJson("players", server.getOnlinePlayers().size(), false);
        data.addJson("maxPlayers", server.getMaxPlayers(), false);
        data.addJson("address", server.getBoundAddress().map(Object::toString).orElse(null), false);
        data.addJson("onlineMode", server.getOnlineMode(), false);
        data.addJson("resourcePack", server.getDefaultResourcePack().orElse(null), false);
        data.addJson("hasWhitelist", server.hasWhitelist(), false);

        data.addJson("uptimeTicks", server.getRunningTimeTicks(), false);
        data.addJson("tps", server.getTicksPerSecond(), false);
        data.addJson("minecraftVersion", platform.getMinecraftVersion().getName(), false);

        data.addJson("game", platform.getContainer(Platform.Component.GAME), true);
        data.addJson("api", platform.getContainer(Platform.Component.API), true);
        data.addJson("implementation", platform.getContainer(Platform.Component.IMPLEMENTATION), true);
    }

    @WebAPIEndpoint(method = HttpMethod.GET, path = "/properties", perm = "properties")
    public void getProperties(ServletData data) {
        data.addJson("ok", true, false);
        data.addJson("properties", serverService.getProperties(), true);
    }

    @WebAPIEndpoint(method = HttpMethod.GET, path = "/properties", perm = "properties")
    public void setProperties(ServletData data) {
        JsonNode body = data.getRequestBody();
        JsonNode props = body.get("properties");

        if (props == null) {
            data.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid new properties");
            return;
        }

        for (Iterator<String> it = props.fieldNames(); it.hasNext(); ) {
            String key = it.next();
            serverService.setProperty(key, props.get(key).asText());
        }

        data.addJson("ok", true, false);
        data.addJson("properties", serverService.getProperties(), true);
    }

    @WebAPIEndpoint(method = HttpMethod.GET, path="/tps", perm = "tps")
    public void getTps(ServletData data) {
        data.addJson("ok", true, false);
        data.addJson("tps", serverService.getAverageTps(), false);
    }

    @WebAPIEndpoint(method = HttpMethod.GET, path="/player", perm = "players")
    public void getPlayers(ServletData data) {
        data.addJson("ok", true, false);
        data.addJson("players", serverService.getOnlinePlayers(), false);
    }
}

@@ -2188,34 +2188,35 @@
 od = HttpMethod.
-GE
+POS
 T, path = %22/prop

Fix saving properties not working