// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.


package org.jetbrains.idea.svn;

import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.options.ConfigurableBase;
import com.intellij.openapi.options.ConfigurableUi;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.util.Consumer;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.svn.dialogs.SshSettingsPanel;

import java.awt.*;
import java.io.File;
import java.util.function.Supplier;

import static org.jetbrains.idea.svn.SvnBundle.message;

public abstract class SvnConfigurable extends ConfigurableBase<ConfigurableUi<SvnConfiguration>, SvnConfiguration> {
  static final @NonNls String ID = "vcs.Subversion";
  static final @NonNls String HELP_ID = "project.propSubversion";

  private final @NotNull Project myProject;
  private final @NotNull Supplier<? extends ConfigurableUi<SvnConfiguration>> myUiSupplier;

  public static @NlsContexts.ConfigurableName @NotNull String getGroupDisplayName() {
    return SvnVcs.VCS_DISPLAY_NAME;
  }

  protected SvnConfigurable(@NotNull Project project,
                            @NonNls @NotNull String idSuffix,
                            @NlsContexts.ConfigurableName @NotNull String displayName,
                            @NotNull Supplier<? extends ConfigurableUi<SvnConfiguration>> uiSupplier) {
    this(project, ID + "." + idSuffix, displayName, uiSupplier, HELP_ID + "." + idSuffix);
  }

  protected SvnConfigurable(@NotNull Project project,
                            @NonNls @NotNull String id,
                            @NlsContexts.ConfigurableName @NotNull String displayName,
                            @NotNull Supplier<? extends ConfigurableUi<SvnConfiguration>> uiSupplier,
                            @NonNls @NotNull String helpId) {
    super(id, displayName, helpId);
    myProject = project;
    myUiSupplier = uiSupplier;
  }

  @Override
  protected ConfigurableUi<SvnConfiguration> createUi() {
    return myUiSupplier.get();
  }

  @Override
  protected @NotNull SvnConfiguration getSettings() {
    return SvnConfiguration.getInstance(myProject);
  }

  public static class Ssh extends SvnConfigurable {
    public Ssh(@NotNull Project project) {
      super(project, "SSH", message("configurable.name.svn.ssh"), () -> new SshSettingsPanel(project));
    }
  }

  public static void selectConfigurationDirectory(@NotNull String path,
                                                  final @NotNull Consumer<? super String> dirConsumer,
                                                  final Project project,
                                                  final @Nullable Component component) {
    FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor()
      .withTitle(message("dialog.title.select.configuration.directory"))
      .withDescription(message("dialog.description.select.configuration.directory"))
      .withShowFileSystemRoots(true)
      .withHideIgnored(false)
      .withShowHiddenFiles(true);

    path = "file://" + path.replace(File.separatorChar, '/');
    VirtualFile root = VirtualFileManager.getInstance().findFileByUrl(path);

    VirtualFile file = FileChooser.chooseFile(descriptor, component, project, root);
    if (file == null) {
      return;
    }
    final String resultPath = file.getPath().replace('/', File.separatorChar);
    dirConsumer.consume(resultPath);
  }
}