package dev.crucifixbeta.mod.modules.impl.combat;

import dev.crucifixbeta.CrucifixBeta;
import dev.crucifixbeta.api.events.eventbus.EventListener;
import dev.crucifixbeta.api.events.impl.UpdateEvent;
import dev.crucifixbeta.api.utils.world.BlockPosX;
import dev.crucifixbeta.api.utils.world.BlockUtil;
import dev.crucifixbeta.mod.modules.Module;
import dev.crucifixbeta.mod.modules.impl.player.PacketMine;
import dev.crucifixbeta.mod.modules.settings.impl.BooleanSetting;
import dev.crucifixbeta.mod.modules.settings.impl.EnumSetting;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.Blocks;
import net.minecraft.util.math.BlockPos;

@Environment(EnvType.CLIENT)
public class AntiCrawl extends Module {
   public static AntiCrawl INSTANCE;
   final double[] xzOffset = new double[]{0.0, 0.3, -0.3};
   private final EnumSetting<AntiCrawl.While> whileSetting = this.add(new EnumSetting<>("While", AntiCrawl.While.Crawling));
   private final BooleanSetting web = this.add(new BooleanSetting("Web", true));
   public boolean work = false;

   public AntiCrawl() {
      super("AntiCrawl", Module.Category.Combat);
      this.setChinese("\u53cd\u8db4\u4e0b");
      INSTANCE = this;
   }

   @EventListener
   public void onUpdate(UpdateEvent event) {
      this.work = false;
      if (!mc.player.isFallFlying()) {
         if (this.whileSetting.is(AntiCrawl.While.Always) && BlockUtil.getBlock(mc.player.getBlockPos()) != Blocks.BEDROCK
            || mc.player.isCrawling()
            || this.whileSetting.is(AntiCrawl.While.Mining) && CrucifixBeta.BREAK.isMining(mc.player.getBlockPos())) {
            for (double var5 : this.xzOffset) {
               for (double var10 : this.xzOffset) {
                  BlockPosX var12 = new BlockPosX(mc.player.getX() + var5, mc.player.getY() + 1.2, mc.player.getZ() + var10);
                  if (this.canBreak(var12)) {
                     PacketMine.INSTANCE.mine(var12);
                     this.work = true;
                     return;
                  }

                  BlockPosX var13;
                  if (this.web.getValue()
                     && mc.world.getBlockState(var13 = new BlockPosX(mc.player.getX() + var5, mc.player.getY(), mc.player.getZ() + var10)).getBlock()
                        == Blocks.COBWEB
                     && this.canBreak(var13)) {
                     PacketMine.INSTANCE.mine(var13);
                     this.work = true;
                     return;
                  }
               }
            }
         }
      }
   }

   private boolean canBreak(BlockPos pos) {
      return (BlockUtil.getClickSideStrict(pos) != null || pos.equals(PacketMine.getBreakPos())) && !PacketMine.unbreakable(pos) && !mc.world.isAir(pos);
   }
@Environment(EnvType.CLIENT)
   private static enum While {
      Crawling,
      Mining,
      Always;

}
}
