package net.northking.cctp.commands.win;

import net.northking.cctp.commands.OSCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TaskKill implements OSCommand
{
  private static final Logger logger = LoggerFactory.getLogger(TaskKill.class);

  /**
   * 命令名称
   */
  private static final String COMMAND_NAME = "TASKKILL";

  private String computer;
  private String username;
  private String password;

  private String pid;
  private String imageName;
  private boolean force = false;
  private boolean killTeam = false;
  private List<String> filters = new ArrayList<>();

  /**
   * /S <computer>　指定连接到的计算机或IP地址，默认本机。
   *
   * @param computer 计算机或IP地址
   */
  public void computer(String computer)
  {
    this.computer = computer;
  }

  /**
   * /u [<Domain>\]<UserName>　指定使用哪个用户执行这个命令。
   *
   * @param username 用户
   */
  public void username(String username)
  {
    this.username = username;
  }

  /**
   * /P 为指定的用户指定密码。
   *
   * @param password 密码
   */
  public void password(String password)
  {
    this.password = password;
  }

  /**
   * /PID 指定进程号
   *
   * @param pid 进程号
   */
  public void pid(String pid)
  {
    this.pid = pid;
  }

  /**
   * /IM 指定镜像名称
   *
   * @param imageName 镜像名称
   */
  public void imageName(String imageName)
  {
    this.imageName = imageName;
  }

  /**
   * /F 强制终止进程
   *
   * @param force 是/否
   */
  public void force(boolean force)
  {
    this.force = force;
  }

  /**
   * /T 终止指定的进程和由它启用的子进程。
   *
   * @param killTeam 是/否
   */
  public void killTeam(boolean killTeam)
  {
    this.killTeam = killTeam;
  }


  /**
   * 显示一系列符合筛选器指定的进程。
   *
   * @param filter 筛选器
   */
  public void filter(String filter)
  {
    this.filters.add(filter);
  }

  private String build()
  {
    StringBuilder command = new StringBuilder(COMMAND_NAME);

    if (computer != null && computer.trim().length() > 0)
    {
      command.append(" /S ").append(computer);
    }
    if (username != null && username.trim().length() > 0)
    {
      command.append(" /u ").append(username);
    }
    if (password != null && password.trim().length() > 0)
    {
      command.append(" /P ").append(password);
    }
    if (pid != null && pid.trim().length() > 0)
    {
      command.append(" /PID ").append(pid);
    }
    if (imageName != null && imageName.trim().length() > 0)
    {
      command.append(" /IM ").append(imageName);
    }
    if (force)
    {
      command.append(" /F");
    }
    if (killTeam)
    {
      command.append(" /T");
    }
    for (String filter : filters)
    {
      command.append(" /FI ").append("\"").append(filter).append("\"");
    }
    return command.toString();
  }

  public void execute()
  {
    String commandStr = build();

    try
    {
      logger.debug("执行命令：{}", commandStr);
      Process process = Runtime.getRuntime().exec("cmd /c " + commandStr);
      process.waitFor();
    } catch (IOException | InterruptedException e)
    {
      logger.error("使用 TASKKILL 命令终止进程时发生异常。", e);
    }
  }


}
