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

import dev.crucifixbeta.api.events.eventbus.EventListener;
import dev.crucifixbeta.api.events.impl.PacketEvent;
import dev.crucifixbeta.api.events.impl.SendMovementPacketsEvent;
import dev.crucifixbeta.api.utils.math.MathUtil;
import dev.crucifixbeta.api.utils.math.Timer;
import dev.crucifixbeta.api.utils.player.MovementUtil;
import dev.crucifixbeta.mod.modules.Module;
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.network.packet.c2s.play.PlayerMoveC2SPacket;
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.Full;
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.PositionAndOnGround;
import net.minecraft.util.math.MathHelper;

@Environment(EnvType.CLIENT)
public class PacketControl extends Module {
   public static PacketControl INSTANCE;
   private final BooleanSetting timerBypass = this.add(new BooleanSetting("TimerBypass", true).setParent());
   private final SliderSetting fullPackets = this.add(new SliderSetting("FullPackets", 15, 1, 50, this.timerBypass::isOpen));
   private final SliderSetting positionPackets = this.add(new SliderSetting("PositionPackets", 15, 1, 50, this.timerBypass::isOpen));
   public final BooleanSetting positionSync = this.add(new BooleanSetting("PositionSync", true).setParent());
   public final SliderSetting positionDelay = this.add(
      new SliderSetting("Delay", 20, 0, 40, this.positionSync::isOpen).setSuffix("ticks")
   );
   private final Timer groundT = new Timer();
   private final Timer rotationT = new Timer();
   private final Timer positionT = new Timer();
   public boolean full = true;
   private int fullPacket;
   private int positionPacket;
   private float yaw;
   private float pitch;

   public PacketControl() {
      super("PacketControl", Module.Category.Exploit);
      this.setChinese("\u53d1\u5305\u63a7\u5236");
      INSTANCE = this;
   }

   @EventListener(
      priority = -999
   )
   public void onRotation(SendMovementPacketsEvent event) {
      if (MovementUtil.isMoving() && this.timerBypass.getValue()) {
         if (this.full) {
            this.yaw = MathHelper.wrapDegrees(event.getYaw() + MathUtil.random(-2.0F, 2.0F));
            this.pitch = MathUtil.clamp(event.getPitch() + MathUtil.random(-2.0F, 2.0F), -90.0F, 90.0F);
         }

         event.setYaw(this.yaw);
         event.setPitch(this.pitch);
      }
   }

   @EventListener
   public void onPacketSend(PacketEvent.Send event) {
      if (event.getPacket() instanceof PlayerMoveC2SPacket var3) {
         if (var3.changesPosition()) {
            this.positionT.reset();
         }

         if (var3.changesLook()) {
            this.rotationT.reset();
         }

         this.groundT.reset();
         if (var3 instanceof PositionAndOnGround && !this.full) {
            this.positionPacket++;
            if ((double)this.positionPacket >= this.positionPackets.getValue()) {
               this.positionPacket = 0;
               this.full = true;
            }
         } else if (var3 instanceof Full && this.full) {
            this.fullPacket++;
            if ((double)this.fullPacket >= this.fullPackets.getValue()) {
               this.fullPacket = 0;
               this.full = false;
            }
         }
      }
   }
}
