// Path: src/main/java/org/iotlabs/dashboard/blueprints/AbstractBluePrint.java
// public abstract class AbstractBluePrint {
// 
//     /**
//      * blueprint name prefix
//      */
//     private String blueprintPrefix;
//     private String blueprintPrefixFormatter;
//     private String blueprintResourcePath;
// 
//     /**
//      * create blueprint object
//      * @param blueprintPrefix blueprint name prefix to set
//      */
//     public AbstractBluePrint(String blueprintPrefix) {
//         if (!blueprintPrefix.startsWith("/")) {
//             blueprintPrefix = "/" + blueprintPrefix;
//         }
//         if (blueprintPrefix.endsWith("/")) {
//             blueprintPrefix = blueprintPrefix.substring(0, blueprintPrefix.length() - 1);
//         }
//         this.blueprintPrefix = blueprintPrefix;
//         this.blueprintPrefixFormatter = blueprintPrefix + "/%s";
//         this.blueprintResourcePath = "web/html/" + blueprintPrefix;
//     }
// 
//     /**
//      * Get endpoint with blueprint prefix
//      * @param endPoint endpoint
//      * @return {blueprint_prefix}/endPoint
//      */
//     public String getEndPoint(String endPoint) {
// 
//         if (StringUtils.isEmpty(endPoint)) {
//             return blueprintPrefix;
//         }
// 
//         if (endPoint.startsWith("/")) {
//             endPoint = endPoint.substring(1, endPoint.length());
//         }
//         return String.format(blueprintPrefixFormatter, endPoint);
//     }
// 
//     /**
//      * Get template resource path for Mustache engine.
//      * @return "web/html/{blue_print}"
//      */
//     public String getTemplatePath() {
//         return blueprintResourcePath;
//     }
// 
//     public abstract void register();
// }
// 
// Path: src/main/java/org/iotlabs/dashboard/blueprints/AuthBluePrint.java
// public class AuthBluePrint extends AbstractBluePrint {
// 
// 
//     public AuthBluePrint() {
//         super("auth");
//     }
// 
//     @Override
//     public void register() {
//         complex(getEndPoint("login"))
//                 .get(
//                         (req, res) -> {
//                             Map<String, Object> scopes = new HashMap<>();
//                             scopes.put("title", "IotLabs");
//                             scopes.put("admin_mail", "toori67@gmail.com");
//                             return new ModelAndView(scopes,"login.html");
//                         }, new MustacheTemplateEngine(getTemplatePath())
//                 )
//                 .post((req, res) -> {
//                     String username = req.queryParams("username");
//                     String passwd = StringUtils.sha256(req.queryParams("password"));
//                     User user = Checker.authenticate(username, passwd);
//                     if (user == null) {
//                         req.session().removeAttribute("auth");
//                         ResponseSetter.setErrorResponse(res, new Http4xx(404, "not authorized"));
//                         return res.body();
//                     } else {
//                         if (Checker.isRequestingHtml(req)) {
//                             res.redirect("/");
//                         }
//                         req.session().attribute("auth", true);
//                         res.type(ContentType.JSON);
//                         res.body(user.toJsonString());
//                         res.status(200);
//                         return res.body();
//                     }
//                 });
//     }
// }
// 
// Path: src/main/java/org/iotlabs/dashboard/blueprints/RootBluePrint.java
// public class RootBluePrint extends AbstractBluePrint {
// 
//     public RootBluePrint() {
//         super("");
//     }
// 
//     @Override
//     public void register() {
//         get("/", (req, res) -> "Welcome :)");
//     }
// }


import org.iotlabs.dashboard.blueprints.AbstractBluePrint;
import org.iotlabs.dashboard.blueprints.AuthBluePrint;
import org.iotlabs.dashboard.blueprints.RootBluePrint;
package org.iotlabs.dashboard;


public class SparkProxy {
    private static class Holder {
        private static final SparkProxy SPARK_PROXY = new SparkProxy();
    }

    public static SparkProxy getInstance() {
        return Holder.SPARK_PROXY;
    }

    private SparkProxy() {
        new SparkInit();
    }

    public void run() {
        registerRouters();
    }

