package com.bonus.canteen.core.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.bonus.canteen.core.menu.domain.BaseTreeNode;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TreeNodeUtil {
   public static <T extends BaseTreeNode> List<T> getParentList(List<T> listNodes, Long id) {
      if (!ObjectUtil.isEmpty(id) && !CollectionUtil.isEmpty(listNodes)) {
         List<T> treeNodes = new ArrayList();
         int length = listNodes.size();
         byte[] nodeIndex = new byte[length];

         for(int i = 0; i < length; ++i) {
            T t = listNodes.get(i);
            if (id.equals(t.getId()) && nodeIndex[i] == 0) {
               nodeIndex[i] = 1;
               treeNodes.add(t);
               id = t.getParentId();
               if (ObjectUtil.isEmpty(id) || id == -1L) {
                  break;
               }

               i = -1;
            }
         }

         return treeNodes;
      } else {
         return null;
      }
   }

   public static <T extends BaseTreeNode> List<T> getChildList(List<T> listNodes, List<Long> ids) {
      return CollectionUtil.isEmpty(ids) ? null : getChildList(listNodes, (Long[])ids.toArray(new Long[0]));
   }

   public static <T extends BaseTreeNode> List<T> getChildList(List<T> listNodes, Long id) {
      return ObjectUtil.isEmpty(id) ? null : getChildList(listNodes, new Long[]{id});
   }

   public static <T extends BaseTreeNode> List<T> getChildList(List<T> listNodes, Long[] ids) {
      if (ids != null && ids.length != 0 && !CollectionUtil.isEmpty(listNodes)) {
         List<T> treeNodes = new ArrayList();
         int length = listNodes.size();
         byte[] nodeIndex = new byte[length];
         Long[] var6 = ids;
         int index = ids.length;

         BaseTreeNode t;
         int i;
         for(i = 0; i < index; ++i) {
            Long id = var6[i];

            for(int j = 0; j < length; ++j) {
               t = (BaseTreeNode)listNodes.get(j);
               if (id.equals(t.getId())) {
                  treeNodes.add((T) t);
                  nodeIndex[j] = 1;
               }
            }
         }

         for(index = 0; index < treeNodes.size(); ++index) {
            Long tempId = ((BaseTreeNode)treeNodes.get(index)).getId();
            if (ObjectUtil.isNotEmpty(tempId)) {
               for(i = 0; i < length; ++i) {
                  t = (BaseTreeNode)listNodes.get(i);
                  if (tempId.equals(t.getParentId()) && nodeIndex[i] == 0) {
                     nodeIndex[i] = 1;
                     treeNodes.add((T) t);
                  }
               }
            }
         }

         return treeNodes;
      } else {
         return null;
      }
   }

   public static <T extends BaseTreeNode> List<T> assembleTree(List<T> listNodes) {
      List<T> newTreeNodes = (List)listNodes.stream().filter((tx) -> {
         return ObjectUtil.isEmpty(tx.getParentId()) || tx.getParentId() == -1L;
      }).collect(Collectors.toList());
      Iterator var2 = newTreeNodes.iterator();

      while(var2.hasNext()) {
         T t = (T)var2.next();
         assembleTree(t, listNodes);
      }

      return newTreeNodes;
   }

   public static <T extends BaseTreeNode> T assembleTreeById(Long id, List<T> listNodes) {
      if (!ObjectUtil.isEmpty(id) && !CollectionUtil.isEmpty(listNodes)) {
         T node = null;
         Iterator var3 = listNodes.iterator();

         while(var3.hasNext()) {
            T temp = (T)var3.next();
            if (id.equals(temp.getId())) {
               node = temp;
               break;
            }
         }

         assembleTree(node, listNodes);
         return node;
      } else {
         return null;
      }
   }

   static <T extends BaseTreeNode> void assembleTree(T node, List<T> listNodes) {
      if (node != null && !CollectionUtil.isEmpty(listNodes)) {
         Stream<T> var10000 = listNodes.stream().filter((tx) -> {
            return Objects.equals(tx.getParentId(), node.getId());
         });
         Objects.requireNonNull(node);
         var10000.forEachOrdered(node::addChild);
         if (!CollectionUtil.isEmpty(node.getChildren())) {
            Iterator var2 = node.getChildren().iterator();

            while(var2.hasNext()) {
               Object t = var2.next();
               assembleTree((T) t, listNodes);
            }
         }
      }

   }

   public static <T extends BaseTreeNode> String idToString(List<T> treeNodes) {
      return idToString(treeNodes, ",");
   }

   public static <T extends BaseTreeNode> String idToString(List<T> treeNodes, String c) {
      StringBuilder pks = new StringBuilder();
      if (treeNodes != null) {
         Iterator var3 = treeNodes.iterator();

         while(var3.hasNext()) {
            T t = (T) var3.next();
            pks.append(t.getId()).append(c);
         }
      }

      return pks.length() > 0 ? pks.delete(pks.length() - c.length(), pks.length()).toString() : "";
   }
}
