/*
 * Copyright 2000-2014 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.vcs.log.graph;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.JBColor;
import com.intellij.util.NotNullFunction;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.hash.LinkedHashMap;
import com.intellij.vcs.log.Hash;
import com.intellij.vcs.log.VcsLogRefManager;
import com.intellij.vcs.log.VcsRef;
import com.intellij.vcs.log.data.RefsModel;
import org.jetbrains.annotations.NotNull;

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

public class GraphColorManagerImpl implements GraphColorManager {

  private static final Logger LOG = Logger.getInstance(GraphColorManagerImpl.class);
  private static final JBColor DEFAULT_COLOR = JBColor.BLACK;

  @NotNull private final RefsModel myRefsModel;
  @NotNull private final NotNullFunction<Integer, Hash> myHashGetter;
  @NotNull private final Map<VirtualFile, VcsLogRefManager> myRefManagers;

  @NotNull private final LinkedHashMap<Integer, Integer> myErrorWasReported = new LinkedHashMap<Integer, Integer>(10) {

    @Override
    protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
      return size() > 100;
    }
  };

  public GraphColorManagerImpl(@NotNull RefsModel refsModel, @NotNull NotNullFunction<Integer, Hash> hashGetter,
                               @NotNull Map<VirtualFile, VcsLogRefManager> refManagers) {
    myRefsModel = refsModel;
    myHashGetter = hashGetter;
    myRefManagers = refManagers;
  }

  @NotNull
  @Override
  public JBColor getColorOfBranch(int headCommit) {
    Collection<VcsRef> refs = myRefsModel.refsToCommit(headCommit);
    if (!checkEmptiness(refs, headCommit)) {
      return DEFAULT_COLOR;
    }
    VcsRef firstRef = getRefManager(refs).sort(refs).get(0);
    Color color = ColorGenerator.getColor(firstRef.getName().hashCode());
    // TODO dark variant
    return new JBColor(color, color);
  }

  private boolean checkEmptiness(@NotNull Collection<VcsRef> refs, int head) {
    if (refs.isEmpty()) {
      if (!myErrorWasReported.containsKey(head)) {
        myErrorWasReported.put(head, head);
        LOG.error("No references found at head " + head + " which corresponds to hash " + myHashGetter.fun(head));
      }
      return false;
    }
    return true;
  }

  @NotNull
  @Override
  public JBColor getColorOfFragment(int headCommit, int magicIndex) {
    Color color = ColorGenerator.getColor(magicIndex);
    return new JBColor(color, color);
  }

  @Override
  public int compareHeads(int head1, int head2) {
    if (head1 == head2) {
      return 0;
    }

    Collection<VcsRef> refs1 = myRefsModel.refsToCommit(head1);
    Collection<VcsRef> refs2 = myRefsModel.refsToCommit(head2);
    if (!checkEmptiness(refs1, head1)) {
      return -1;
    }
    if (!checkEmptiness(refs2, head2)) {
      return 1;
    }

    VcsLogRefManager refManager1 = getRefManager(refs1);
    VcsLogRefManager refManager2 = getRefManager(refs2);
    if (!refManager1.equals(refManager2)) {
      LOG.debug("Different ref managers (and therefore different VCSs) are not comparable");
      return 0;
    }

    Map<VcsRef, Boolean> positions = ContainerUtil.newHashMap();
    for (VcsRef ref : refs1) {
      positions.put(ref, true);
    }
    for (VcsRef ref : refs2) {
      positions.put(ref, false);
    }

    VcsRef firstRef = refManager1.sort(positions.keySet()).get(0);
    return positions.get(firstRef) ? 1 : -1;
  }

  @NotNull
  private VcsLogRefManager getRefManager(@NotNull Collection<VcsRef> refs) {
    return myRefManagers.get(refs.iterator().next().getRoot());
  }

}

@@ -2287,38 +2287,34 @@
 t);%0A    if (
-!checkEmptines
+isEmptyRef
 s(refs, head
@@ -2579,29 +2579,26 @@
 boolean 
-checkEmptines
+isEmptyRef
 s(@NotNu
@@ -2894,20 +2894,19 @@
  return 
-fals
+tru
 e;%0A    %7D
@@ -2913,27 +2913,28 @@
 %0A    return 
-tru
+fals
 e;%0A  %7D%0A%0A  @N
@@ -3367,41 +3367,142 @@
     
-if (!checkEmptines
+boolean firstEmpty = isEmptyRefs(refs1, head1);%0A    boolean secondEmpty = isEmptyRef
 s(refs
-1
+2
 , head
-1)
+2);%0A    if (firstEmpty && secondEmpty
 ) %7B%0A
@@ -3514,18 +3514,17 @@
  return 
--1
+0
 ;%0A    %7D%0A
@@ -3535,37 +3535,64 @@
 if (
-!checkEmptiness(refs2, head2)
+firstEmpty) %7B%0A      return -1;%0A    %7D%0A    if (secondEmpty
 ) %7B%0A
@@ -3776,101 +3776,8 @@
 ) %7B%0A
-      LOG.debug(%22Different ref managers (and therefore different VCSs) are not comparable%22);%0A
     

Fix transitivity in compareTo