const exec = require('child_process').exec
let iconv = null;
try {
    iconv = require('iconv-lite');
} catch (error) {

}
let workerProcess = undefined;//子进程名称
let ifkill = false;
exports.runExec = function (Path, cmdCode, SetProcessYesMsg, SetProcessNoMsg, SetProcessEndMsg, encoding_ = "GBK", env = undefined, ifArr = false) {
    if (ifArr == false) {
        //ifArr-非队列执行，不等待之前的命令行执行完毕，本参数默认为假；如果为真的情况下则每次执行命令行都会创建一个临时对象去执行，此时结束命令行方法则会失效。
        if (ifkill == true) {
            return;
        }
    }
    let cwd = {
        cwd: Path,
        encoding: encoding_,
    }
    if (env != undefined) {
        cwd["env"] = env
    }
    if (ifArr == false) {
        workerProcess = exec(cmdCode, cwd)
        ifkill = true;
        // 打印正常的后台可执行程序输出
        workerProcess.stdout.on('data', function (data) {
            if (ifkill == true) {
                try {
                    const utf8Stdout = iconv.decode(Buffer.from(data, 'binary'), 'GBK');
                    let JSONData = { "输出数据": data, "编码后": utf8Stdout }
                    if (SetProcessYesMsg != undefined) {
                        SetProcessYesMsg(JSONData)
                    }
                } catch (error) {
                    const decoder = new TextDecoder();
                    let JSONData = { "输出数据": data, "编码后": decoder.decode(data) }
                    if (SetProcessYesMsg != undefined) {
                        SetProcessYesMsg(JSONData)
                    }
                }
            }
        })
        // 打印错误的后台可执行程序输出
        workerProcess.stderr.on('data', function (data) {
            if (ifkill == true) {
                try {
                    const utf8Stdout = iconv.decode(Buffer.from(data, 'binary'), 'GBK');
                    let JSONData = { "输出数据": data, "编码后": utf8Stdout }
                    if (SetProcessNoMsg != undefined) {
                        SetProcessNoMsg(JSONData)
                    }
                } catch (error) {
                    const decoder = new TextDecoder();
                    let JSONData = { "输出数据": data, "编码后": decoder.decode(data) }
                    if (SetProcessNoMsg != undefined) {
                        SetProcessNoMsg(JSONData)
                    }
                }
            }
        })
        // 退出之后的输出
        workerProcess.on('close', function (code) {
            if (ifkill == true) {
                ifkill = false;
                if (SetProcessEndMsg != undefined) {
                    SetProcessEndMsg(code)
                }
            }
        })
    } else {
        let workerProcessArr = exec(cmdCode, cwd)
        // 打印正常的后台可执行程序输出
        workerProcessArr.stdout.on('data', function (data) {
            try {
                const utf8Stdout = iconv.decode(Buffer.from(data, 'binary'), 'GBK');
                let JSONData = { "输出数据": data, "编码后": utf8Stdout }
                if (SetProcessYesMsg != undefined) {
                    SetProcessYesMsg(JSONData)
                }
            } catch (error) {
                const decoder = new TextDecoder();
                let JSONData = { "输出数据": data, "编码后": decoder.decode(data) }
                if (SetProcessYesMsg != undefined) {
                    SetProcessYesMsg(JSONData)
                }
            }
        })
        // 打印错误的后台可执行程序输出
        workerProcessArr.stderr.on('data', function (data) {
            try {
                const utf8Stdout = iconv.decode(Buffer.from(data, 'binary'), 'GBK');
                let JSONData = { "输出数据": data, "编码后": utf8Stdout }
                if (SetProcessNoMsg != undefined) {
                    SetProcessNoMsg(JSONData)
                }
            } catch (error) {
                const decoder = new TextDecoder();
                let JSONData = { "输出数据": data, "编码后": decoder.decode(data) }
                if (SetProcessNoMsg != undefined) {
                    SetProcessNoMsg(JSONData)
                }
            }
        })
        // 退出之后的输出
        workerProcessArr.on('close', function (code) {
            if (SetProcessEndMsg != undefined) {
                SetProcessEndMsg(code)
            }
            try {
                workerProcessArr.kill()
            } catch (error) {

            }
        })
    }
}
exports.kill = function () {
    if (ifkill == true) {
        ifkill = false;
    }
    try {
        workerProcess.kill()
    } catch (error) {

    }
}