use std::path::{PathBuf,Path};
use std::collections::HashMap;
use core::error::Error;
use std::time::SystemTime;

mod lexer;
mod parser;

#[derive(Debug,Copy,Clone)]
pub struct Position {
    pub line: u32,
    pub column: u32,
}

impl Position {
    pub fn new( line: u32, column: u32 ) -> Self {
        Self {
            line, column,
        }
    }
}

pub struct Crimtag {
    fragments: HashMap<String, Fragment>,
    sources: Vec<FragmentSource>,
}

#[derive(PartialEq,Eq,Debug,Clone)]
enum FragmentSource {
    File {
        path: PathBuf,
        loaded: SystemTime,
    },
    Static,
    External {
        key: String,
    },
}

struct Fragment {
    name: String,
    source: usize,
    sections: HashMap<String, Output>,
}

struct Output {
    name: String,
    ast_root: Token,
}

type Properties = HashMap<String, String>;

#[derive(Debug)]
enum Token {
    Root(Vec<Token>),
    Fragment(String,Properties,Vec<Token>),
    Output(String,Properties,Vec<Token>),
    Show(String,Properties),
    Text(String),
    Literal(String),
    /*
    Tag{
        name: String,
        params: Vec<String>,
        props: Properties,
        children: Vec<Token>
    },*/
}

struct State {
}

impl Crimtag {
    pub fn new() -> Self {
        Self {
            fragments: HashMap::new(),
            sources: vec![FragmentSource::Static],
        }
    }

    fn id_source(&mut self, src: &FragmentSource ) -> usize {
        for idx in 0..self.sources.len() {
            if self.sources[idx] == *src {
                return idx;
            }
        }

        // Nothing found, add a new one
        let idx = self.sources.len();
        self.sources.push( src.clone() );
        idx
    }

    pub fn load_file(&mut self, path: &Path) -> Result<(),Box::<dyn Error>> {
        let time = if let Ok(meta) = path.metadata() {
            if let Ok(t) = meta.modified() {
                t
            } else if let Ok(t) = meta.created() {
                t
            } else {
                SystemTime::now()
            }
        } else {
            SystemTime::now()
        };

        let mut p = parser::Parser::new();
        p.parse( &String::from_utf8( std::fs::read( path )? )? );

        Ok(())
    }

    pub fn load_static(&mut self, data: &str) -> Result<(),Box::<dyn Error>> {
        let mut p = parser::Parser::new();
        println!("Parsed token tree: {:?}", p.parse( &data )? );

        Ok(())
    }

    pub fn load_external(&mut self, key: &str, data: &str) -> Result<(),Box::<dyn Error>> {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::lexer::*;

    #[test]
    fn lexing() {
        let data = r#"Leading comment: [|fragment "basic" something="yeup"|>Hello there <|fragment|] Trailing text"#;
        let ll = lexer::Lexer::new( &data );
        for sym in ll {
            if let SymbolType::Error{what} = sym.symbol() {
                println!("Error {}:{}: {:?}", sym.start().line, sym.start().column, what );
                break;
            } else {
                println!("Symbol: {:?}", sym );
            }
        }
    }

    #[test]
    fn parsing() {
        let mut ct = Crimtag::new();
        if let Err(e) = ct.load_static(r#"Here is a sample
[|fragment "index" theme="standard"|><html><body>Hi</body></html><|fragment|]
That was fun! now a placeholder: [|fragment "placeholder"|] and now one with an implicit [|fragment "simple"|>Body [|show "content"|]<|fragment|] aoeu

Now with explicit outputs:
[|fragment "complex"|>
    [|output "content"|>
       Here's a [|show "name"|].
    <|output|]
    [|output "sidebar"|>
        What's up world?
    <|output|]
    [|output "footer"|>
        Same, I guess!
    <|output|]
<|fragment|]"#) {
            println!("Error: {:?}", e );
        } else {
            println!("It finished");
        }
    }
}
