extern crate supports_color;

pub mod iwakulog {
    use supports_color::Stream;

    pub enum LogLevel {
        Fail,
        Warn,
        Info
    }

    const COLOR_RESET: &str = "\x1b[0m";
    const COLOR_FAIL: &str = "\x1b[0;31m";
    const COLOR_WARN: &str = "\x1b[0;33m";
    const COLOR_INFO: &str = "\x1b[0;34m";

    fn _prefix(p: &str, c1: &str, c2: &str, c3: &str, color_support: bool) -> String {
        if color_support {
            return format!("{}[{}{}{}]", c1, c2, p, c3);
        }
        return format!("[{}]", p);
    }

    fn prefix(level: LogLevel) -> String {
        let color_support = supports_color::on(Stream::Stdout);
        let prefixa = match level {
            LogLevel::Fail => _prefix("f", COLOR_RESET, COLOR_FAIL, COLOR_RESET, color_support.is_some()),
            LogLevel::Warn => _prefix("w", COLOR_RESET, COLOR_WARN, COLOR_RESET, color_support.is_some()),
            LogLevel::Info => _prefix("i", COLOR_RESET, COLOR_INFO, COLOR_RESET, color_support.is_some()),
        };
        return prefixa;
    }

    pub fn log(level: LogLevel, message: &str) {
        println!("{}: {}", prefix(level), message);
    }
}
