package dev.crucifixbeta.api.utils.path;

import dev.crucifixbeta.api.utils.Wrapper;
import dev.crucifixbeta.api.utils.world.BlockPosX;
import java.util.ArrayList;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.block.PlantBlock;
import net.minecraft.block.WallSignBlock;
import net.minecraft.entity.LivingEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;

@Environment(EnvType.CLIENT)
public class PathUtils implements Wrapper {
   public static final double range = 9.0;

   private static boolean canPassThrough(BlockPos pos) {
      Block var1 = mc.world.getBlockState(new BlockPos(pos.getX(), pos.getY(), pos.getZ())).getBlock();
      return var1 == Blocks.AIR
         || var1 instanceof PlantBlock
         || var1 == Blocks.VINE
         || var1 == Blocks.LADDER
         || var1 == Blocks.WATER
         || var1 == Blocks.WATER_CAULDRON
         || var1 instanceof WallSignBlock;
   }

   public static ArrayList<Vec3> computePath(LivingEntity fromEntity, LivingEntity toEntity) {
      return computePath(new Vec3(fromEntity.getX(), fromEntity.getY(), fromEntity.getZ()), new Vec3(toEntity.getX(), toEntity.getY(), toEntity.getZ()));
   }

   public static ArrayList<Vec3> computePath(Vec3d vec3d) {
      return computePath(new Vec3(mc.player.getX(), mc.player.getY(), mc.player.getZ()), new Vec3(vec3d.x, vec3d.y, vec3d.z));
   }

   public static ArrayList<Vec3> computePath(Vec3 topFrom, Vec3 to) {
      if (!canPassThrough(new BlockPosX(topFrom.mc()))) {
         topFrom = topFrom.addVector(0.0, 1.0, 0.0);
      }

      AStarCustomPathFinder var2 = new AStarCustomPathFinder(topFrom, to);
      var2.compute();
      int var3 = 0;
      Vec3 var4 = null;
      Vec3 var5 = null;
      ArrayList<Vec3> var6 = new ArrayList<>();
      ArrayList<Vec3> var7 = var2.getPath();

      for (Vec3 var9 : var7) {
         if (var3 != 0 && var3 != var7.size() - 1) {
            boolean var10 = true;
            if (var9.squareDistanceTo(var5) > 9.0) {
               var10 = false;
            } else {
               double var11 = Math.min(var5.x(), var9.x());
               double var13 = Math.min(var5.y(), var9.y());
               double var15 = Math.min(var5.z(), var9.z());
               double var17 = Math.max(var5.x(), var9.x());
               double var19 = Math.max(var5.y(), var9.y());
               double var21 = Math.max(var5.z(), var9.z());

               label65:
               for (int var23 = (int)var11; (double)var23 <= var17; var23++) {
                  for (int var24 = (int)var13; (double)var24 <= var19; var24++) {
                     for (int var25 = (int)var15; (double)var25 <= var21; var25++) {
                        if (!AStarCustomPathFinder.checkPositionValidity(var23, var24, var25, false)) {
                           var10 = false;
                           break label65;
                        }
                     }
                  }
               }
            }

            if (!var10) {
               if (!var6.contains(var4.addVector(0.5, 0.0, 0.5))) {
                  var6.add(var4.addVector(0.5, 0.0, 0.5));
               }

               var5 = var4;
            }
         } else {
            if (var4 != null && !var6.contains(var4.addVector(0.5, 0.0, 0.5))) {
               var6.add(var4.addVector(0.5, 0.0, 0.5));
            }

            if (!var6.contains(var9.addVector(0.5, 0.0, 0.5))) {
               var6.add(var9.addVector(0.5, 0.0, 0.5));
            }

            var5 = var9;
         }

         var4 = var9;
         var3++;
      }

      return var6;
   }
}
