use crate::app::AppConfig;
use crate::website::page::{PagesTree, WebPage};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct WebSite {
    pages_tree: PagesTree,
    pages_index_by_url: HashMap<PathBuf, WebPage>,
}

impl WebSite {
    pub fn new(pages_tree: PagesTree) -> Self {
        let mut pages_index_by_url = HashMap::new();
        WebSite::create_index_by_url(&mut pages_index_by_url, &pages_tree, PathBuf::from("/"));

        WebSite {
            pages_tree,
            pages_index_by_url,
        }
    }

    pub fn from_json_str(json: &str) -> Self {
        WebSite::new(serde_json::from_str(json).unwrap())
    }

    pub fn load(config: &AppConfig) -> WebSite {
        let file_path = match &config.load {
            None => std::env::current_dir()
                .unwrap()
                .join("templates")
                .join("new_website.json"),
            Some(pth) => pth.clone(),
        };

        WebSite::from_json_str(&std::fs::read_to_string(file_path).unwrap())
    }

    fn create_index_by_url(
        index: &mut HashMap<PathBuf, WebPage>,
        pages_tree: &PagesTree,
        from_url: PathBuf,
    ) {
        let page_data = pages_tree.page_data.clone();
        let url = from_url.join(&page_data.slug);

        index.insert(url.clone(), page_data.to_web_page());

        if let Some(sub_pages) = &pages_tree.sub_pages {
            for pt in sub_pages {
                WebSite::create_index_by_url(index, pt, url.clone());
            }
        }
    }

    pub fn get_page_by_url(&self, url: &PathBuf) -> Option<&WebPage> {
        self.pages_index_by_url.get(&PathBuf::from("/").join(url))
    }
}

#[cfg(test)]
mod test_website {
    use super::*;
    use crate::testing::TEST_JSON_WEBSITE;

    #[test]
    fn test_index_pages_by_slug() {
        let website = WebSite::from_json_str(TEST_JSON_WEBSITE);
        let root_page = website.get_page_by_url(&PathBuf::from("/"));
        assert!(root_page.is_some());
        let root_page = root_page.unwrap();
        assert_eq!(root_page.page_data.html_body, "<h1>Test Website</h1>");

        let sub_page = website.get_page_by_url(&PathBuf::from("subpage"));
        assert!(sub_page.is_some());
        let sub_page = sub_page.unwrap();
        assert_eq!(sub_page.page_data.html_body, "<h1>A sub page</h1>");

        let nested_page = website.get_page_by_url(&PathBuf::from("subpage/nested"));
        assert!(nested_page.is_some());
        let nested_page = nested_page.unwrap();
        assert_eq!(nested_page.page_data.html_body, "<h1>Nested page</h1>");
    }
}