package org.jetbrains.debugger;

import com.intellij.xdebugger.frame.XCompositeNode;
import com.intellij.xdebugger.frame.XValueChildrenList;
import com.intellij.xdebugger.frame.XValueGroup;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.debugger.values.ObjectValue;
import org.jetbrains.debugger.values.ValueType;

import java.util.ArrayList;
import java.util.List;

public final class LazyVariablesGroup extends XValueGroup {
  public static final ValueGroupFactory<ObjectValue> GROUP_FACTORY = new ValueGroupFactory<ObjectValue>() {
    @Override
    public XValueGroup create(@NotNull ObjectValue value, int start, int end, @NotNull VariableContext context) {
      return new LazyVariablesGroup(value, start, end, context);
    }
  };

  private final ObjectValue value;

  private final int start;
  private final int end;
  private final VariableContext context;

  private final ValueType componentType;
  private final boolean sparse;

  public LazyVariablesGroup(@NotNull ObjectValue value, int start, int end, @NotNull VariableContext context) {
    this(value, start, end, context, null, true);
  }

  public LazyVariablesGroup(@NotNull ObjectValue value, int start, int end, @NotNull VariableContext context, @Nullable ValueType componentType, boolean sparse) {
    super(String.format("[%,d \u2026 %,d]", start, end));

    this.value = value;

    this.start = start;
    this.end = end;

    this.context = context;

    this.componentType = componentType;
    this.sparse = sparse;
  }

  @Override
  public void computeChildren(@NotNull XCompositeNode node) {
    node.setAlreadySorted(true);

    int bucketThreshold = XCompositeNode.MAX_CHILDREN_TO_SHOW;
    int size = end - start;
    if (!sparse && size > bucketThreshold) {
      node.addChildren(XValueChildrenList.topGroups(computeNotSparseGroups(value, context, start, end, bucketThreshold)), true);
      return;
    }

    value.getIndexedProperties(start, end + 1, bucketThreshold, new VariableView.ObsolescentIndexedVariablesConsumer(node) {
      @Override
      public void consumeRanges(@Nullable int[] ranges) {
        if (ranges == null) {
          XValueChildrenList groupList = new XValueChildrenList();
          addGroups(value, GROUP_FACTORY, groupList, start, end, XCompositeNode.MAX_CHILDREN_TO_SHOW, context);
          node.addChildren(groupList, true);
        }
        else {
          addRanges(value, ranges, node, context, true);
        }
      }

      @Override
      public void consumeVariables(@NotNull List<Variable> variables) {
        node.addChildren(Variables.createVariablesList(variables, context), true);
      }
    }, componentType);
  }

  @NotNull
  public static List<XValueGroup> computeNotSparseGroups(@NotNull ObjectValue value, @NotNull VariableContext context, int from, int to, int bucketThreshold) {
    int size = to - from;
    int bucketSize = (int)Math.pow(bucketThreshold, Math.ceil(Math.log(size) / Math.log(bucketThreshold)) - 1);
    List<XValueGroup> groupList = new ArrayList<XValueGroup>((int)Math.ceil(size / bucketSize));
    for (; from < to; from += bucketSize) {
      groupList.add(new LazyVariablesGroup(value, from, from + (Math.min(bucketSize, to - from) - 1), context, ValueType.NUMBER, false));
    }
    return groupList;
  }

  public static void addRanges(@NotNull ObjectValue value, int[] ranges, @NotNull XCompositeNode node, @NotNull VariableContext context, boolean isLast) {
    XValueChildrenList groupList = new XValueChildrenList(ranges.length / 2);
    for (int i = 0, n = ranges.length; i < n; i += 2) {
      groupList.addTopGroup(new LazyVariablesGroup(value, ranges[i], ranges[i + 1], context));
    }
    node.addChildren(groupList, isLast);
  }

  public static <T> void addGroups(@NotNull T data,
                                   @NotNull ValueGroupFactory<T> groupFactory,
                                   @NotNull XValueChildrenList groupList,
                                   int from,
                                   int limit,
                                   int bucketSize,
                                   @NotNull VariableContext context) {
    int to = Math.min(bucketSize, limit);
    boolean done = false;
    do {
      int groupFrom = from;
      int groupTo = to;

      from += bucketSize;
      to = from + Math.min(bucketSize, limit - from);

      // don't create group for only one member
      if (to - from == 1) {
        groupTo++;
        done = true;
      }
      groupList.addTopGroup(groupFactory.create(data, groupFrom, groupTo, context));
      if (from >= limit) {
        break;
      }
    }
    while (!done);
  }
}
@@ -849,24 +849,33 @@
 al int start
+Inclusive
 ;%0A  private 
@@ -887,16 +887,25 @@
  int end
+Inclusive
 ;%0A  priv
@@ -1075,33 +1075,51 @@
 e, int start
-, int end
+Inclusive, int endInclusive
 , @NotNull V
@@ -1157,37 +1157,55 @@
 his(value, start
-, end
+Inclusive, endInclusive
 , context, null,
@@ -1278,33 +1278,51 @@
 e, int start
-, int end
+Inclusive, int endInclusive
 , @NotNull V
@@ -1447,21 +1447,39 @@
 %22, start
-, end
+Inclusive, endInclusive
 ));%0A%0A   
@@ -1518,36 +1518,72 @@
 tart
- = start;%0A    this.end = end
+Inclusive = startInclusive;%0A    this.endInclusive = endInclusive
 ;%0A%0A 
@@ -1861,54 +1861,53 @@
    i
-nt size = end - start;%0A    if (!sparse && size
+f (!sparse && (endInclusive - startInclusive)
  %3E b
@@ -2020,21 +2020,43 @@
 t, start
-, end
+Inclusive, endInclusive + 1
 , bucket
@@ -2132,21 +2132,39 @@
 es(start
-, end
+Inclusive, endInclusive
  + 1, bu
@@ -2468,21 +2468,39 @@
 t, start
-, end
+Inclusive, endInclusive
 , XCompo
@@ -3035,16 +3035,34 @@
 from
-, int to
+Inclusive, int toExclusive
 , in
@@ -3099,23 +3099,41 @@
 ize = to
- - from
+Exclusive - fromInclusive
 ;%0A    in
@@ -3354,19 +3354,46 @@
 from
- %3C to; from
+Inclusive %3C toExclusive; fromInclusive
  += 
@@ -3460,22 +3460,40 @@
 ue, from
-, from
+Inclusive, fromInclusive
  + (Math
@@ -3511,23 +3511,41 @@
 Size, to
- - from
+Exclusive - fromInclusive
 ) - 1), 

fix computeNotSparseGroups — expected exclusive "to", so, we should pass "endInclusive + 1"