content: package com.alphabugx.dialog;
  
  import com.alphabugx.config.AppConfig;
  import javafx.geometry.Insets;
  import javafx.scene.control.*;
  import javafx.scene.layout.GridPane;
  import javafx.stage.DirectoryChooser;
  import javafx.stage.FileChooser;
  import javafx.stage.Stage;
  
  import java.io.File;
  import java.util.Optional;
  
  /**
   * 设置对话框类
   * 用于配置JDK和CFR工具路径
   */
  public class SettingsDialog {
      
      /**
       * 显示JDK路径配置对话框
       */
      public static void showJdkConfigDialog(Stage owner) {
          Dialog<String> dialog = new Dialog<>();
          dialog.setTitle("JDK路径配置");
          dialog.setHeaderText("请选择JDK安装路径");
          dialog.initOwner(owner);
          
          // 设置对话框按钮
          ButtonType okButtonType = new ButtonType("确定", ButtonBar.ButtonData.OK_DONE);
          dialog.getDialogPane().getButtonTypes().addAll(okButtonType, ButtonType.CANCEL);
          
          // 创建界面元素
          GridPane grid = new GridPane();
          grid.setHgap(10);
          grid.setVgap(10);
          grid.setPadding(new Insets(20, 150, 10, 10));
          
          TextField jdkPathField = new TextField();
          jdkPathField.setPromptText("JDK安装路径");
          jdkPathField.setPrefWidth(300);
          
          // 加载当前配置
          AppConfig config = AppConfig.getInstance();
          jdkPathField.setText(config.getJdkPath());
          
          Button browseButton = new Button("浏览...");
          browseButton.setOnAction(e -> {
              DirectoryChooser directoryChooser = new DirectoryChooser();
              directoryChooser.setTitle("选择JDK安装目录");
              
              // 设置初始目录
              String currentPath = jdkPathField.getText();
              if (!currentPath.isEmpty() && new File(currentPath).exists()) {
                  directoryChooser.setInitialDirectory(new File(currentPath));
              }
              
              File selectedDirectory = directoryChooser.showDialog(dialog.getOwner());
              if (selectedDirectory != null) {
                  jdkPathField.setText(selectedDirectory.getAbsolutePath());
              }
          });
          
          Button autoDetectButton = new Button("自动检测");
          autoDetectButton.setOnAction(e -> {
              String detectedPath = detectJdkPath();
              if (detectedPath != null) {
                  jdkPathField.setText(detectedPath);
                  showInfo("自动检测成功", "检测到JDK路径: " + detectedPath);
              } else {
                  showWarning("自动检测失败", "无法自动检测JDK路径，请手动选择");
              }
          });
          
          Label statusLabel = new Label();
          statusLabel.setStyle("-fx-text-fill: #666666; -fx-font-size: 11px;");
          updateJdkStatus(jdkPathField.getText(), statusLabel);
          
          // 添加文本变化监听器
          jdkPathField.textProperty().addListener((obs, oldText, newText) -> {
              updateJdkStatus(newText, statusLabel);
          });
          
          grid.add(new Label("JDK路径:"), 0, 0);
          grid.add(jdkPathField, 1, 0);
          grid.add(browseButton, 2, 0);
          grid.add(autoDetectButton, 3, 0);
          grid.add(statusLabel, 1, 1, 3, 1);
          
          dialog.getDialogPane().setContent(grid);
          
          // 设置结果转换器
          dialog.setResultConverter(dialogButton -> {
              if (dialogButton == okButtonType) {
                  return jdkPathField.getText();
              }
              return null;
          });
          
          // 显示对话框并处理结果
          Optional<String> result = dialog.showAndWait();
          result.ifPresent(jdkPath -> {
              config.setJdkPath(jdkPath);
              config.saveConfig();
              showInfo("配置保存成功", "JDK路径已更新: " + jdkPath);
          });
      }
      
      /**
       * 显示CFR工具配置对话框
       */
      public static void showCfrConfigDialog(Stage owner) {
          Dialog<String> dialog = new Dialog<>();
          dialog.setTitle("CFR工具配置");
          dialog.setHeaderText("请选择CFR工具路径");
          dialog.initOwner(owner);
          
          // 设置对话框按钮
          ButtonType okButtonType = new ButtonType("确定", ButtonBar.ButtonData.OK_DONE);
          dialog.getDialogPane().getButtonTypes().addAll(okButtonType, ButtonType.CANCEL);
          
          // 创建界面元素
          GridPane grid = new GridPane();
          grid.setHgap(10);
          grid.setVgap(10);
          grid.setPadding(new Insets(20, 150, 10, 10));
          
          TextField cfrPathField = new TextField();
          cfrPathField.setPromptText("CFR工具路径");
          cfrPathField.setPrefWidth(300);
          
          // 加载当前配置
          AppConfig config = AppConfig.getInstance();
          cfrPathField.setText(config.getCfrPath());
          
          Button browseButton = new Button("浏览...");
          browseButton.setOnAction(e -> {
              FileChooser fileChooser = new FileChooser();
              fileChooser.setTitle("选择CFR工具");
              fileChooser.getExtensionFilters().add(
                  new FileChooser.ExtensionFilter("Jar文件", "*.jar")
              );
              
              // 设置初始目录
              String currentPath = cfrPathField.getText();
              if (!currentPath.isEmpty()) {
                  File currentFile = new File(currentPath);
                  if (currentFile.getParentFile() != null && currentFile.getParentFile().exists()) {
                      fileChooser.setInitialDirectory(currentFile.getParentFile());
                  }
              }
              
              File selectedFile = fileChooser.showOpenDialog(dialog.getOwner());
              if (selectedFile != null) {
                  cfrPathField.setText(selectedFile.getAbsolutePath());
              }
          });
          
          Button defaultButton = new Button("使用默认");
          defaultButton.setOnAction(e -> {
              cfrPathField.setText("cfr-0.152.jar");
          });
          
          Label statusLabel = new Label();
          statusLabel.setStyle("-fx-text-fill: #666666; -fx-font-size: 11px;");
          updateCfrStatus(cfrPathField.getText(), statusLabel);
          
          // 添加文本变化监听器
          cfrPathField.textProperty().addListener((obs, oldText, newText) -> {
              updateCfrStatus(newText, statusLabel);
          });
          
          grid.add(new Label("CFR路径:"), 0, 0);
          grid.add(cfrPathField, 1, 0);
          grid.add(browseButton, 2, 0);
          grid.add(defaultButton, 3, 0);
          grid.add(statusLabel, 1, 1, 3, 1);
          
          dialog.getDialogPane().setContent(grid);
          
          // 设置结果转换器
          dialog.setResultConverter(dialogButton -> {
              if (dialogButton == okButtonType) {
                  return cfrPathField.getText();
              }
              return null;
          });
          
          // 显示对话框并处理结果
          Optional<String> result = dialog.showAndWait();
          result.ifPresent(cfrPath -> {
              config.setCfrPath(cfrPath);
              config.saveConfig();
              showInfo("配置保存成功", "CFR工具路径已更新: " + cfrPath);
          });
      }
      
      /**
       * 更新JDK状态信息
       */
      private static void updateJdkStatus(String jdkPath, Label statusLabel) {
          if (jdkPath == null || jdkPath.trim().isEmpty()) {
              statusLabel.setText("状态: 未配置JDK路径");
              statusLabel.setStyle("-fx-text-fill: #ff6666; -fx-font-size: 11px;");
              return;
          }
          
          File jdkDir = new File(jdkPath);
          if (!jdkDir.exists() || !jdkDir.isDirectory()) {
              statusLabel.setText("状态: 路径不存在");
              statusLabel.setStyle("-fx-text-fill: #ff6666; -fx-font-size: 11px;");
              return;
          }
          
          // 检查是否包含javac
          String osName = System.getProperty("os.name").toLowerCase();
          String javacExe = osName.contains("win") ? "javac.exe" : "javac";
          File javacFile = new File(jdkDir, "bin" + File.separator + javacExe);
          
          if (javacFile.exists()) {
              statusLabel.setText("状态: JDK路径有效");
              statusLabel.setStyle("-fx-text-fill: #00aa00; -fx-font-size: 11px;");
          } else {
              statusLabel.setText("状态: 路径无效，未找到javac");
              statusLabel.setStyle("-fx-text-fill: #ff6666; -fx-font-size: 11px;");
          }
      }
      
      /**
       * 更新CFR状态信息
       */
      private static void updateCfrStatus(String cfrPath, Label statusLabel) {
          if (cfrPath == null || cfrPath.trim().isEmpty()) {
              statusLabel.setText("状态: 未配置CFR工具路径");
              statusLabel.setStyle("-fx-text-fill: #ff6666; -fx-font-size: 11px;");
              return;
          }
          
          File cfrFile = new File(cfrPath);
          if (!cfrFile.exists() || !cfrFile.isFile()) {
              statusLabel.setText("状态: CFR工具不存在");
              statusLabel.setStyle("-fx-text-fill: #ff6666; -fx-font-size: 11px;");
              return;
          }
          
          if (cfrFile.getName().toLowerCase().endsWith(".jar")) {
              statusLabel.setText("状态: CFR工具有效");
              statusLabel.setStyle("-fx-text-fill: #00aa00; -fx-font-size: 11px;");
          } else {
              statusLabel.setText("状态: 文件类型错误，需要jar文件");
              statusLabel.setStyle("-fx-text-fill: #ff6666; -fx-font-size: 11px;");
          }
      }
      
      /**
       * 自动检测JDK路径
       */
      private static String detectJdkPath() {
          // 首先尝试JAVA_HOME环境变量
          String javaHome = System.getenv("JAVA_HOME");
          if (javaHome != null && isValidJdkPath(javaHome)) {
              return javaHome;
          }
          
          // 尝试从java.home系统属性
          javaHome = System.getProperty("java.home");
          if (javaHome != null) {
              // 如果是JRE路径，尝试找到父目录的JDK
              if (javaHome.endsWith("jre")) {
                  File jdkPath = new File(javaHome).getParentFile();
                  if (jdkPath != null && isValidJdkPath(jdkPath.getAbsolutePath())) {
                      return jdkPath.getAbsolutePath();
                  }
              }
              
              if (isValidJdkPath(javaHome)) {
                  return javaHome;
              }
          }
          
          // 在常见位置搜索JDK
          String[] commonPaths = {
              "C:\\Program Files\\Java",
              "C:\\Program Files (x86)\\Java",
              "/usr/lib/jvm",
              "/usr/java",
              "/opt/java",
              "/Library/Java/JavaVirtualMachines"
          };
          
          for (String basePath : commonPaths) {
              File baseDir = new File(basePath);
              if (baseDir.exists() && baseDir.isDirectory()) {
                  File[] jdkDirs = baseDir.listFiles((dir, name) -> 
                      name.toLowerCase().contains("jdk"));
                  
                  if (jdkDirs != null) {
                      for (File jdkDir : jdkDirs) {
                          if (isValidJdkPath(jdkDir.getAbsolutePath())) {
                              return jdkDir.getAbsolutePath();
                          }
                      }
                  }
              }
          }
          
          return null;
      }
      
      /**
       * 验证JDK路径是否有效
       */
      private static boolean isValidJdkPath(String jdkPath) {
          if (jdkPath == null || jdkPath.trim().isEmpty()) {
              return false;
          }
          
          File jdkDir = new File(jdkPath);
          if (!jdkDir.exists() || !jdkDir.isDirectory()) {
              return false;
          }
          
          // 检查是否包含javac
          String osName = System.getProperty("os.name").toLowerCase();
          String javacExe = osName.contains("win") ? "javac.exe" : "javac";
          File javacFile = new File(jdkDir, "bin" + File.separator + javacExe);
          
          return javacFile.exists();
      }
      
      /**
       * 显示信息对话框
       */
      private static void showInfo(String title, String message) {
          Alert alert = new Alert(Alert.AlertType.INFORMATION);
          alert.setTitle(title);
          alert.setHeaderText(null);
          alert.setContentText(message);
          alert.showAndWait();
      }
      
      /**
       * 显示警告对话框
       */
      private static void showWarning(String title, String message) {
          Alert alert = new Alert(Alert.AlertType.WARNING);
          alert.setTitle(title);
          alert.setHeaderText(null);
          alert.setContentText(message);
          alert.showAndWait();
      }
  } 