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

import dev.crucifixbeta.CrucifixBeta;
import dev.crucifixbeta.api.events.eventbus.EventListener;
import dev.crucifixbeta.api.events.impl.DeathEvent;
import dev.crucifixbeta.api.events.impl.EntitySpawnEvent;
import dev.crucifixbeta.api.events.impl.PacketEvent;
import dev.crucifixbeta.api.events.impl.RemoveEntityEvent;
import dev.crucifixbeta.api.events.impl.TotemEvent;
import dev.crucifixbeta.api.events.impl.UpdateEvent;
import dev.crucifixbeta.api.utils.math.Timer;
import dev.crucifixbeta.api.utils.player.InventoryUtil;
import dev.crucifixbeta.core.impl.CommandManager;
import dev.crucifixbeta.mod.modules.Module;
import dev.crucifixbeta.mod.modules.impl.client.ClientSetting;
import dev.crucifixbeta.mod.modules.settings.impl.BooleanSetting;
import dev.crucifixbeta.mod.modules.settings.impl.SliderSetting;
import java.awt.Color;
import java.text.DecimalFormat;
import java.util.Objects;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.packet.s2c.play.PlayerPositionLookS2CPacket;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Formatting;

@Environment(EnvType.CLIENT)
public class Tips extends Module {
   public static Tips INSTANCE;
   public final BooleanSetting visualRange = this.add(new BooleanSetting("VisualRange", false).setParent());
   public final BooleanSetting friends = this.add(new BooleanSetting("Friends", false, this.visualRange::isOpen));
   public final BooleanSetting visualRangeMessage = this.add(new BooleanSetting("Message", true, this.visualRange::isOpen));
   public final BooleanSetting visualRangeSound = this.add(new BooleanSetting("Sound", true, this.visualRange::isOpen));
   public final BooleanSetting popCounter = this.add(new BooleanSetting("PopCounter", true));
   public final BooleanSetting deathCoords = this.add(new BooleanSetting("DeathCoords", true));
   public final BooleanSetting serverLag = this.add(new BooleanSetting("ServerLag", true));
   public final BooleanSetting lagBack = this.add(new BooleanSetting("LagBack", true));
   public final BooleanSetting potion = this.add(new BooleanSetting("Potion", true).setParent());
   public final BooleanSetting resistanceLevelCheck = this.add(new BooleanSetting("ResistanceLevelCheck", true, this.potion::isOpen));
   private final SliderSetting yOffset = this.add(new SliderSetting("YOffset", 0, -200, 200, this.potion::isOpen));
   final DecimalFormat df = new DecimalFormat("0.0");
   final int color = new Color(190, 0, 0).getRGB();
   private final Timer lagTimer = new Timer();
   private final Timer lagBackTimer = new Timer();
   int turtles = 0;

   public Tips() {
      super("Tips", Module.Category.Misc);
      this.setChinese("\u63d0\u793a");
      INSTANCE = this;
   }

   @EventListener
   public void onAddEntity(EntitySpawnEvent event) {
      if (this.visualRange.getValue() && event.getEntity() instanceof PlayerEntity && event.getEntity().getDisplayName() != null) {
         String var2 = event.getEntity().getDisplayName().getString();
         boolean var3 = CrucifixBeta.FRIEND.isFriend(var2);
         if ((!var3 || this.friends.getValue()) && event.getEntity() != mc.player) {
            if (this.visualRangeMessage.getValue()) {
               CommandManager.sendMessageId(
                  (var3 ? Formatting.AQUA + var2 : Formatting.WHITE + var2) + "§f entered your visual range.", event.getEntity().getId() + 777
               );
            }

            if (this.visualRangeSound.getValue()) {
               mc.world.playSound(mc.player, mc.player.getBlockPos(), SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.PLAYERS, 100.0F, 1.9F);
            }
         }
      }
   }

   @EventListener
   public void onRemoveEntity(RemoveEntityEvent event) {
      if (this.visualRange.getValue() && event.getEntity() instanceof PlayerEntity && event.getEntity().getDisplayName() != null) {
         String var2 = event.getEntity().getDisplayName().getString();
         boolean var3 = CrucifixBeta.FRIEND.isFriend(var2);
         if ((!var3 || this.friends.getValue()) && event.getEntity() != mc.player) {
            if (this.visualRangeMessage.getValue()) {
               CommandManager.sendMessageId(
                  (var3 ? Formatting.AQUA + var2 : Formatting.WHITE + var2) + "§f left your visual range.", event.getEntity().getId() + 777
               );
            }

            if (this.visualRangeSound.getValue()) {
               mc.world.playSound(mc.player, mc.player.getBlockPos(), SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.PLAYERS, 100.0F, 1.9F);
            }
         }
      }
   }

   @EventListener
   public void onUpdate(UpdateEvent event) {
      if (this.potion.getValue()) {
         this.turtles = InventoryUtil.getPotionCount(StatusEffects.RESISTANCE.value());
      }
   }

   @EventListener
   public void onPacketEvent(PacketEvent.Receive event) {
      this.lagTimer.reset();
      if (event.getPacket() instanceof PlayerPositionLookS2CPacket) {
         this.lagBackTimer.reset();
      }
   }

