pub mod admin;
pub mod contexts;
pub mod controllers;
pub mod misc;
pub mod models;
mod schema;

#[macro_use]
extern crate rocket;
#[macro_use]
extern crate diesel;

use comrak::{markdown_to_html, ComrakOptions};
use controllers::posts;
use dotenv::dotenv;
use rocket::fs::{FileServer, NamedFile};
use rocket::response::content::RawXml;
use rocket::Request;
use rocket_dyn_templates::Template;
use std::path::Path;
use std::vec::Vec;

#[get("/?<page>")]
fn index(page: Option<u64>) -> Template {
    let page: u64 = page.unwrap_or(1);

    let (posts, n_posts) = posts::get_posts(Some(page));

    let context = contexts::PageOfPosts::new(posts, page, n_posts);

    Template::render("index", context)
}

#[get("/feed.xml")]
fn rss_feed() -> RawXml<Template> {
    let (mut posts, _) = posts::get_posts(None);

    let comrak_options = ComrakOptions {
        ext_table: true,
        ext_autolink: true,
        ext_tasklist: true,
        unsafe_: true,
        ..ComrakOptions::default()
    };

    posts.iter_mut().for_each(|post| {
        let content = markdown_to_html(&post.content, &comrak_options);
        post.content = content;
    });

    let context = contexts::XmlFeed::new(posts);

    RawXml(Template::render("rss", context))
}

#[get("/atom.xml")]
fn atom_feed() -> RawXml<Template> {
    let (mut posts, _) = posts::get_posts(None);

    let comrak_options = ComrakOptions {
        ext_table: true,
        ext_autolink: true,
        ext_tasklist: true,
        unsafe_: true,
        ..ComrakOptions::default()
    };

    posts.iter_mut().for_each(|post| {
        let content = markdown_to_html(&post.content, &comrak_options);
        post.content = content;
    });

    let context = contexts::XmlFeed::new(posts);

    RawXml(Template::render("atom", context))
}

#[get("/post/<title>")]
fn show_post(title: String) -> Template {
    let title_splited: Vec<&str> = title.split('-').collect();
    let id = title_splited.last().unwrap().parse().unwrap_or(-1);

    match posts::get_post(id) {
        Ok(mut post) => {
            let comrak_options = ComrakOptions {
                ext_table: true,
                ext_autolink: true,
                ext_tasklist: true,
                unsafe_: true,
                ..ComrakOptions::default()
            };

            let content = markdown_to_html(&post.content, &comrak_options);
            post.content = content;

            let context = contexts::SinglePost::new(post);

            Template::render("post", context)
        }
        Err(_) => {
            let url = format!("/post/{}", title_splited.join("-"));

            let context = contexts::NotFound::new(url);

            Template::render("404", context)
        }
    }
}

#[catch(404)]
fn not_found_404(req: &Request) -> Template {
    let uri = format!("{}", req.uri());

    let context = contexts::NotFound::new(uri);

    Template::render("404", context)
}

#[get("/favicon.ico")]
async fn favicon() -> Option<NamedFile> {
    NamedFile::open(Path::new("static/favicon.ico")).await.ok()
}

#[launch]
fn rocket() -> _ {
    dotenv().ok();

    rocket::build()
        .attach(Template::fairing())
        .mount("/", routes![index, show_post, favicon, rss_feed, atom_feed])
        .mount("/admin", admin::get_routes())
        .mount("/static", FileServer::from("static"))
        .register("/", catchers![not_found_404])
}