package jetbrains.mps.vcs.annotate;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import java.util.Map;
import com.intellij.openapi.vcs.history.VcsFileRevision;
import jetbrains.mps.internal.collections.runtime.MapSequence;
import java.util.HashMap;
import jetbrains.mps.vcs.history.CommitsGraphNode;
import java.util.List;
import jetbrains.mps.internal.collections.runtime.ListSequence;

@GeneratedClass(node = "r:f509a650-cbd9-47e7-b2a0-79f49c562c0b(jetbrains.mps.vcs.annotate)/6566909625908996008", model = "r:f509a650-cbd9-47e7-b2a0-79f49c562c0b(jetbrains.mps.vcs.annotate)")
public class CommitNumberSubcolumn extends AnnotationAspectSubcolumn {

  private Map<VcsFileRevision, Integer> myRevisionsToNumbers = MapSequence.fromMap(new HashMap<VcsFileRevision, Integer>());


  public CommitNumberSubcolumn(EditorAnnotation editorAnnotation) {
    super(editorAnnotation, "Commit number");
    myRightAligned = true;
  }

  @Override
  public String getText(CommitsGraphNode graphNode) {
    List<VcsFileRevision> revisions = myEditorAnnotation.getAllRevisions();
    if (MapSequence.fromMap(myRevisionsToNumbers).count() != ListSequence.fromList(revisions).count()) {
      calcNumbers(revisions);
    }
    return "" + MapSequence.fromMap(myRevisionsToNumbers).get(graphNode.getRevision());
  }

  private void calcNumbers(List<VcsFileRevision> revisions) {
    myRevisionsToNumbers = MapSequence.fromMap(new HashMap<VcsFileRevision, Integer>());
    for (int i = ListSequence.fromList(revisions).count(), j = 0; i > 0; i--, j++) {
      MapSequence.fromMap(myRevisionsToNumbers).put(ListSequence.fromList(revisions).getElement(j), i);
    }
  }
}
