package cd.casic.ci.process.engine.worker.base;

import cd.casic.ci.process.constant.CommandConstant;
import cd.casic.ci.process.engine.runContext.BaseRunContext;
import cd.casic.ci.process.enums.MachineSystemEnum;
import cd.casic.ci.process.ssh.SshClient;
import cd.casic.ci.process.ssh.SshClientFactory;
import cd.casic.ci.process.ssh.WinRMHelper;
import cd.casic.module.machine.dal.dataobject.MachineInfoDO;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
/**
 *  使用ssh的worker的基类
 * */
@Slf4j
public abstract class SshWorker extends BaseWorker {
    /**
     * 执行shell命令
     * @param machineInfo 机器
     * @param commands 命令
     * @return 0 成功；其他值 失败
     */
    public int shell(MachineInfoDO machineInfo, String sudoPassword, BaseRunContext context, String... commands) {
        List<String> commandList = Arrays.asList(commands);
        if(MachineSystemEnum.WINDOWS.getSystem().equals(machineInfo.getMachineInfoType())){
            return powerShell(machineInfo, commandList);
        }
//        NodeLogger nodeLogger = nodeLoggerThreadLocal.get();
        int statusCode = -1;
//        String loggerUuid = nodeLogger.getLoggerUuid();
//        String nodeUuid = nodeLogger.getNodeUuid();
        //loggerUuid得转换成String类型，才能生成key，然后才能通过websocket实时推送节点执行日志
        SshClient ssh = null;
        try {
            ssh = SshClientFactory.createSsh(machineInfo);
            //执行命令，并且把命令的执行回传到前端
            // TODO 记录日志
            //                loggerService.sendMessage(key, var);
            statusCode = ssh.execNew(commandList,sudoPassword, (content)->append(context,content));
            log.info("exit-status: " + statusCode);
            //主动释放当前socket连接
//            loggerService.close(key);
        } catch (Exception e) {
            String errorMessage = "与机器建立SSH连接出错" + CommandConstant.ENTER;
//            errorHandle(e, errorMessage);
            append(context,errorMessage);
        } finally {
            if(ssh!=null) {
                ssh.disconnect();
            }
        }
        return statusCode;
    }
    /**
     * 执行shell命令
     * @param machineInfo 机器
     * @param commandList 命令
     * @return 0 成功；其他值 失败
     */
    public int powerShell(MachineInfoDO machineInfo, List<String> commandList) {
        int statusCode = -1;
        //loggerUuid得转换成String类型，才能生成key，然后才能通过websocket实时推送节点执行日志

        try {
            WinRMHelper winRmHelper = new WinRMHelper(machineInfo);
            statusCode = winRmHelper.executePs(commandList, var ->{
                log.info("================================================================" + var);
                // TODO 记录日志
//                loggerService.sendMessage(key, var);
//                nodeLogger.append(var);
            });

            log.info("exit-status: " + statusCode);
            //主动释放当前socket连接

        } catch (Exception e) {
            String errorMessage = "与Window机器建立winrm连接出错" + CommandConstant.ENTER;
//            errorHandle(e, errorMessage);
        }
        return statusCode;
    }
}
