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

import dev.crucifixbeta.api.events.eventbus.EventListener;
import dev.crucifixbeta.api.events.impl.DeathEvent;
import dev.crucifixbeta.api.utils.math.MathUtil;
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.entity.EntityType;
import net.minecraft.entity.LightningEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;

@Environment(EnvType.CLIENT)
public class KillEffect extends Module {
   private final BooleanSetting lightning = this.add(new BooleanSetting("Lightning", true));
   private final BooleanSetting levelUp = this.add(new BooleanSetting("LevelUp", true).setParent());
   private final SliderSetting lMaxPitch = this.add(new SliderSetting("LMaxPitch", 1.0, 0.0, 2.0, 0.1, this.levelUp::isOpen));
   private final SliderSetting lMinPitch = this.add(new SliderSetting("LMinPitch", 1.0, 0.0, 2.0, 0.1, this.levelUp::isOpen));
   private final BooleanSetting trident = this.add(new BooleanSetting("Trident", false).setParent());
   private final SliderSetting tMaxPitch = this.add(new SliderSetting("TMaxPitch", 1.0, 0.0, 2.0, 0.1, this.trident::isOpen));
   private final SliderSetting tMinPitch = this.add(new SliderSetting("TMinPitch", 1.0, 0.0, 2.0, 0.1, this.trident::isOpen));
   private final SliderSetting factor = this.add(new SliderSetting("Factor", 1.0, 1.0, 10.0, 1.0));

   public KillEffect() {
      super("KillEffect", Module.Category.Misc);
      this.setChinese("\u51fb\u6740\u6548\u679c");
   }

   @EventListener
   public void onPlayerDeath(DeathEvent event) {
      if (!nullCheck()) {
         PlayerEntity var2 = event.getPlayer();
         if (var2 != null) {
            for (int var3 = 0; (double)var3 < this.factor.getValue(); var3++) {
               this.doEffect(var2);
            }
         }
      }
   }

   private void doEffect(PlayerEntity player) {
      double var2 = player.getX();
      double var4 = player.getY();
      double var6 = player.getZ();
      if (this.lightning.getValue()) {
         LightningEntity var8 = new LightningEntity(EntityType.LIGHTNING_BOLT, mc.world);
         var8.updatePosition(var2, var4, var6);
         var8.refreshPositionAfterTeleport(var2, var4, var6);
         mc.world.addEntity(var8);
      }

      if (this.levelUp.getValue()) {
         mc.world
            .playSound(
               mc.player,
               var2,
               var4,
               var6,
               SoundEvents.ENTITY_PLAYER_LEVELUP,
               SoundCategory.PLAYERS,
               100.0F,
               MathUtil.random(this.lMinPitch.getValueFloat(), this.lMaxPitch.getValueFloat())
            );
      }

      if (this.trident.getValue()) {
         mc.world
            .playSound(
               mc.player,
               var2,
               var4,
               var6,
               SoundEvents.ITEM_TRIDENT_THUNDER,
               SoundCategory.MASTER,
               999.0F,
               MathUtil.random(this.tMinPitch.getValueFloat(), this.tMaxPitch.getValueFloat())
            );
      }
   }
}
