// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ide.favoritesTreeView;

import com.intellij.ide.dnd.aware.DnDAwareTree;
import com.intellij.ide.util.treeView.AbstractTreeNode;
import com.intellij.ui.treeStructure.Tree;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FavoritesTreeUtil {
  public static FavoriteTreeNodeDescriptor @NotNull [] getSelectedNodeDescriptors(final DnDAwareTree tree) {
    TreePath[] path = tree.getSelectionPaths();
    if (path == null) {
      return FavoriteTreeNodeDescriptor.EMPTY_ARRAY;
    }
    ArrayList<FavoriteTreeNodeDescriptor> result = new ArrayList<>();
    for (TreePath treePath : path) {
      DefaultMutableTreeNode lastPathNode = (DefaultMutableTreeNode)treePath.getLastPathComponent();
      Object userObject = lastPathNode.getUserObject();
      if (!(userObject instanceof FavoriteTreeNodeDescriptor)) {
        continue;
      }
      FavoriteTreeNodeDescriptor treeNodeDescriptor = (FavoriteTreeNodeDescriptor)userObject;
      result.add(treeNodeDescriptor);
    }
    return result.toArray(FavoriteTreeNodeDescriptor.EMPTY_ARRAY);
  }

  public static List<AbstractTreeNode<?>> getLogicalPathToSelected(final Tree tree) {
    final List<AbstractTreeNode<?>> result = new ArrayList<>();
    final TreePath selectionPath = tree.getSelectionPath();
    return getLogicalPathTo(result, selectionPath);
  }

  public static List<Integer> getLogicalIndexPathTo(TreePath selectionPath) {
    final List<Integer> result = new ArrayList<>();
    final Object component = selectionPath.getLastPathComponent();
    if (component instanceof DefaultMutableTreeNode) {
      final Object uo = ((DefaultMutableTreeNode)component).getUserObject();
      if (uo instanceof FavoriteTreeNodeDescriptor) {
        AbstractTreeNode treeNode = ((FavoriteTreeNodeDescriptor)uo).getElement();
        while ((!(treeNode instanceof FavoritesListNode)) && treeNode != null) {
//          final int idx = getIndex(treeNode.getParent().getChildren(), treeNode);
//          if (idx == -1) return null;
          result.add(treeNode.getIndex());
          treeNode = treeNode.getParent();
        }
        Collections.reverse(result);
        return result;
      }
    }
    return Collections.emptyList();
  }

  /*private static int getIndex(Collection<AbstractTreeNode<?>> children, AbstractTreeNode node) {
    int idx = 0;
    for (AbstractTreeNode child : children) {
      if (child == node) {
        return idx;
      }
      ++ idx;
    }
    assert false;
    return -1;
  }*/

  public static List<AbstractTreeNode<?>> getLogicalPathTo(List<AbstractTreeNode<?>> result, TreePath selectionPath) {
    final Object component = selectionPath.getLastPathComponent();
    if (component instanceof DefaultMutableTreeNode) {
      final Object uo = ((DefaultMutableTreeNode)component).getUserObject();
      if (uo instanceof FavoriteTreeNodeDescriptor) {
        AbstractTreeNode treeNode = ((FavoriteTreeNodeDescriptor)uo).getElement();
        while ((!(treeNode instanceof FavoritesListNode)) && treeNode != null) {
          result.add(treeNode);
          treeNode = treeNode.getParent();
        }
        Collections.reverse(result);
        return result;
      }
    }
    return Collections.emptyList();
  }

  @Nullable
  public static FavoritesListNode extractParentList(FavoriteTreeNodeDescriptor descriptor) {
    AbstractTreeNode current = descriptor.getElement();
    while (current != null) {
      if (current instanceof FavoritesListNode) {
        return (FavoritesListNode)current;
      }
      current = current.getParent();
    }
    return null;
  }

  static FavoritesListProvider getProvider(@NotNull FavoritesManager manager, @NotNull FavoriteTreeNodeDescriptor descriptor) {
    AbstractTreeNode treeNode = descriptor.getElement();
    while (treeNode != null && (!(treeNode instanceof FavoritesListNode))) {
      treeNode = treeNode.getParent();
    }
    if (treeNode != null) {
      final String name = ((FavoritesListNode)treeNode).getValue();
      return manager.getListProvider(name);
    }
    return null;
  }
}

@@ -1685,1167 +1685,8 @@
  %7D%0A%0A
-  public static List%3CInteger%3E getLogicalIndexPathTo(TreePath selectionPath) %7B%0A    final List%3CInteger%3E result = new ArrayList%3C%3E();%0A    final Object component = selectionPath.getLastPathComponent();%0A    if (component instanceof DefaultMutableTreeNode) %7B%0A      final Object uo = ((DefaultMutableTreeNode)component).getUserObject();%0A      if (uo instanceof FavoriteTreeNodeDescriptor) %7B%0A        AbstractTreeNode treeNode = ((FavoriteTreeNodeDescriptor)uo).getElement();%0A        while ((!(treeNode instanceof FavoritesListNode)) && treeNode != null) %7B%0A//          final int idx = getIndex(treeNode.getParent().getChildren(), treeNode);%0A//          if (idx == -1) return null;%0A          result.add(treeNode.getIndex());%0A          treeNode = treeNode.getParent();%0A        %7D%0A        Collections.reverse(result);%0A        return result;%0A      %7D%0A    %7D%0A    return Collections.emptyList();%0A  %7D%0A%0A  /*private static int getIndex(Collection%3CAbstractTreeNode%3C?%3E%3E children, AbstractTreeNode node) %7B%0A    int idx = 0;%0A    for (AbstractTreeNode child : children) %7B%0A      if (child == node) %7B%0A        return idx;%0A      %7D%0A      ++ idx;%0A    %7D%0A    assert false;%0A    return -1;%0A  %7D*/%0A%0A
   pu

remove unused code