package com.github.bot.curiosone.api;

import com.github.bot.curiosone.core.workflow.Logic;
import com.github.bot.curiosone.core.workflow.Message;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spark.Spark;

/**
 * Represents the entry point of the Curiosone APIs.
 * Contains the main method, where the server runs.
 */
public class Main {

  /**
  * Logs Main class.
  */
  private static final Logger log = LoggerFactory.getLogger(Main.class);

  /**
   * Default port value.
   */
  private static final int DEFAULT_PORT = 4567;

  /**
   * The entry point of the APIs.
   * This method activates and runs the server.
   * @param args Unused arguments.
   */
  public static void main(String[] args) {

    /**
     * Sets the port where the server run.
     */
    int port = System.getenv("PORT") != null
        ? Integer.parseInt(System.getenv("PORT")) : DEFAULT_PORT;
    Spark.port(port);

    /**
     * Enables CORS
     */
    Spark.before(CorsUtil::headers);
    Spark.options("/*", CorsUtil::options);

    /**
     * Sets the right content type for the response.
     */
    Spark.after(
        (req, res) -> {
          res.type("application/json");
        });

    /*
     * Prints audit logs.
     */
    Spark.after(
        (req, res) -> {
          log.info(AuditUtil.format(req, res));
        });

    /**
     * Responds with ok.
     */
    Spark.get(
        "/status",
        (req, res) -> {
          Map<String, String> map = new HashMap<String, String>();
          map.put("status", "ok");
          return new Gson().toJson(map);
        });

    /**
     * Routes requests to the curiosone core
     */
    Spark.post(
        "/talk",
        (req, res) -> {
          Message user = null;
          Message bot = null;
          try {
            user = new Gson().fromJson(req.body(), Message.class);
          } catch (JsonSyntaxException e) {
            e.printStackTrace();
          }
          bot = Logic.talk(user);

          return new Gson().toJson(bot);
        });
  }
}

@@ -451,16 +451,110 @@
 r runs.%0A
+ * @see  %3Ca href=%22https://spark.apache.org/docs/latest/api/java/index.html%22%3ESpark Javadoc%3C/a%3E%0A
  */%0Apubl
@@ -862,20 +862,34 @@
  @param 
+ 
 args
+%0A   *        
  Unused 
@@ -1595,18 +1595,20 @@
 ds with 
+%22
 ok
+%22
 .%0A     *
@@ -1861,17 +1861,17 @@
  to the 
-c
+C
 uriosone
@@ -1871,17 +1871,17 @@
 riosone 
-c
+C
 ore%0A    

improve API/Main javadoc