mod atom;
mod dir_helper;
mod doc;
mod include_static_helper;
mod simple_template;
mod tree;

use std::{ops::ControlFlow, sync::Arc};

use atom::FeedDir;
use chrono::{DateTime, Utc};
use dir_helper::DirHelper;
use handlebars::{Handlebars, handlebars_helper};
use include_static_helper::IncludeStaticHelper;
use serde::Serialize;
use tracing::{error, info_span, instrument};

use crate::{
    config::Config,
    dirs::Dirs,
    fun::seasons::Season,
    generate::{
        doc::DocDir,
        simple_template::SimpleTemplateDir,
        tree::{DirIndex, TreehouseDir},
    },
    sources::Sources,
    vfs::{
        self, AnchoredAtExt, Cd, Content, ContentCache, Dir, DynDir, HtmlCanonicalize, MemDir,
        ToDynDir, VPath, VPathBuf, layered_dir,
    },
};

#[derive(Serialize)]
struct BaseTemplateData<'a> {
    config: &'a Config,
    import_map: String,
    season: Option<Season>,
    dev: bool,
}

impl<'a> BaseTemplateData<'a> {
    fn new(sources: &'a Sources) -> Self {
        Self {
            config: &sources.config,
            import_map: serde_json::to_string_pretty(&sources.import_map)
                .expect("import map should be serializable to JSON"),
            season: Season::current(),
            dev: cfg!(debug_assertions),
        }
    }
}

fn create_handlebars(site: &str, static_: DynDir) -> Handlebars<'static> {
    let mut handlebars = Handlebars::new();

    handlebars_helper!(cat: |a: String, b: String| a + &b);
    handlebars_helper!(iso_date: |d: DateTime<Utc>| d.format("%F").to_string());

    handlebars.register_helper("cat", Box::new(cat));
    handlebars.register_helper("iso_date", Box::new(iso_date));
    handlebars.register_helper("asset", Box::new(DirHelper::new(site, static_.clone())));
    handlebars.register_helper(
        "include_static",
        Box::new(IncludeStaticHelper::new(static_)),
    );

    handlebars
}

#[instrument(skip(handlebars))]
fn load_templates(handlebars: &mut Handlebars, dir: &dyn Dir) {
    vfs::walk_dir_rec(dir, VPath::ROOT, &mut |path| {
        if path.extension() == Some("hbs")
            && let Some(content) = vfs::query::<Content>(dir, path).and_then(|c| c.string().ok())
        {
            let _span = info_span!("register_template", ?path).entered();
            if let Err(err) = handlebars.register_template_string(path.as_str(), content) {
                error!("in template: {err}");
            }
        }
        ControlFlow::Continue(())
    });
}

pub fn target(dirs: Arc<Dirs>, sources: Arc<Sources>) -> DynDir {
    let mut handlebars = create_handlebars(&sources.config.site, dirs.static_.clone());
    load_templates(&mut handlebars, &dirs.template);
    let handlebars = Arc::new(handlebars);

    let mut root = MemDir::new();
    root.add(
        VPath::new("feed"),
        ContentCache::new(FeedDir::new(
            dirs.clone(),
            sources.clone(),
            handlebars.clone(),
        ))
        .to_dyn(),
    );
    root.add(VPath::new("static"), dirs.static_.clone());
    root.add(
        VPath::new("robots.txt"),
        Cd::new(dirs.static_.clone(), VPathBuf::new("robots.txt")).to_dyn(),
    );

    let dir_index = DirIndex::new(sources.treehouse.files_by_tree_path.keys().map(|x| &**x));
    let treehouse_dir = layered_dir(&[
        TreehouseDir::new(dirs.clone(), sources.clone(), handlebars.clone(), dir_index).to_dyn(),
        DocDir {
            sources: sources.clone(),
            dirs,
            handlebars: handlebars.clone(),
        }
        .to_dyn(),
        SimpleTemplateDir::new(sources.clone(), handlebars.clone()).to_dyn(),
    ]);

    let tree_view = ContentCache::new(treehouse_dir);
    tree_view.warm_up();
    let tree_view = HtmlCanonicalize::new(tree_view);

    layered_dir(&[tree_view.to_dyn(), root.to_dyn()])
        .anchored_at(VPath::ROOT.to_owned())
        .to_dyn()
}
