package org.oddox;

import org.oddox.action.AuthorAction;
import org.oddox.action.AuthorsAction;
import org.oddox.action.BlogAction;
import org.oddox.action.CategoriesAction;
import org.oddox.action.CategoryAction;
import org.oddox.action.PostAction;
import org.oddox.action.RssAction;
import org.oddox.action.SearchAction;
import org.oddox.action.TagAction;
import org.oddox.action.TagsAction;
import org.oddox.action.YearAction;
import org.oddox.action.YearsAction;
import org.oddox.action.api.ForgotAction;
import org.oddox.action.api.HealthAction;
import org.oddox.action.api.RootAction;
import org.oddox.action.filter.DynamicContentFilter;
import org.oddox.action.filter.StaticContentFilter;
import org.oddox.action.interceptor.AppInterceptor;
import org.oddox.action.interceptor.ArchiveInterceptor;

import io.vertx.reactivex.core.Vertx;
import io.vertx.reactivex.ext.web.Router;
import io.vertx.reactivex.ext.web.handler.StaticHandler;

/**
 * WebRoutes creates the http route paths for actions.
 * 
 * @author amdelamar
 * @date 05/28/2018
 */
public final class WebRoutes {

    private WebRoutes() {
        // prevent instantiation
    }

    /**
     * Creates and maps all web router access points.
     * @param vertx Vertx
     * @return Router
     */
    public static Router newRouter(Vertx vertx) {
        // Map All Routes
        Router main = Router.router(vertx);

        // Static Resources
        // Readonly + nocache, so any changes in webroot are visible on browser refresh
        // for production, these wouldn't be needed.
        main.route("/*")
                .handler(StaticHandler.create()
                        .setAllowRootFileSystemAccess(false)
                        .setAlwaysAsyncFS(true)
                        .setFilesReadOnly(true)
                        .setMaxAgeSeconds(31536000l)
                        .setCachingEnabled(true)
                        .setCacheEntryTimeout(31536000l));

        // Filters for HTTP headers
        main.route("/**.*")
                .handler(new StaticContentFilter());
        main.route("/*")
                .handler(new DynamicContentFilter());

        // Interceptors for setting Context attributes
        main.route("/*")
                .handler(new AppInterceptor());
        main.route("/*")
                .handler(new ArchiveInterceptor());

        // Template Actions

        // Blog
        main.route("/")
                .handler(new BlogAction());
        main.route("/blog")
                .handler(new BlogAction());
        main.route("/page/:page")
                .handler(new BlogAction());
        main.route("/blog/:post")
                .handler(new PostAction());

        // Author
        main.route("/author/:author")
                .handler(new AuthorAction());
        main.route("/author")
                .handler(new AuthorsAction());

        // Category
        main.route("/category/:category")
                .handler(new CategoryAction());
        main.route("/category/:category/page/:page")
                .handler(new CategoryAction());
        main.route("/category")
                .handler(new CategoriesAction());

        // Tag
        main.route("/tag/:tag")
                .handler(new TagAction());
        main.route("/tag/:tag/page/:page")
                .handler(new TagAction());
        main.route("/tag")
                .handler(new TagsAction());

        // Year
        main.route("/year/:year")
                .handler(new YearAction());
        main.route("/year/:year/page/:page")
                .handler(new YearAction());
        main.route("/year")
                .handler(new YearsAction());

        // Other
        main.route("/rss")
                .handler(new RssAction());
        main.route("/search")
                .handler(new SearchAction());
        
        // APIs
        main.route()
                .path("/api")
                .handler(new RootAction());
        main.route()
                .path("/api/forgot")
                .handler(new ForgotAction());
        main.route()
                .path("/api/health")
                .handler(new HealthAction());

        return main;
    }

}

@@ -875,24 +875,82 @@
 web.Router;%0A
+import io.vertx.reactivex.ext.web.handler.FaviconHandler;%0A
 import io.ve
@@ -1480,273 +1480,144 @@
     
-// Readonly + nocache, so any changes in webroot are visible on browser refresh%0A        // for production, these wouldn't be needed.%0A        main.route(%22/*%22)%0A                .handler(StaticHandler.create()%0A                        .setAllowRootFileSystemAccess(false
+main.routeWithRegex(%22(.+)%5B.%5D(css%7Cjs%7Chtm%7Chtml%7Ctxt%7Cmd%7Ccsv%7Cjpg%7Cjpeg%7Cpng%7Ctff%7Cwoff%7Ceot%7Csvg)$%22)%0A                .handler(StaticHandler.create(
 )%0A  
@@ -1870,16 +1870,102 @@
 6000l));
+%0A        main.route(%22/favicon.ico%22)%0A                .handler(FaviconHandler.create());
 %0A%0A      
@@ -3845,24 +3845,16 @@
 ion());%0A
-        
 %0A       

Fix StaticHandler with regex