use crate::commands::{self, Verbosity};
use crate::error::Error;
use crate::instance::InstanceDao;
use crate::util::Terminal;

use std::path::Path;
use std::str;
use std::thread;
use std::time::Duration;

pub fn console(instance_dao: &InstanceDao, name: &str) -> Result<(), Error> {
    let instance = instance_dao.load(name)?;

    if !instance_dao.is_running(&instance) {
        commands::start(
            instance_dao,
            &None,
            Verbosity::Quiet,
            &vec![name.to_string()],
        )?;
    }

    let console_path = format!("{}/{}/console", instance_dao.cache_dir, name);
    while !Path::new(&console_path).exists() {
        thread::sleep(Duration::new(1, 0));
    }

    if let Ok(mut term) = Terminal::open(&console_path) {
        term.wait();
    } else {
        println!("Cannot open shell");
    }

    Ok(())
}
