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

import dev.crucifixbeta.CrucifixBeta;
import dev.crucifixbeta.api.events.eventbus.EventListener;
import dev.crucifixbeta.api.events.impl.UpdateEvent;
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.impl.render.PlaceRender;
import dev.crucifixbeta.mod.modules.settings.impl.BooleanSetting;
import dev.crucifixbeta.mod.modules.settings.impl.SliderSetting;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.Blocks;
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket;
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket.Action;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;

@Environment(EnvType.CLIENT)
public class Nuker extends Module {
   private final SliderSetting range = this.add(new SliderSetting("Range", 4.0, 0.0, 8.0, 0.1));
   private final BooleanSetting down = this.add(new BooleanSetting("Down", false));
   private final BooleanSetting sand = this.add(new BooleanSetting("Sand", false));
   private final SliderSetting breaks = this.add(new SliderSetting("Breaks", 10, 0, 20, this.sand::getValue));

   public Nuker() {
      super("Nuker", Module.Category.Misc);
      this.setChinese("\u8303\u56f4\u6316\u6398");
   }

   @EventListener
   public void onUpdate(UpdateEvent event) {
      if (PacketMine.getBreakPos() == null || mc.world.isAir(PacketMine.getBreakPos())) {
         if (this.sand.getValue()) {
            if (!mc.player.isOnGround()) {
               return;
            }

            int var2 = 0;

            for (BlockPos var4 : BlockUtil.getSphere(this.range.getValueFloat(), mc.player.getEyePos())) {
               Direction var5;
               if ((Blocks.SAND == mc.world.getBlockState(var4).getBlock() || Blocks.RED_SAND == mc.world.getBlockState(var4).getBlock())
                  && (var5 = BlockUtil.getClickSideStrict(var4)) != null) {
                  PlaceRender.INSTANCE.create(var4);
                  CrucifixBeta.ROTATION.snapAt(var4.toCenterPos());
                  sendSequencedPacket(id -> new PlayerActionC2SPacket(Action.STOP_DESTROY_BLOCK, var4, var5, id));
                  sendSequencedPacket(id -> new PlayerActionC2SPacket(Action.START_DESTROY_BLOCK, var4, var5, id));
                  sendSequencedPacket(id -> new PlayerActionC2SPacket(Action.STOP_DESTROY_BLOCK, var4, var5, id));
                  CrucifixBeta.ROTATION.snapBack();
                  if ((double)(++var2) >= this.breaks.getValue()) {
                     return;
                  }
               }
            }
         } else {
            BlockPos var6 = this.getBlock();
            if (var6 != null) {
               PacketMine.INSTANCE.mine(var6);
            }
         }
      }
   }

   private BlockPos getBlock() {
      BlockPos var1 = null;

      for (BlockPos var3 : BlockUtil.getSphere(this.range.getValueFloat(), mc.player.getEyePos())) {
         if (!mc.world.isAir(var3) && !PacketMine.unbreakable(var3) && BlockUtil.getClickSideStrict(var3) != null) {
            if (!((double)var3.getY() < mc.player.getY())) {
               return var3;
            }

            if (var1 == null && this.down.getValue()) {
               var1 = var3;
            }
         }
      }

      return var1;
   }
}