    /**
     * register default setted routers.
     */
    private void registerRouters() {
// Path: src/main/java/org/iotlabs/dashboard/blueprints/AbstractBluePrint.java
// public abstract class AbstractBluePrint {
// 
//     /**
//      * blueprint name prefix
//      */
//     private String blueprintPrefix;
//     private String blueprintPrefixFormatter;
//     private String blueprintResourcePath;
// 
//     /**
//      * create blueprint object
//      * @param blueprintPrefix blueprint name prefix to set
//      */
//     public AbstractBluePrint(String blueprintPrefix) {
//         if (!blueprintPrefix.startsWith("/")) {
//             blueprintPrefix = "/" + blueprintPrefix;
//         }
//         if (blueprintPrefix.endsWith("/")) {
//             blueprintPrefix = blueprintPrefix.substring(0, blueprintPrefix.length() - 1);
//         }
//         this.blueprintPrefix = blueprintPrefix;
//         this.blueprintPrefixFormatter = blueprintPrefix + "/%s";
//         this.blueprintResourcePath = "web/html/" + blueprintPrefix;
//     }
// 
//     /**
//      * Get endpoint with blueprint prefix
//      * @param endPoint endpoint
//      * @return {blueprint_prefix}/endPoint
//      */
//     public String getEndPoint(String endPoint) {
// 
//         if (StringUtils.isEmpty(endPoint)) {
//             return blueprintPrefix;
//         }
// 
//         if (endPoint.startsWith("/")) {
//             endPoint = endPoint.substring(1, endPoint.length());
//         }
//         return String.format(blueprintPrefixFormatter, endPoint);
//     }
// 
//     /**
//      * Get template resource path for Mustache engine.
//      * @return "web/html/{blue_print}"
//      */
//     public String getTemplatePath() {
//         return blueprintResourcePath;
//     }
// 
//     public abstract void register();
// }
// 
// Path: src/main/java/org/iotlabs/dashboard/blueprints/AuthBluePrint.java
// public class AuthBluePrint extends AbstractBluePrint {
// 
// 
//     public AuthBluePrint() {
//         super("auth");
//     }
// 
//     @Override
//     public void register() {
//         complex(getEndPoint("login"))
//                 .get(
//                         (req, res) -> {
//                             Map<String, Object> scopes = new HashMap<>();
//                             scopes.put("title", "IotLabs");
//                             scopes.put("admin_mail", "toori67@gmail.com");
//                             return new ModelAndView(scopes,"login.html");
//                         }, new MustacheTemplateEngine(getTemplatePath())
//                 )
//                 .post((req, res) -> {
//                     String username = req.queryParams("username");
//                     String passwd = StringUtils.sha256(req.queryParams("password"));
//                     User user = Checker.authenticate(username, passwd);
//                     if (user == null) {
//                         req.session().removeAttribute("auth");
//                         ResponseSetter.setErrorResponse(res, new Http4xx(404, "not authorized"));
//                         return res.body();
//                     } else {
//                         if (Checker.isRequestingHtml(req)) {
//                             res.redirect("/");
//                         }
//                         req.session().attribute("auth", true);
//                         res.type(ContentType.JSON);
//                         res.body(user.toJsonString());
//                         res.status(200);
//                         return res.body();
//                     }
//                 });
//     }
// }
// 
// Path: src/main/java/org/iotlabs/dashboard/blueprints/RootBluePrint.java
// public class RootBluePrint extends AbstractBluePrint {
// 
//     public RootBluePrint() {
//         super("");
//     }
// 
//     @Override
//     public void register() {
//         get("/", (req, res) -> "Welcome :)");
//     }
// }


// Path: src/main/java/org/iotlabs/dashboard/SparkProxy.java
import org.iotlabs.dashboard.blueprints.AbstractBluePrint;
import org.iotlabs.dashboard.blueprints.AuthBluePrint;
import org.iotlabs.dashboard.blueprints.RootBluePrint;

package org.iotlabs.dashboard;


public class SparkProxy {
    private static class Holder {
        private static final SparkProxy SPARK_PROXY = new SparkProxy();
    }

    public static SparkProxy getInstance() {
        return Holder.SPARK_PROXY;
    }

    private SparkProxy() {
        new SparkInit();
    }

    public void run() {
        registerRouters();
    }

    /**
     * register default setted routers.
     */
    private void registerRouters() {
        new AuthBluePrint().register();