
package com.intellij.psi.codeStyle;

import com.intellij.openapi.components.ServiceManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;

public abstract class CodeStyleSchemes {
  public static CodeStyleSchemes getInstance(){
    return ServiceManager.getService(CodeStyleSchemes.class);
  }

  public abstract CodeStyleScheme getCurrentScheme();

  public abstract void setCurrentScheme(CodeStyleScheme scheme);

  public abstract CodeStyleScheme createNewScheme(String preferredName, CodeStyleScheme parentScheme);

  @TestOnly
  public abstract void deleteScheme(CodeStyleScheme scheme);

  @Nullable
  protected abstract CodeStyleScheme findSchemeByName(@NotNull String name);

  /**
   * Attempts to find a scheme with a given name or an alternative suitable scheme.
   *
   * @param preferredSchemeName The scheme name to find or null for the currently selected scheme.
   * @return A found scheme or a default scheme if the scheme name was not found or, if neither exists or the scheme name is null, the
   *         currently selected scheme.
   */
  @NotNull
  public CodeStyleScheme findPreferredScheme(@Nullable String preferredSchemeName) {
    CodeStyleScheme scheme = null;
    if (preferredSchemeName != null) {
      scheme = findSchemeByName(preferredSchemeName);
    }
    if (scheme == null) {
      scheme = getCurrentScheme();
    }
    if (scheme == null) {
      scheme = getDefaultScheme();
    }
    return scheme;
  }

  public abstract CodeStyleScheme getDefaultScheme();

  public abstract void addScheme(@NotNull CodeStyleScheme currentScheme);
}

