use std::io::Write;
use std::io::{stdin, stdout};
use crate::common::myfs::*;
use crate::common::define::*;
use crate::host::host_cmds::*;
use crate::host::send::*;
use colored::*;
pub fn run_shell() -> anyhow::Result<()> {
    let mut slave = "".to_string();
    let mut wait= true;
    loop {
        print!("{}",">>".white().bold());
        stdout().flush()?;

        let mut input = String::new();
        stdin().read_line(&mut input)?;
        input=input.replace("\r","");
        let (cmd, args) = parse_input(&input)?;
        if cmd == CMD_EXIT {
            break;
        }
        if cmd == CMD_USE {
            if args.len() == 1 {
                slave = args[0].clone();
                print!("--->:{}\n", slave.blue().bold());
            } else {
                show(&slave.clone(),FAIL,"use args should be 1");
            }
            continue;
        }
   
        if cmd == CMD_WAIT {
            if args.len() == 1 {
                let waitornot=&args[0];
                if waitornot=="true"{
                    wait=true;
                    show(&slave.clone(),SUCCESS,"等待返回结果");
                }else if waitornot=="false"{
                    wait=false;
                    show(&slave.clone(),SUCCESS,"不等待返回结果");
                }else{
                    show(&slave.clone(),FAIL,"wait的参数是true或者false");
                }
            } else {
                show(&slave.clone(),FAIL,"wait的参数是1个");
            }
            continue;
        }
        if slave == "" {
            show(&slave.clone(),FAIL,"slave can not empty, use <slaveid> first");
            continue;
        }
        if cmd==CMD_RESTART{
            pub_restart(&slave);
        }
        match cmd.as_str() {
            "" => cmd_empty(),
            CMD_SEND => send_file(&input, &slave),
            CMD_REC => receive_file(&input, &slave),
            CMD_SEND_ALL => send_all(&input, &slave),
            _ => other_cmd(&input, &slave,wait),
        };
    }
    Ok(())
}
