use std::fs::File;
use std::io::{BufRead, BufReader};
use std::env::VarError;
use crate::basedir;
pub fn filename() -> Result<String, VarError> {
    let conf_dir = match basedir::config_home() {
        Ok(c) => c,
        Err(e) => return Err(e),
    };
    Ok(format!("{}/user-dirs.dirs", conf_dir.as_str()))
}
#[derive(Clone, Debug)]
pub struct UserDirs {
        pub desktop:String,
        pub download:String,
        pub template:String,
        pub public_share:String,
        pub documents:String,
        pub music:String,
        pub pictures:String,
        pub videos:String,
}
impl UserDirs {
        #[allow(dead_code)]
    pub fn empty() -> Self {
        Self {
            desktop:String::new(),
            download:String::new(),
            template:String::new(),
            public_share:String::new(),
            documents:String::new(),
            music:String::new(),
            pictures:String::new(),
            videos:String::new(),
        }
    }
        #[allow(dead_code)]
    pub fn new() -> Self {
        let mut desktop = String::new();
        let mut download = String::new();
        let mut template = String::new();
        let mut public_share = String::new();
        let mut documents = String::new();
        let mut music = String::new();
        let mut pictures = String::new();
        let mut videos = String::new();
        let file_name = match filename() {
            Ok(f) => f,
            Err(_) => return Self::empty(),
        };
                let home = match basedir::home() {
            Ok(h) => h,
            Err(_) => "$HOME".to_string(),
        };
                let file = match File::open(file_name.as_str()) {
            Ok(f) => f,
            Err(_) => return Self::empty(),
        };
        let file_reader = BufReader::new(file);
        for (_line_number, line) in file_reader.lines().enumerate() {
            if line.is_err() {
                continue;
            }
            let mut line = line.unwrap();
                        if let Some(position) = line.find('#') {
                if position == 0 {
                    continue;
                }
            }
            line.retain(|c| c != '"');            if let Some((var, dir)) = line.rsplit_once('=') {
                let _stizzle = dir.replace("$HOME", home.as_str());
                if var == "XDG_DESKTOP_DIR" {
                    desktop = dir.to_string();
                } else if var == "XDG_DOWNLOAD_DIR" {
                    download = dir.to_string();
                } else if var == "XDG_TEMPLATES_DIR" {
                    template = dir.to_string();
                } else if var == "XDG_PUBLICSHARE_DIR" {
                    public_share = dir.to_string();
                } else if var == "XDG_DOCUMENTS_DIR" {
                    documents = dir.to_string();
                } else if var == "XDG_MUSIC_DIR" {
                    music = dir.to_string();
                } else if var == "XDG_PICTURES_DIR" {
                    pictures = dir.to_string();
                } else if var == "XDG_VIDEOS_DIR" {
                    videos = dir.to_string();
                }
            }
        }
        Self {
            desktop,
            download,
            template,
            public_share,
            documents,
            music,
            pictures,
            videos,
        }
    }
}