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

import asm.accessors.IFireworkRocketEntity;
import dev.crucifixbeta.api.events.eventbus.EventListener;
import dev.crucifixbeta.api.events.impl.ClientTickEvent;
import dev.crucifixbeta.api.events.impl.PacketEvent;
import dev.crucifixbeta.api.events.impl.RemoveFireworkEvent;
import dev.crucifixbeta.api.utils.math.Timer;
import dev.crucifixbeta.mod.modules.Module;
import dev.crucifixbeta.mod.modules.settings.impl.SliderSetting;
import it.unimi.dsi.fastutil.ints.IntListIterator;
import java.util.ArrayList;
import java.util.List;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.entity.projectile.FireworkRocketEntity;
import net.minecraft.network.packet.c2s.common.CommonPongC2SPacket;
import net.minecraft.network.packet.s2c.play.EntitiesDestroyS2CPacket;
import net.minecraft.network.packet.s2c.play.PlayerPositionLookS2CPacket;

@Environment(EnvType.CLIENT)
public class RocketExtend extends Module {
   final List<CommonPongC2SPacket> packetList = new ArrayList<>();
   private final Timer extendFireworkTimer = new Timer();
   private final SliderSetting time = this.add(new SliderSetting("Time", 10.0, 0.0, 50.0, 0.1));
   private boolean extendFirework;
   private FireworkRocketEntity firework;

   public RocketExtend() {
      super("RocketExtend", Module.Category.Exploit);
      this.setChinese("\u70df\u82b1\u5ef6\u957f");
   }

   @Override
   public void onDisable() {
      if (this.firework != null) {
         ((IFireworkRocketEntity)this.firework).hookExplodeAndRemove();
      }

      this.firework = null;
      this.extendFirework = false;

      for (CommonPongC2SPacket var2 : this.packetList) {
         mc.getNetworkHandler().sendPacket(var2);
      }

      this.packetList.clear();
   }

   @EventListener
   public void onRemoveFirework(RemoveFireworkEvent event) {
      if (mc.player != null) {
         if (mc.player.isFallFlying()
            && this.firework != event.getRocketEntity()
            && ((IFireworkRocketEntity)event.getRocketEntity()).hookWasShotByEntity()
            && ((IFireworkRocketEntity)event.getRocketEntity()).getShooter() == mc.player) {
            this.extendFirework = true;
            event.cancel();
            this.firework = event.getRocketEntity();
            this.extendFireworkTimer.reset();
         }
      }
   }

   @EventListener
   public void onTick(ClientTickEvent event) {
      if (!nullCheck()) {
         if (this.extendFirework) {
            if (!mc.player.isFallFlying() || mc.player.isOnGround() || this.extendFireworkTimer.passedS(this.time.getValue())) {
               this.extendFirework = false;
               if (this.firework != null) {
                  ((IFireworkRocketEntity)this.firework).hookExplodeAndRemove();
                  this.firework = null;
               }

               for (CommonPongC2SPacket var3 : this.packetList) {
                  mc.getNetworkHandler().sendPacket(var3);
               }

               this.packetList.clear();
            }
         }
      }
   }

   @EventListener
   public void onPacketOutbound(PacketEvent.Send event) {
      if (mc.player != null && mc.world != null) {
         if (event.getPacket() instanceof CommonPongC2SPacket var3 && this.extendFirework && mc.player.isFallFlying()) {
            event.cancel();
            this.packetList.add(var3);
         }
      }
   }

   @EventListener
   public void onPacketInbound(PacketEvent.Receive event) {
      if (!nullCheck() && mc.player.isFallFlying() && this.extendFirework) {
         if (event.getPacket() instanceof EntitiesDestroyS2CPacket var3 && this.firework != null) {
            IntListIterator var4 = var3.getEntityIds().iterator();

            while (var4.hasNext()) {
               int var5 = var4.next();
               if (var5 == this.firework.getId()) {
                  event.cancel();
                  return;
               }
            }
         }

         if (event.getPacket() instanceof PlayerPositionLookS2CPacket) {
            this.extendFirework = false;
            if (this.firework != null) {
               ((IFireworkRocketEntity)this.firework).hookExplodeAndRemove();
               this.firework = null;
            }

            for (CommonPongC2SPacket var7 : this.packetList) {
               mc.getNetworkHandler().sendPacket(var7);
            }

            this.packetList.clear();
         }
      }
   }
}
