package umm3601;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import spark.Request;
import spark.Response;
import umm3601.user.UserController;

import java.io.IOException;

import static spark.Spark.*;
import static spark.debug.DebugScreen.enableDebugScreen;

public class Server {
    private static final String userDatabaseName = "dev";
    private static final int serverPort = 4567;

    public static void main(String[] args) throws IOException {

        MongoClient mongoClient = new MongoClient();
        MongoDatabase userDatabase = mongoClient.getDatabase(userDatabaseName);

        UserController userController = new UserController(userDatabase);

        //Configure Spark
        port(serverPort);
        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);
        get("api/users/:id", userController::getUser);
        post("api/users/new", userController::addNewUser);

        // 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("*", Server::addGzipHeader);

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

    }

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

@@ -125,24 +125,72 @@
 k.Response;%0A
+import spark.Route;%0Aimport spark.utils.IOUtils;%0A
 import umm36
@@ -213,16 +213,16 @@
 roller;%0A
-
 %0Aimport 
@@ -241,16 +241,44 @@
 ception;
+%0Aimport java.io.InputStream;
 %0A%0Aimport
@@ -1807,24 +1807,25 @@
 , %22/%22);%0A
+%0A
         
 redirect
@@ -1820,49 +1820,252 @@
     
-redirect.get(%22/%22, %22http://localhost:9000%22
+Route clientRoute = (req, res) -%3E %7B%0A            //Return client files%0A            InputStream stream = Server.class.getResourceAsStream(%22/public/index.html%22);%0A            return IOUtils.toString(stream);%0A        %7D;%0A%0A        get(%22/%22, clientRoute
 );%0A%0A
@@ -3069,24 +3069,57 @@
 ipHeader);%0A%0A
+        get(%22/*%22, clientRoute);%0A%0A
         // H
@@ -3299,16 +3299,16 @@
 that!%22;%0A
-
         
@@ -3308,24 +3308,26 @@
        %7D);%0A%0A
+%0A%0A
     %7D%0A%0A    /

Add client route resolves issue #8 on production-template repo