package umm3601;

import static spark.Spark.*;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import spark.Filter;
import com.google.gson.Gson;
import spark.Request;
import spark.Response;
import umm3601.todo.ToDoController;
import umm3601.user.Database;
import umm3601.user.UserController;

import java.io.IOException;
import static spark.debug.DebugScreen.*;


public class Server {
    public static final String userDatabaseName = "src/main/data/users.json";
    private static MongoClient mongoClient;
    private static MongoDatabase userDatabase;

    public static void main(String[] args) throws IOException {
        final Gson gson = new Gson();

        mongoClient = new MongoClient(); //Default port
        userDatabase = mongoClient.getDatabase(userDatabaseName);

        UserController userController = buildUserController();

        //Configure Spark
        port(4567);
        enableDebugScreen();

        // Specify where assets like images will be "stored"
        staticFiles.location("/public");

        options("/*", (request, response) -> {

            String accessControlRequestHeaders = request.headers("Access-Control-Request-Headers");
            if (accessControlRequestHeaders != null) {
                response.header("Access-Control-Allow-Headers", accessControlRequestHeaders);
            }

            String accessControlRequestMethod = request.headers("Access-Control-Request-Method");
            if (accessControlRequestMethod != null) {
                response.header("Access-Control-Allow-Methods", accessControlRequestMethod);
            }

            return "OK";
        });

        before((request, response) -> response.header("Access-Control-Allow-Origin", "*"));




        // Simple example route
        get("/hello", (req, res) -> "Hello World");

        // Redirects for the "home" page
        redirect.get("", "/");
        redirect.get("/", "http://localhost:9000");

        /// User Endpoints ///////////////////////////
        /////////////////////////////////////////////

        //List users, filtered using query parameters
        get("api/users", userController::getUsers);

        // See specific user
        get("api/users/:id", userController::getUser);


        // An example of throwing an unhandled exception so you can see how the
        // Java Spark debugger displays errors like this.
        get("api/error", (req, res) -> {
            throw new RuntimeException("A demonstration error");
        });

        // Called after each request to insert the GZIP header into the response.
        // This causes the response to be compressed _if_ the client specified
        // in their request that they can accept compressed responses.
        // There's a similar "before" method that can be used to modify requests
        // before they they're processed by things like `get`.
        after("*", addGzipHeader);

        // Handle "404" file not found requests:
        notFound((req, res) -> {
            res.type("text");
            res.status(404);
            return "Sorry, we couldn't find that!";
        });

    }

    /***
     * Create a database using the json fie, use it as
     * data source for a new UserController
     *
     * Constructing the controller might throw an IOException if
     * there are problems reading from the JSON "database" file.
     * If that happens we'll print out an error message and shut
     * the server down.
     * @throws IOException if we can't open or read the user data file
     */
    private static UserController buildUserController() {
        UserController userController = null;

        try {
            userController = new UserController(userDatabase);
        } catch (IOException e) {
            System.err.println("The server failed to load the user data; shutting down.");
            e.printStackTrace(System.err);

            // Shut the server down
            stop();
            System.exit(1);
        }

        return userController;
    }

    // Enable GZIP for all responses
    private static Filter addGzipHeader = (Request request, Response response) -> {
        response.header("Content-Encoding", "gzip");
    };
}

@@ -73,16 +73,51 @@
 Client;%0A
+import com.mongodb.MongoException;%0A
 import c
@@ -145,24 +145,24 @@
 goDatabase;%0A
-
 import spark
@@ -248,44 +248,8 @@
 se;%0A
-import umm3601.todo.ToDoController;%0A
 impo
@@ -459,33 +459,60 @@
  = %22
-src/main/data/users.json%22
+test%22;%0A    public static final int serverPort = 4567
 ;%0A  
@@ -601,16 +601,17 @@
 abase;%0A%0A
+%0A
     publ
@@ -669,46 +669,8 @@
 on %7B
-%0A        final Gson gson = new Gson();
 %0A%0A  
@@ -830,21 +830,20 @@
 oller = 
-build
+new 
 UserCont
@@ -849,16 +849,28 @@
 troller(
+userDatabase
 );%0A%0A    
@@ -904,20 +904,26 @@
    port(
-4567
+serverPort
 );%0A     
@@ -3155,903 +3155,8 @@
  %7D%0A%0A
-    /***%0A     * Create a database using the json fie, use it as%0A     * data source for a new UserController%0A     *%0A     * Constructing the controller might throw an IOException if%0A     * there are problems reading from the JSON %22database%22 file.%0A     * If that happens we'll print out an error message and shut%0A     * the server down.%0A     * @throws IOException if we can't open or read the user data file%0A     */%0A    private static UserController buildUserController() %7B%0A        UserController userController = null;%0A%0A        try %7B%0A            userController = new UserController(userDatabase);%0A        %7D catch (IOException e) %7B%0A            System.err.println(%22The server failed to load the user data; shutting down.%22);%0A            e.printStackTrace(System.err);%0A%0A            // Shut the server down%0A            stop();%0A            System.exit(1);%0A        %7D%0A%0A        return userController;%0A    %7D
 %0A%0A  

Refactor Server.java, correct userDatabaseName