use serde::Deserialize;
use std::fs;

const DEFAULT_GIT_DIR: &str = "~/.git";
const DEFAULT_GIT_PATH: &str = "/usr/sbin/git";
const DEFAULT_ICON_PATH: &str = "~/.git/favicon.ico";
const DEFAULT_LOAD_DURATION: u64 = 300;
const DEFAULT_GIT_GRAPH_COUNT: usize = 50;

#[derive(Clone, Deserialize)]
pub struct Config {
    pub load_duration: u64,
    pub git_dir: String,
    pub icon_path: String,
    pub git_graph_count: usize,
    pub private_repos: Vec<String>,
    pub git_path: String,
}

impl Config {
    pub fn default() -> Config {
        Config {
            git_dir: DEFAULT_GIT_DIR.to_string(),
            icon_path: DEFAULT_ICON_PATH.to_string(),
            load_duration: DEFAULT_LOAD_DURATION,
            git_graph_count: DEFAULT_GIT_GRAPH_COUNT,
            private_repos: Vec::new(),
            git_path: DEFAULT_GIT_PATH.to_string(),
        }
    }
    pub fn load_config_from_file(path: &str) -> Config {
        let config_text = fs::read_to_string(path).unwrap_or_default();
        toml::from_str(&config_text).unwrap()
    }
}