/*
 * Copyright 2000-2010 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.openapi.vcs.actions;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorGutterAction;
import com.intellij.openapi.editor.colors.ColorKey;
import com.intellij.openapi.editor.colors.EditorFontType;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vcs.annotate.AnnotationListener;
import com.intellij.openapi.vcs.annotate.FileAnnotation;
import com.intellij.openapi.vcs.annotate.LineAnnotationAspect;
import com.intellij.openapi.vcs.annotate.TextAnnotationPresentation;
import com.intellij.openapi.vcs.history.VcsRevisionNumber;
import com.intellij.vcsUtil.VcsUtil;
import com.intellij.xml.util.XmlStringUtil;
import org.jetbrains.annotations.Nullable;

import java.awt.*;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author Irina Chernushina
 * @author Konstantin Bulenkov
 */
class AnnotationFieldGutter implements ActiveAnnotationGutter {
  protected final FileAnnotation myAnnotation;
  private final Editor myEditor;
  protected final LineAnnotationAspect myAspect;
  private final TextAnnotationPresentation myPresentation;
  private final AnnotationListener myListener;
  private final boolean myIsGutterAction;
  private Map<String, Color> myColorScheme;
  private boolean myShowBg = true;
  private boolean myShowAdditionalInfo = false;

  AnnotationFieldGutter(FileAnnotation annotation, Editor editor, LineAnnotationAspect aspect, final TextAnnotationPresentation presentation, Map<String, Color> colorScheme) {
    myAnnotation = annotation;
    myEditor = editor;
    myAspect = aspect;
    myPresentation = presentation;
    myIsGutterAction = myAspect instanceof EditorGutterAction;
    myColorScheme = colorScheme;

    myListener = new AnnotationListener() {
      public void onAnnotationChanged() {
        myEditor.getGutter().closeAllAnnotations();
      }
    };

    myAnnotation.addListener(myListener);
  }

  public boolean isGutterAction() {
    return myIsGutterAction;
  }

  public String getLineText(int line, Editor editor) {
    final String value = isAvailable() ? myAspect.getValue(line) : "";
    if (myAspect.getId() == LineAnnotationAspect.AUTHOR && ShowShortenNames.isSet()) {
      return shorten(value);
    }
    return value;
  }

  @Nullable
  private static String shorten(String value) {
    if (value != null) {
      final List<String> strings = StringUtil.split(value, " ");
      if (strings.size() > 1) {
        //Middle name check: Vasya S. Pupkin
        return strings.get(1).length() < 3 && strings.size() > 2 && strings.get(2).length() > 1 ? strings.get(2) : strings.get(1);
      }
    }
    return value;
  }

  @Nullable
  public String getToolTip(final int line, final Editor editor) {
    return isAvailable() ?
    XmlStringUtil.escapeString(myAnnotation.getToolTip(line)) : null;
  }

  public void doAction(int line) {
    if (myIsGutterAction) {
      ((EditorGutterAction)myAspect).doAction(line);
    }
  }

  public Cursor getCursor(final int line) {
    if (myIsGutterAction) {
      return ((EditorGutterAction)myAspect).getCursor(line);
    } else {
      return Cursor.getDefaultCursor();
    }

  }

  public EditorFontType getStyle(final int line, final Editor editor) {
    return myPresentation.getFontType(line);
  }

  @Nullable
  public ColorKey getColor(final int line, final Editor editor) {
    return myPresentation.getColor(line);
  }

  public List<AnAction> getPopupActions(int line, final Editor editor) {
    return myPresentation.getActions(line);
  }

  public void gutterClosed() {
    myAnnotation.removeListener(myListener);
    myAnnotation.dispose();    
    final Collection<ActiveAnnotationGutter> gutters = myEditor.getUserData(AnnotateToggleAction.KEY_IN_EDITOR);
    if (gutters != null) {
      gutters.remove(this);
    }
  }

  @Nullable
  public Color getBgColor(int line, Editor editor) {
    if (myColorScheme == null || !myShowBg) return null;
    final String s = getLineText(line, editor);
    final VcsRevisionNumber number = myAnnotation.getLineRevisionNumber(line);
    if (number == null) return null;
    final Color bg = myColorScheme.get(number.asString());
    return bg == null ? findBgColor(s) : bg;
  }

  @Nullable
  private Color findBgColor(String s) {
    if (myColorScheme != null) {
      for (String key : myColorScheme.keySet()) {
            if (key.startsWith(s)) {
              return myColorScheme.get(key);
            }
          }
    }
    return null;
  }

  public void setAspectValueToBgColorMap(Map<String, Color> colorScheme) {
    myColorScheme = colorScheme;
  }

  public void setShowBg(boolean show) {
    myShowBg = show;
  }

  public void setShowAdditionalInfo(boolean show) {
    myShowAdditionalInfo = show;
  }

  public boolean isAvailable() {
    return myShowAdditionalInfo || VcsUtil.isAspectAvailableByDefault(getID());
  }

  @Nullable
  public String getID() {
    return myAspect == null ? null : myAspect.getId();
  }
}

@@ -3296,32 +3296,405 @@
 get(1);%0A      %7D%0A
+      if (value.contains(%22.%22)) %7B%0A        strings.clear();%0A        strings.addAll(StringUtil.split(value, %22.%22));%0A        // vasya.pupkin case%0A        if (strings.size() %3E 1) %7B%0A          //Middle name check: Vasya.S.Pupkin%0A          return strings.get(1).length() == 1 && strings.size() %3E 2 && strings.get(2).length() %3E 1 ? strings.get(2) : strings.get(1);%0A        %7D%0A      %7D%0A
     %7D%0A    return

include dot to last name detector