use std::process::{Command, Stdio};
use crate::common::define::*;
use crate::common::define::REQ_DURATION;
use std::sync::mpsc::{Sender, Receiver};
use std::sync::mpsc;
use std::thread;
use std::time::Duration;

pub fn other_sync_cmd(slave: &Order) -> Order{
    let mut  feedback=slave.clone();
    let slave1=slave.clone();
    let (tx, rx): (Sender<String>, Receiver<String>) = mpsc::channel();
    let worker=thread::spawn(move || {
        let res = sync_cmd(&slave1);
        match res {
            Ok(s) => tx.send(s).unwrap(),
            Err(e) => {
                dbg!("cmd fail");
                let s = format!("无法执行cmd{}", e);
                tx.send(s).unwrap();
            }
        }
        ;
    });
    let secs = Duration::from_secs(*REQ_DURATION);
    let res = rx.recv_timeout(secs);
    match res {
        Ok(s) => {
            dbg!("get res");
            feedback.status=SUCCESS.to_string();
            feedback.resp=s;
        },
        Err(e) => {
            // 杀掉不工作的worker和rx,tx
            drop(rx);
            drop(worker);
            dbg!("i can not wait,time is over");
            feedback.status=FAIL.to_string();
            feedback.resp=format!("time out:{}", e);
        }
    }
    feedback
}

pub fn sync_cmd(slave: &Order) -> anyhow::Result<String> {
    let req = slave.req.clone();
    dbg!(&req);
    let output = if cfg!(target_os = "windows") {
        Command::new("cmd")
            .args(&["/C", &req])
            .stdout(Stdio::piped())
            .spawn()?
            .wait_with_output()?
    } else {
        Command::new("sh")
            .arg("-c")
            .arg(&req)
            .stdout(Stdio::piped())
            .spawn()?
            .wait_with_output()?
    };
    if output.status.success() {
        let res1 = output.stdout;
        let res2 = String::from_utf8_lossy(&res1);
        let res3 = res2.as_ref().to_string();
        return Ok(res3);
    }
    let err1 = output.stderr;
    let err2 = String::from_utf8_lossy(&err1);
    let err3 = err2.as_ref().to_string();
    Ok(format!("error from other cmd:{}", err3))
}