use std::collections::HashMap;
use std::fs;
use std::io::Write;
use std::path::PathBuf;
use std::process;



pub fn install(config_path: &PathBuf){
    let mut _terminal_commands = HashMap::from([
        ("konsole", "konsole -e !!!"),
        ("gnome", "gnome-terminal -- bash !!!"),
        ("xfce", "xfce4-terminal --execute '!!!'"),
        ("alacritty", "alacritty -e !!!"),
    ]);
    let mut config_folder_path: PathBuf = config_path.clone();
    config_folder_path.pop();
    let mut projects_conf_path = config_folder_path.clone();
    let del_on_fail = config_folder_path.clone();
    projects_conf_path.push("projects.conf");
    fs::create_dir_all(&config_folder_path).expect("error creating config dir");
    let mut config_file = fs::File::create(config_path).expect("error creating file");
    let mut projects_conf_file = fs::File::create(projects_conf_path).expect("error creating projects config file");
    projects_conf_file.write_all(b"customer:name:notes:files:active:box_name\n").expect("error writing default project info");
    let mut terminal_response = String::new();
    let mut notes_response = String::new();
    let mut files_response = String::new();
    let mut tools_response = String::new();
    let mut template_name = String::new();
    let mut have_template = String::new();
    println!("terminal you use? (example: konsole, xfce, gnome, etc)");
    std::io::stdin().read_line(&mut terminal_response).unwrap();
    let mut _terminal_command = String::new();
    if terminal_response.contains("konsole"){
        let mut response_buffer = String::new();
        println!("do you already have a custom profile setup?");
        println!("this is pretty specific to pyro's setup, I do some special sauce for my konsole stuff");
        std::io::stdin().read_line(&mut response_buffer).unwrap();
        if response_buffer.to_lowercase().contains("y"){
            let mut profile = String::new();
            println!("konsole profile name?");
            std::io::stdin().read_line(&mut profile).unwrap();
            _terminal_command = format!("konsole --profile {}", profile);
        }
    }
    else{
        _terminal_command = _terminal_commands[terminal_response.trim_end()].to_owned();
    }
    println!("path to save project notes?");
    std::io::stdin().read_line(&mut notes_response).unwrap();
    println!("path to save project files?");
    std::io::stdin().read_line(&mut files_response).unwrap();
    println!("path to folder with your custom tools?");
    std::io::stdin().read_line(&mut tools_response).unwrap();
    print!("
This tool is mainly to handle distrobox creation and usage.
It's expecting you to have a distrobox that you will use as a template.
Do you have a distrobox set up to function as your template for all new projects?
");
    std::io::stdin().read_line(&mut have_template).unwrap();
    if have_template.contains("n"){
        println!("please set up a distrobox with root as a template and re-run this tool");
        println!("example distrobox setup command:");
        println!("distrobox create --root --image archlinux --name template");
        println!("then enter that distrobox and install all the tools you want and do what ever setup you need");
        println!("and re-run this tool.");
        process::Command::new("rm").arg(del_on_fail).spawn().expect("ERROR deleting config folder, please manually clean up");
        std::process::exit(1);
    }
    let _list = process::Command::new("distrobox").arg("list").arg("--root").status();
    println!("distrobox template name?");
    std::io::stdin().read_line(&mut template_name).unwrap();
    let config_string = format!("Project_files:{}\nProject_notes:{}\ntools_folder:{}\nterminal:{}", files_response.trim_end(), notes_response.trim_end(), tools_response.trim_end(), _terminal_command.trim_end());
    config_file.write_all(config_string.as_bytes()).expect("error writing to config file");
    let default_projectline = format!("default:default:{}:{}:yes:{}", &notes_response.trim_end(), &files_response.trim_end(), &template_name.trim_end());
    projects_conf_file.write_all(default_projectline.as_bytes()).expect("error writing default project line");
    println!("config file generated and saved to {}\n", config_path.display());
    println!("please rerun the program");
    

    let config_path = &config_folder_path.clone();
    let mut install_path = config_path.clone();
    let mut password_spray_template_path = install_path.clone();
    install_path.push("new_projects.conf");
    password_spray_template_path.push("passwordspray.md");
    let password_spray_template_path = install_path.clone();
    let mut conf_file = fs::File::create(install_path).expect("error creating config file");
    let mut project_folder_path = String::new();
    let mut porject_note_path = String::new();
    println!("path to the project folders directory?");
    std::io::stdin().read_line(&mut project_folder_path).expect("error reading project folder from stdin");
    println!("path to the project notes directory");
    std::io::stdin().read_line(&mut porject_note_path).expect("error reading project note folder form stdin");
    write!(conf_file, "project_folder_path:{}
project_notes_path:{}
", project_folder_path.trim_end(), porject_note_path.trim_end()).expect("error writing config file");
    let mut passpray_file =  fs::File::create(password_spray_template_path).expect("error creating passwordspray file");
    write!(passpray_file, "
- [ ] useraspass
- [ ] Seasonyear!
- [ ] Service123!
- [ ] admin
- [ ] Admin
- [ ] Admin123!
- [ ] admin123
- [ ] admin1
- [ ] 1234567
- [ ] Seasonyear
- [ ] seasonyear!
- [ ] seasonyear
- [ ] COMPANYYEAR!
- [ ] COMPANYYEAR
- [ ] November2024!
- [ ] September2024!
- [ ] October2024!
- [ ] COMPANYfoundingyear!
- [ ] COMPANYfoundingyear
- [ ] COMPANYstreetnumber!
- [ ] COMPANYstreetnumber
- [ ] Password
- [ ] P@ssw0rd
- [ ] Password1!
- [ ] Password123!
- [ ] Passwordyear!
- [ ] P@55w0rd
- [ ] Service
- [ ] Service!
- [ ] Serviceyear!").expect("error writing password spray template");
    std::process::exit(0);
}