use std::process;
use crate::Project;

pub fn stop_all_boxes(projects: &Vec<Project>){
    let mut problem_childs: Vec<Project> = Vec::new();
    let mut all_done = true;
    for project in projects{
        let stopped = process::Command::new("distrobox")
                             .arg("stop")
                             .arg("--root")
                             .arg(&project.boxname)
                             .status().expect("error spawing distrobox");
        if stopped.success(){
            println!("{} sucessfully stopped!", &project.boxname);
        }
        else{
            println!("{} not stopped!!!!!!!", &project.boxname);
            all_done = false;
            problem_childs.push(project.clone());
        }
    }
    if all_done{
        println!("All boxes stopped GO US YAAAAA WE DID IT");
    }
    else{
        println!("OOOOOOOF some boxes didn't stop yo, that's cringe");
        println!("here are the problem childs, you may need to stop them manually");
        for child in problem_childs{
            println!("{}",child.boxname);
        }
    }
}

pub fn project_standalone_terminal(project: Project, mut terminal: String){
    terminal = terminal.trim_end().to_owned();
    let mut profile = false;
    if terminal.contains("profile"){
        profile = true;
    }
    let terminal_vec:Vec<&str> = terminal.split(" ").collect();
    let mut terminal_start = process::Command::new(terminal_vec[0]);
    let mut first = true;
    for arg in terminal_vec{
        if first == true{
            first = false;
        }
        else{
            terminal_start.arg(arg);
        }
    } 
    if profile == false {
        terminal_start.arg(&project.boxname);
    }
    print!("{}", terminal);
    let start = terminal_start.spawn();
    match start{
        Ok(_child) => println!("New Terminal Started, you can retun to the menu now!"),
        Err(_error) => println!("error starting new terminanl, something may be wrong with the terminal part of your config!\nCheck ~/.conf/pyro_pentest_tool/conf for errors")
    }
    //process::Command::new("konsole").arg("--profile").arg("attack").arg(project.boxname).spawn().expect("error opeing konsole");
}

pub fn project_inline_terminal(project: Project){
    process::Command::new("distrobox").arg("enter").arg("--root").arg(project.boxname).arg("--").arg("script").arg("-a").arg("-B").arg("/pentest/working/terminal.log").status().expect("error opeing konsole");
}