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

import dev.crucifixbeta.api.events.eventbus.EventListener;
import dev.crucifixbeta.api.events.impl.UpdateEvent;
import dev.crucifixbeta.api.utils.math.Timer;
import dev.crucifixbeta.mod.modules.Module;
import dev.crucifixbeta.mod.modules.settings.impl.BooleanSetting;
import dev.crucifixbeta.mod.modules.settings.impl.SliderSetting;
import dev.crucifixbeta.mod.modules.settings.impl.StringSetting;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;
import java.util.Random;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.network.PlayerListEntry;

@Environment(EnvType.CLIENT)
public class Spammer extends Module {
   private static final String CHARACTERS;
   public final BooleanSetting checkSelf = this.add(new BooleanSetting("CheckSelf", false));
   final StringSetting message = this.add(new StringSetting("Message", "I am useing CrucifiX Client"));
   private final Random random = new Random();
   private final SliderSetting randoms = this.add(new SliderSetting("Random", 3.0, 0.0, 20.0, 1.0));
   private final SliderSetting delay = this.add(new SliderSetting("Delay", 5.0, 0.0, 60.0, 0.1).setSuffix("s"));
   private final BooleanSetting tellMode = this.add(new BooleanSetting("RandomWhisper", false));
   private final BooleanSetting autoDisable = this.add(new BooleanSetting("AutoDisable", true));
   private final Timer timer = new Timer();

   public Spammer() {
      super("Spammer", Module.Category.Misc);
      this.setChinese("\u81ea\u52a8\u5237\u5c4f");
   }

   @Override
   public void onLogout() {
      if (this.autoDisable.getValue()) {
         this.disable();
      }
   }

   @EventListener
   public void onUpdate(UpdateEvent event) {
      if (this.timer.passedS(this.delay.getValue())) {
         this.timer.reset();
         String var2 = this.generateRandomString(this.randoms.getValueInt());
         if (!var2.isEmpty()) {
            var2 = " " + var2;
         }

         if (this.tellMode.getValue()) {
            Collection var3 = mc.getNetworkHandler().getPlayerList();
            ArrayList var4 = new ArrayList(var3);
            int var5 = var4.size();
            if (var5 == 0) {
               return;
            }

            PlayerListEntry var6 = (PlayerListEntry)var4.get(this.random.nextInt(var5));

            for (int var7 = 0;
               this.checkSelf.getValue() && Objects.equals(var6.getProfile().getName(), mc.player.getGameProfile().getName());
               var6 = (PlayerListEntry)var4.get(this.random.nextInt(var5))
            ) {
               if (var7 > 50) {
                  return;
               }

               var7++;
            }

            mc.getNetworkHandler().sendChatCommand("tell " + var6.getProfile().getName() + " " + this.message.getValue() + var2);
         } else if (this.message.getValue().startsWith("/")) {
            mc.getNetworkHandler().sendCommand(this.message.getValue().replaceFirst("/", "") + var2);
         } else {
            mc.getNetworkHandler().sendChatMessage(this.message.getValue() + var2);
         }
      }
   }

   private String generateRandomString(int length) {
      StringBuilder var2 = new StringBuilder(length);

      for (int var3 = 0; var3 < length; var3++) {
         int var4 = this.random.nextInt(("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").length());
         var2.append(("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").charAt(var4));
      }

      return var2.toString();
   }

   static {

      CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
   }
}
