package jetbrains.mps.vcs.diff.ui.common;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import java.util.Map;
import jetbrains.mps.vcs.diff.changes.ChangeType;
import java.awt.Color;
import java.util.EnumMap;
import jetbrains.mps.internal.collections.runtime.MapSequence;
import com.intellij.openapi.vcs.FileStatus;
import com.intellij.openapi.application.ApplicationManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.editor.colors.EditorColors;

@GeneratedClass(node = "r:07568eb8-30c0-4bb3-9dcb-50ee4b8de59a(jetbrains.mps.vcs.diff.ui.common)/4652592318748335554", model = "r:07568eb8-30c0-4bb3-9dcb-50ee4b8de59a(jetbrains.mps.vcs.diff.ui.common)")
public class ChangeColors {
  private final Map<ChangeType, Color> myDiffColors = new EnumMap<ChangeType, Color>(ChangeType.class);
  private final Map<ChangeType, Color> myTreeColors = new EnumMap<ChangeType, Color>(ChangeType.class);
  private final Map<ChangeType, Color> myGutterColors = new EnumMap<ChangeType, Color>(ChangeType.class);
  private final Map<ChangeType, Color> myErrorStripeColors = new EnumMap<ChangeType, Color>(ChangeType.class);
  private final Color MOVE_COLOR = new Color(255, 255, 224);


  private ChangeColors() {
    updateEditorColors(null);
    MapSequence.fromMap(myTreeColors).put(ChangeType.ADD, FileStatus.ADDED.getColor());
    MapSequence.fromMap(myTreeColors).put(ChangeType.DELETE, FileStatus.DELETED.getColor());
    MapSequence.fromMap(myTreeColors).put(ChangeType.CHANGE, FileStatus.MODIFIED.getColor());
    MapSequence.fromMap(myTreeColors).put(ChangeType.CONFLICTED, FileStatus.MERGED_WITH_CONFLICTS.getColor());
  }

  public static ChangeColors getInstance() {
    return ApplicationManager.getApplication().getService(ChangeColors.class);
  }

  public Color getDiffColor(@NotNull ChangeType changeType) {
    return MapSequence.fromMap(myDiffColors).get(changeType);
  }

  public Color getGutterColor(@NotNull ChangeType changeType) {
    return MapSequence.fromMap(myGutterColors).get(changeType);
  }

  public Color getErrorStripeColor(@NotNull ChangeType changeType) {
    return MapSequence.fromMap(myErrorStripeColors).get(changeType);
  }

  @NotNull
  public Color getTreeColor(@NotNull ChangeType changeType) {
    return MapSequence.fromMap(myTreeColors).get(changeType);
  }

  /*package*/ void updateEditorColors(@Nullable EditorColorsScheme scheme) {

    if (scheme == null) {
      if (ApplicationManager.getApplication() == null) {
        return;
      }
      scheme = EditorColorsManager.getInstance().getGlobalScheme();
    }
    TextAttributes taDiffInsert = scheme.getAttributes(TextAttributesKey.find("DIFF_INSERTED"));
    TextAttributes taDiffDelete = scheme.getAttributes(TextAttributesKey.find("DIFF_DELETED"));
    TextAttributes taDiffChange = scheme.getAttributes(TextAttributesKey.find("DIFF_MODIFIED"));
    TextAttributes taDiffConflict = scheme.getAttributes(TextAttributesKey.find("DIFF_CONFLICT"));

    MapSequence.fromMap(myDiffColors).put(ChangeType.ADD, taDiffInsert.getBackgroundColor());

    MapSequence.fromMap(myDiffColors).put(ChangeType.DELETE, taDiffDelete.getBackgroundColor());

    MapSequence.fromMap(myDiffColors).put(ChangeType.CHANGE, taDiffChange.getBackgroundColor());

    MapSequence.fromMap(myDiffColors).put(ChangeType.CONFLICTED, taDiffConflict.getBackgroundColor());

    MapSequence.fromMap(myDiffColors).put(ChangeType.MOVE, MOVE_COLOR);

    MapSequence.fromMap(myGutterColors).put(ChangeType.ADD, scheme.getColor(EditorColors.ADDED_LINES_COLOR));

    MapSequence.fromMap(myGutterColors).put(ChangeType.DELETE, scheme.getColor(EditorColors.DELETED_LINES_COLOR));

    MapSequence.fromMap(myGutterColors).put(ChangeType.CHANGE, scheme.getColor(EditorColors.MODIFIED_LINES_COLOR));

    MapSequence.fromMap(myGutterColors).put(ChangeType.MOVE, MOVE_COLOR);

    MapSequence.fromMap(myErrorStripeColors).put(ChangeType.ADD, taDiffInsert.getErrorStripeColor());
    MapSequence.fromMap(myErrorStripeColors).put(ChangeType.DELETE, taDiffDelete.getErrorStripeColor());
    MapSequence.fromMap(myErrorStripeColors).put(ChangeType.CHANGE, taDiffChange.getErrorStripeColor());
    MapSequence.fromMap(myErrorStripeColors).put(ChangeType.CONFLICTED, taDiffConflict.getErrorStripeColor());
    MapSequence.fromMap(myErrorStripeColors).put(ChangeType.MOVE, MOVE_COLOR);

  }
}
