package com.intellij.coverage.view;

import com.intellij.coverage.CoverageEngine;
import com.intellij.coverage.CoverageSuitesBundle;
import com.intellij.ide.util.treeView.AbstractTreeNode;
import com.intellij.ide.util.treeView.NodeDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import com.intellij.util.ui.ColumnInfo;

import java.util.Comparator;

class PercentageCoverageColumnInfo extends ColumnInfo<NodeDescriptor, String> {
  private final int myColumnIdx;
  private final Comparator<NodeDescriptor> myComparator;
  private final CoverageSuitesBundle mySuitesBundle;
  private final CoverageViewManager.StateBean myStateBean;

  PercentageCoverageColumnInfo(int columnIdx,
                               String name,
                               final CoverageSuitesBundle suitesBundle,
                               CoverageViewManager.StateBean stateBean) {
    super(name);
    this.myColumnIdx = columnIdx;
    myComparator = (o1, o2) -> {
      final String val1 = valueOf(o1);
      final String val2 = valueOf(o2);
      if (val1 != null && val2 != null) {
        final int percentageIndex1 = val1.indexOf('%');
        final int percentageIndex2 = val2.indexOf('%');
        if (percentageIndex1 > -1 && percentageIndex2 >-1) {
          final String percentage1 = val1.substring(0, percentageIndex1);
          final String percentage2 = val2.substring(0, percentageIndex2);
          final int compare = Comparing.compare(Integer.parseInt(percentage1), Integer.parseInt(percentage2));
          if (compare == 0) {
            final int total1 = val1.indexOf('/');
            final int total2 = val2.indexOf('/');
            if (total1 > -1 && total2 > -1) {
              final int r1 = val1.indexOf(')', total1);
              final int r2 = val2.indexOf(')', total2);
              if (r1 > -1 && r2 > -1) {
                return Integer.parseInt(val2.substring(total2 + 1, r2)) - Integer.parseInt(val1.substring(total1 + 1, r1)) ;
              }
            }
          }
          return compare;
        }
        if (percentageIndex1 > -1) return 1;
        if (percentageIndex2 > -1) return -1;
      }
      return Comparing.compare(val1, val2);
    };
    mySuitesBundle = suitesBundle;
    myStateBean = stateBean;
  }

  @Override
  public String valueOf(NodeDescriptor node) {
    final CoverageEngine coverageEngine = mySuitesBundle.getCoverageEngine();
    final Project project = node.getProject();
    return coverageEngine.createCoverageViewExtension(project, mySuitesBundle, myStateBean).getPercentage(myColumnIdx, (AbstractTreeNode)node);
  }

  @Override
  public Comparator<NodeDescriptor> getComparator() {
    return myComparator;
  }
}

@@ -1,8 +1,609 @@
+/*%0A * Copyright 2000-2017 JetBrains s.r.o.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 
@@ -2093,55 +2093,59 @@
 are(
-Integer.parseInt(percentage1), Integer.parseInt
+Double.parseDouble(percentage1), Double.parseDouble
 (per
@@ -2511,29 +2511,49 @@
 urn 
-Integer.parseInt
+Comparing.compare(Double.parseDouble
 (val
-2
+1
 .sub
@@ -2568,43 +2568,85 @@
 otal
-2
+1
  + 1, r
-2)) - Integer.parseInt
+1)),%0A                                         Double.parseDouble
 (val
-1
+2
 .sub
@@ -2661,20 +2661,20 @@
 otal
-1
+2
  + 1, r
-1
+2)
 ))
- 
 ;%0A  

allow double percentage values while sorting coverage results (EA-103300)