import {redirect, Route, SessionAuthMiddleware, api} from '@extollo/lib'
import {Home} from '../controllers/Home.controller'
import {PageView} from '../middlewares/PageView.middleware'
import {Snippets} from '../controllers/Snippets.controller'
import {GoLinks} from '../controllers/GoLinks.controller'
import {Feed} from '../controllers/Feed.controller'
import {LoadSnippet} from '../middlewares/parameters/LoadSnippet.middleware'
import {LoadFeedPosts} from '../middlewares/parameters/LoadFeedPosts.middleware'
import {ValidContactForm} from '../middlewares/parameters/ValidContactForm.middleware'
import {RateLimit} from '../middlewares/RateLimit.middleware'
import {SiteTheme} from '../middlewares/SiteTheme.middleware'
import {Blog} from '../controllers/Blog.controller'
import {MarkMark} from '../controllers/MarkMark.controller'
import {FoodBlog} from '../controllers/FoodBlog.controller'

Route.endpoint('options', '**')
    .handledBy(() => api.one({}))

Route
    .group('/', () => {
        Route.get('/70s')
            .pre(SiteTheme)
            .parameterMiddleware(LoadFeedPosts)
            .calls<Home>(Home, home => home.welcome70s)
            .alias('home2')

        Route.get('/')
            // .pre(SiteTheme)
            // .parameterMiddleware(LoadFeedPosts)
            .calls<Home>(Home, home => home.welcome)
            .alias('home')

        Route.get('/blog')
            .pre(SiteTheme)
            .calls<Blog>(Blog, blog => blog.index)
            .alias('blog')

        Route.get('/blog/archive')
            .pre(SiteTheme)
            .calls<Blog>(Blog, blog => blog.archive)
            .alias('blog:archive')

        Route.get('/blog/tags')
            .pre(SiteTheme)
            .calls<Blog>(Blog, blog => blog.tags)
            .alias('blog:tags')

        Route.get('/blog/feeds')
            .pre(SiteTheme)
            .calls<Blog>(Blog, blog => blog.feeds)
            .alias('blog:feeds')

        Route.get('/blog/rss2.xml')
            .calls<Blog>(Blog, blog => blog.rss)
            .alias('blog:rss')

        Route.get('/blog/atom.xml')
            .calls<Blog>(Blog, blog => blog.atom)
            .alias('blog:atom')

        Route.get('/blog/json.json')
            .calls<Blog>(Blog, blog => blog.json)
            .alias('blog:json')

        Route.get('/blog/tag/:tag')
            .pre(SiteTheme)
            .calls<Blog>(Blog, blog => blog.tag)

        Route.get('/blog/*/*/*/:slug')
            .pre(SiteTheme)
            .calls<Blog>(Blog, blog => blog.post)

        Route.get('/food')
            .pre(SiteTheme)
            .calls<FoodBlog>(FoodBlog, fb => fb.index)
            .alias('wpa')

        Route.get('/food/about')
            .pre(SiteTheme)
            .calls<FoodBlog>(FoodBlog, fb => fb.about)

        Route.get('/food/c/:country')
            .pre(SiteTheme)
            .calls<FoodBlog>(FoodBlog, fb => fb.country)

        Route.get('/food/archive')
            .pre(SiteTheme)
            .calls<FoodBlog>(FoodBlog, fb => fb.archive)

        Route.get('/food/feeds')
            .pre(SiteTheme)
            .calls<FoodBlog>(FoodBlog, fb => fb.feeds)

        Route.get('/food/rss2.xml')
            .calls<FoodBlog>(FoodBlog, fb => fb.rss)
            .alias('food:rss')

        Route.get('/food/atom.xml')
            .calls<FoodBlog>(FoodBlog, fb => fb.atom)
            .alias('food:atom')

        Route.get('/food/json.json')
            .calls<FoodBlog>(FoodBlog, fb => fb.json)
            .alias('food:json')

        Route.get('/food/*/*/*/:slug')
            .pre(SiteTheme)
            .calls<FoodBlog>(FoodBlog, fb => fb.post)

        Route.post('/contact')
            .pre(RateLimit)
            .pre(SiteTheme)
            .parameterMiddleware(ValidContactForm)
            .calls<Home>(Home, home => home.contact)
            .alias('contact')

        Route.post('/api/contact')
            .pre(RateLimit)
            .parameterMiddleware(ValidContactForm)
            .calls<Home>(Home, home => home.contactApi)
            .alias('contact-api')

        Route.get('/humans.txt')
            .calls<Home>(Home, home => home.humans)

        Route.get('/technical')
            .pre(SiteTheme)
            .calls<Home>(Home, home => home.technical)

        Route.get('/style')
            .pre(SiteTheme)
            .calls<Home>(Home, home => home.style)

        Route.get('/analytics/opt-out')
            .pre(SiteTheme)
            .calls<Home>(Home, home => home.optOutPrompt)
            .alias('opt-out-prompt')

        Route.post('/analytics/opt-out')
            .pre(SiteTheme)
            .calls<Home>(Home, home => home.optOut)
            .alias('opt-out')

        Route.get('/snippet/:slug')
            .pre(SiteTheme)
            .parameterMiddleware(LoadSnippet)
            .calls<Snippets>(Snippets, snippets => snippets.viewSnippet)

        Route.any('/go/:short')
            .pre(SiteTheme)
            .calls<GoLinks>(GoLinks, go => go.launch)

        Route.get('/favicon.ico')
            .pre(SiteTheme)
            .calls<Home>(Home, home => home.favicon)

        Route.group('feed', () => {
            Route.get('/')
                .pre(SiteTheme)
                .parameterMiddleware(LoadFeedPosts, {all: true})
                .calls<Feed>(Feed, feed => feed.feed)
                .alias('feed')

            Route.get('/rss.xml')
                .parameterMiddleware(LoadFeedPosts, {all: true})
                .calls<Feed>(Feed, feed => feed.rss)
                .alias('feed.rss')

            Route.get('/atom.xml')
                .parameterMiddleware(LoadFeedPosts, {all: true})
                .calls<Feed>(Feed, feed => feed.atom)
                .alias('feed.atom')

            Route.get('/json.json')
                .parameterMiddleware(LoadFeedPosts, {all: true})
                .calls<Feed>(Feed, feed => feed.json)
                .alias('feed.json')
        })

        Route.group('/markmark', () => {
            Route.get('/')
                .pre(SiteTheme)
                .calls<MarkMark>(MarkMark, m => m.welcome)

            Route.get('/standard')
                .pre(SiteTheme)
                .calls<MarkMark>(MarkMark, m => m.standard)
        })

        Route.get('/theme/:themeKey')
            .pre(SiteTheme)
            .calls<Home>(Home, h => h.getThemeCSSFile)

        Route.get('/links')
            .pre(SiteTheme)
            .calls<MarkMark>(MarkMark, m => m.linksHtml)
            .alias('links')

        Route.get('/links/rss2.xml')
            .calls<MarkMark>(MarkMark, m => m.rss)
            .alias('links:rss')

        Route.get('/links/atom.xml')
            .calls<MarkMark>(MarkMark, m => m.atom)
            .alias('links:atom')

        Route.get('/links/json.json')
            .calls<MarkMark>(MarkMark, m => m.json)
            .alias('links:json')

        Route.get('/links.mark.md')
            .calls<MarkMark>(MarkMark, m => m.linksMarkMark)
    })
    .pre(SessionAuthMiddleware)
    .pre(PageView)
