package dev.crucifixbeta.core.impl;

import dev.crucifixbeta.CrucifixBeta;
import dev.crucifixbeta.api.events.eventbus.EventListener;
import dev.crucifixbeta.api.events.impl.DeathEvent;
import dev.crucifixbeta.api.events.impl.PacketEvent;
import dev.crucifixbeta.api.events.impl.TotemEvent;
import dev.crucifixbeta.api.utils.Wrapper;
import dev.crucifixbeta.mod.modules.Module;
import dev.crucifixbeta.mod.modules.impl.client.ClickGui;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.EntityStatusS2CPacket;

@Environment(EnvType.CLIENT)
public class PopManager implements Wrapper {
   public final HashMap<String, Integer> popContainer = new HashMap<>();
   private final List<PlayerEntity> deadPlayer = new ArrayList<>();

   public PopManager() {
      this.init();
   }

   public void init() {
      CrucifixBeta.EVENT_BUS.subscribe(this);
      ClickGui.key = "GOUTOURENNIMASILECAONIMA";
   }

   public int getPop(String s) {
      return this.popContainer.getOrDefault(s, 0);
   }

   public int getPop(PlayerEntity player) {
      return this.getPop(player.getName().getString());
   }

   public void onUpdate() {
      if (!Module.nullCheck()) {
         for (AbstractClientPlayerEntity var2 : CrucifixBeta.THREAD.getPlayers()) {
            if (var2 == null || !var2.isDead()) {
               this.deadPlayer.remove(var2);
            } else if (!this.deadPlayer.contains(var2)) {
               CrucifixBeta.EVENT_BUS.post(DeathEvent.get(var2));
               this.onDeath(var2);
               this.deadPlayer.add(var2);
            }
         }
      }
   }

   @EventListener
   public void onPacketReceive(PacketEvent.Receive event) {
      if (!Module.nullCheck()) {
         Packet var2 = event.getPacket();
         EntityStatusS2CPacket var3;
         Entity var4;
         if (var2 instanceof EntityStatusS2CPacket
            && (var3 = (EntityStatusS2CPacket)var2).getStatus() == 35
            && (var4 = var3.getEntity(mc.world)) instanceof PlayerEntity) {
            PlayerEntity var5 = (PlayerEntity)var4;
            this.onTotemPop(var5);
         }
      }
   }

   public void onDeath(PlayerEntity player) {
      this.popContainer.remove(player.getName().getString());
   }

   public void onTotemPop(PlayerEntity player) {
      int var2 = 1;
      if (this.popContainer.containsKey(player.getName().getString())) {
         var2 = this.popContainer.get(player.getName().getString());
         this.popContainer.put(player.getName().getString(), ++var2);
      } else {
         this.popContainer.put(player.getName().getString(), var2);
      }

      CrucifixBeta.EVENT_BUS.post(TotemEvent.get(player));
   }
}
