package xdean.tool.sys.other;

import java.io.IOException;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import xdean.tool.api.Tool;
import xdean.tool.api.impl.AbstractToolItem;
import xdean.tool.api.impl.TextToolItem;
import xdean.tool.sys.SystemTools;
import xdean.tool.sys.Util;

@Tool(parent = SystemTools.class)
public class ShutdownTool extends TextToolItem {

  public ShutdownTool() {
    super("Shutdown");
  }

  @Tool(parent = ShutdownTool.class)
  public static class TimedShutdown extends AbstractToolItem {
    private static final String DO = "Timed";
    private static final String CANCEL = "Cancel";
    BooleanProperty timed = new SimpleBooleanProperty(false);

    public TimedShutdown() {
      super(DO);
      timed.addListener((ob, o, n) -> textProperty().set(n ? CANCEL : DO));
    }

    @Override
    public void onClick() {
      if (timed.get()) {
        try {
          int result = Runtime.getRuntime().exec("cmd /c shutdown -a").waitFor();
          if (result == 0) {
            Util.showMessage("Cancel success.");
            timed.set(false);
          } else {
            Util.showMessage("Cancel fail");
          }
        } catch (InterruptedException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
      } else {
        Util.showInputInteger("Input shutdown delay in second")
            .ifPresent(i -> {
              try {
                int result = Runtime.getRuntime().exec("cmd /c shutdown -s -t " + i).waitFor();
                if (result == 0) {
                  timed.set(true);
                }
              } catch (InterruptedException | IOException e) {
                e.printStackTrace();
              }
            });
      }
    }
  }
}

@@ -236,50 +236,8 @@
 m;%0D%0A
-import xdean.tool.api.impl.TextToolItem;%0D%0A
 impo
@@ -335,72 +335,43 @@
 lass
-)%0D%0Apublic class ShutdownTool extends TextToolItem %7B%0D%0A%0D%0A  
+, path = %22Shutdown%22)%0D%0A
 public 
+class 
 Shut
@@ -382,41 +382,10 @@
 Tool
-() %7B%0D%0A    super(%22Shutdown%22);%0D%0A  %7D
+ %7B
 %0D%0A%0D%0A

update shutdown