package com.conveyal.taui.controllers;

import com.conveyal.r5.common.JsonUtilities;
import com.conveyal.taui.models.Modification;
import com.conveyal.taui.models.Scenario;
import com.conveyal.taui.persistence.Persistence;
import com.conveyal.taui.util.JsonUtil;
import com.fasterxml.jackson.core.JsonParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spark.Request;
import spark.Response;

import java.io.IOException;
import java.util.Collection;

import static spark.Spark.halt;
import static spark.Spark.get;
import static spark.Spark.post;
import static spark.Spark.put;
import static spark.Spark.options;

/**
 * Controller for persisting modifications.
 */
public class ModificationController {
    private static final Logger LOG = LoggerFactory.getLogger(ModificationController.class);

    public static Modification getModification (Request req, Response res) {
        String id = req.params("id");
        return Persistence.modifications.get(id);
    }

    public static Modification createOrUpdate (Request req, Response res) {
        Modification mod = null;
        try {
            mod = JsonUtilities.objectMapper.readValue(req.body(), Modification.class);

            if (mod.scenario == null) halt(404);
            Scenario scenario = Persistence.scenarios.get(mod.scenario);

            if (scenario == null || !req.attribute("group").equals(scenario.group)) halt(404);


        } catch (IOException e) {
            LOG.info("Error parsing modification JSON from client", e);
            halt(400, "Bad modification");
        }

        Persistence.modifications.put(mod.id, mod);

        return mod;
    }

    public static void register () {
        get("/api/modification/:id", ModificationController::getModification, JsonUtil.objectMapper::writeValueAsString);
        post("/api/modification", ModificationController::createOrUpdate, JsonUtil.objectMapper::writeValueAsString);
        // option to get any configured cors headers
        options("/api/modification", (q, s) -> "");
        put("/api/modification/:id", ModificationController::createOrUpdate, JsonUtil.objectMapper::writeValueAsString);
        options("/api/modification/:id", (q, s) -> "");
    }
}

@@ -471,16 +471,50 @@
 ction;%0A%0A
+import static spark.Spark.delete;%0A
 import s
@@ -1687,32 +1687,422 @@
 urn mod;%0A    %7D%0A%0A
+    public static Modification deleteModification (Request req, Response res) %7B%0A        Modification m = Persistence.modifications.get(req.params(%22id%22));%0A        if (m == null) halt(404);%0A%0A        Scenario s = Persistence.scenarios.get(m.scenario);%0A%0A        if (s == null %7C%7C !req.attribute(%22group%22).equals(s.group)) halt(404);%0A%0A        return Persistence.modifications.remove(m.id);%0A    %7D%0A%0A
     public stati
@@ -2640,20 +2640,148 @@
  s) -%3E %22%22);%0A
+        delete(%22/api/modification/:id%22, ModificationController::deleteModification, JsonUtil.objectMapper::writeValueAsString);%0A
     %7D%0A%7D%0A

add delete to modificationcontroller.