import java.time.LocalDate;
import java.time.Month;
import java.util.HashMap;
import java.util.Iterator;

import spark.ModelAndView;
import spark.template.velocity.VelocityTemplateEngine;
import static spark.Spark.*;

public class App {
    public static void main(String[] args) {
        staticFileLocation("/public");
        String layout = "templates/layout.vtl";

        ProcessBuilder process = new ProcessBuilder();
        Integer port;
        if (process.environment().get("PORT") != null) {
            port = Integer.parseInt(process.environment().get("PORT"));
        } else {
            port = 4567;
        }

        setPort(port);

        //RESTful ARCHITECTURE
        //Use POST to create something on the server
        //Use GET to retrieve something from the server
        //Use PUT to change or update something on the server
        //Use DELETE to remove or delete something on the server
        //Keep URLs intuitive
        //Each request from client contains all info necessary for that request

        //ROUTES: Home Page

        get("/", (request, response) -> {
            HashMap<String, Object> model = new HashMap<String, Object>();
            model.put("words", Word.getAll());
            model.put("template", "templates/allwords.vtl");
            return new ModelAndView(model, layout);
        }, new VelocityTemplateEngine());

        //ROUTES: Identification of Resources

        get("/new", (request, response) -> {
            HashMap<String, Object> model = new HashMap<String, Object>();
            model.put("template", "templates/new.vtl");
            return new ModelAndView(model, layout);
        }, new VelocityTemplateEngine());

        get("/:id", (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          boolean wordNotAdded = false;
          if (Word.getWord(request.params(":id")) == null) {
            wordNotAdded = true;
          }
          model.put("wordnotadded", wordNotAdded);
          model.put("word", Word.getWord(request.params(":id")));
          model.put("template", "templates/word.vtl");
          return new ModelAndView(model, layout);
        }, new VelocityTemplateEngine());

        //ROUTES: Changing Resources

        post("/", (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          boolean alreadyAdded = false;
          String userWord = request.queryParams("userword");

          if (Word.getWord(userWord) != null) {
            alreadyAdded = true;
          } else {
            Word newWord = new Word(userWord);
          }

          model.put("alreadyadded", alreadyAdded);
          model.put("words", Word.getAll());
          model.put("template", "templates/allwords.vtl");
          return new ModelAndView(model, layout);
      }, new VelocityTemplateEngine());

      post("/:id", (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          Word thisWord = Word.getWord(request.params(":id"));
          String userDefinition = request.queryParams("userdefinition");
          String userExample = request.queryParams("userexample");

          Definition newDefinition = new Definition(userDefinition, userExample);

          thisWord.addDefinition(newDefinition);

          model.put("word", thisWord);
          model.put("template", "templates/word.vtl");
          return new ModelAndView(model, layout);
      }, new VelocityTemplateEngine());

      post("/:id/remove/:rmdef", (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          Word thisWord = Word.getWord(request.params(":id"));
          String removalRequest = request.params(":rmdef");
          String goodRemovalRequest = "";

          for (String word : removalRequest.split("%20")) {
            goodRemovalRequest = goodRemovalRequest + word + " ";
          }

          for (Iterator<Definition> iterator = thisWord.getAllDefinitions().iterator(); iterator.hasNext();) {
            Definition definition = iterator.next();
            System.out.println(definition);
            if (definition.getString().equals(goodRemovalRequest.trim())) {
              iterator.remove();
            }
          }

          model.put("word", thisWord);
          model.put("template", "templates/word.vtl");
          return new ModelAndView(model, layout);
      }, new VelocityTemplateEngine());

      post("/remove/:id", (request, response) -> {
        HashMap<String, Object> model = new HashMap<String, Object>();
        Word.removeWord(request.params(":id"));
        model.put("words", Word.getAll());
        model.put("template", "templates/allwords.vtl");
        return new ModelAndView(model, layout);
      }, new VelocityTemplateEngine());

    }
}

@@ -1,24 +1,97 @@
+import java.io.UnsupportedEncodingException;%0Aimport java.net.URLDecoder;%0A
 import java.time.LocalDa
@@ -3865,19 +3865,22 @@
  String 
-goo
+decode
 dRemoval
@@ -3893,17 +3893,16 @@
 t = %22%22;%0A
-%0A
         
@@ -3907,55 +3907,11 @@
     
-for (String word : removalRequest.split(%22%2520%22))
+try
  %7B%0A 
@@ -3921,19 +3921,22 @@
         
-goo
+decode
 dRemoval
@@ -3949,39 +3949,197 @@
 t = 
-goodRemovalRequest + word + %22 %22
+URLDecoder.decode(removalRequest, %22UTF-8%22);%0A          %7D catch (UnsupportedEncodingException uee) %7B%0A            System.err.println(%22Caught UnsupportedEncodingException: %22 + uee.getMessage())
 ;%0A  
@@ -4407,11 +4407,14 @@
 als(
-goo
+decode
 dRem
@@ -4428,15 +4428,8 @@
 uest
-.trim()
 )) %7B
@@ -4808,45 +4808,389 @@
     
-Word.removeWord(request.params(%22:id%22)
+String removalRequest = request.params(%22:id%22);%0A        String decodedRemovalRequest = %22%22;%0A        try %7B%0A          decodedRemovalRequest = URLDecoder.decode(removalRequest, %22UTF-8%22);%0A        %7D catch (UnsupportedEncodingException uee) %7B%0A          System.err.println(%22Caught UnsupportedEncodingException: %22 + uee.getMessage());%0A        %7D%0A%0A        Word.removeWord(decodedRemovalRequest
 );%0A 

Refactor removal requests to use URLDecoder