   @Override
   public void onRender2D(DrawContext drawContext, float tickDelta) {
      if (this.serverLag.getValue() && this.lagTimer.passedS(1.4)) {
         String var3 = "Server not responding (" + this.df.format((double)this.lagTimer.getMs() / 1000.0) + "s)";
         TextRenderer var4 = mc.textRenderer;
         int var5 = mc.getWindow().getScaledWidth() / 2 - mc.textRenderer.getWidth(var3) / 2;
         Objects.requireNonNull(mc.textRenderer);
         drawContext.drawText(var4, var3, var5, 19, this.color, false);
      }

      if (this.lagBack.getValue() && !this.lagBackTimer.passedS(1.5)) {
         String var9 = "Lagback (" + this.df.format((double)(1500L - this.lagBackTimer.getMs()) / 1000.0) + "s)";
         TextRenderer var10 = mc.textRenderer;
         int var12 = mc.getWindow().getScaledWidth() / 2 - mc.textRenderer.getWidth(var9) / 2;
         Objects.requireNonNull(mc.textRenderer);
         drawContext.drawText(var10, var9, var12, 28, this.color, false);
      }

      if (this.potion.getValue()) {
         StringBuilder var11 = new StringBuilder();
         if (this.turtles > 0) {
            var11.append("\u00a7e").append(this.turtles);
         }

         if (mc.player.hasStatusEffect(StatusEffects.RESISTANCE)
            && (!this.resistanceLevelCheck.getValue() || mc.player.getStatusEffect(StatusEffects.RESISTANCE).getAmplifier() > 0)) {
            if (!var11.isEmpty()) {
               var11.append(" ");
            }

            var11.append("\u00a79").append(mc.player.getStatusEffect(StatusEffects.RESISTANCE).getDuration() / 20 + 1);
         }

         if (mc.player.hasStatusEffect(StatusEffects.STRENGTH)) {
            if (!var11.isEmpty()) {
               var11.append(" ");
            }

            var11.append("\u00a74").append(mc.player.getStatusEffect(StatusEffects.STRENGTH).getDuration() / 20 + 1);
         }

         if (mc.player.hasStatusEffect(StatusEffects.SPEED)) {
            if (!var11.isEmpty()) {
               var11.append(" ");
            }

            var11.append("\u00a7b").append(mc.player.getStatusEffect(StatusEffects.SPEED).getDuration() / 20 + 1);
         }

         TextRenderer var13 = mc.textRenderer;
         String var6 = var11.toString();
         int var7 = mc.getWindow().getScaledWidth() / 2 - mc.textRenderer.getWidth(var11.toString()) / 2;
         int var8 = mc.getWindow().getScaledHeight() / 2;
         Objects.requireNonNull(mc.textRenderer);
         drawContext.drawText(var13, var6, var7, var8 + 9 - this.yOffset.getValueInt(), -1, false);
      }
   }

   @EventListener
   public void onPlayerDeath(DeathEvent event) {
      PlayerEntity var2 = event.getPlayer();
      if (this.popCounter.getValue()) {
         if (CrucifixBeta.POP.popContainer.containsKey(var2.getName().getString())) {
            int var3 = CrucifixBeta.POP.popContainer.get(var2.getName().getString());
            if (var3 == 1) {
               if (var2.equals(mc.player)) {
                  this.sendMessage("§fYou§r died after popping §f" + var3 + "§r totem.", var2.getId());
               } else {
                  this.sendMessage("§f" + var2.getName().getString() + "§r died after popping §f" + var3 + "§r totem.", var2.getId());
               }
            } else if (var2.equals(mc.player)) {
               this.sendMessage("§fYou§r died after popping §f" + var3 + "§r totems.", var2.getId());
            } else {
               this.sendMessage("§f" + var2.getName().getString() + "§r died after popping §f" + var3 + "§r totems.", var2.getId());
            }
         } else if (var2.equals(mc.player)) {
            this.sendMessage("\u00a7fYou\u00a7r died.", var2.getId());
         } else {
            this.sendMessage("§f" + var2.getName().getString() + "§r died.", var2.getId());
         }
      }

      if (this.deathCoords.getValue() && var2 == mc.player) {
         this.sendMessage("§4You died at " + var2.getBlockX() + ", " + var2.getBlockY() + ", " + var2.getBlockZ());
      }
   }

   @EventListener
   public void onTotem(TotemEvent event) {
      if (this.popCounter.getValue()) {
         PlayerEntity var2 = event.getPlayer();
         int var3 = 1;
         if (CrucifixBeta.POP.popContainer.containsKey(var2.getName().getString())) {
            var3 = CrucifixBeta.POP.popContainer.get(var2.getName().getString());
         }

         if (var3 == 1) {
            if (var2.equals(mc.player)) {
               this.sendMessage("§fYou§r popped §f" + var3 + "§r totem.", var2.getId());
            } else {
               this.sendMessage("§f" + var2.getName().getString() + " §rpopped §f" + var3 + "§r totems.", var2.getId());
            }
         } else if (var2.equals(mc.player)) {
            this.sendMessage("§fYou§r popped §f" + var3 + "§r totem.", var2.getId());
         } else {
            this.sendMessage("§f" + var2.getName().getString() + " §rhas popped §f" + var3 + "§r totems.", var2.getId());
         }
      }
   }

   public void sendMessage(String message, int id) {
      if (!nullCheck()) {
         if (ClientSetting.INSTANCE.messageStyle.getValue() == ClientSetting.Style.Moon) {
            CommandManager.sendMessageId("§f[§3" + this.getName() + "§f] " + message, id);
            return;
         }

         CommandManager.sendMessageId(message, id);
      }
   }
}
