package dev.crucifixbeta.mod.commands.impl;

import dev.crucifixbeta.CrucifixBeta;
import dev.crucifixbeta.mod.commands.Command;
import dev.crucifixbeta.mod.modules.Module;
import java.util.ArrayList;
import java.util.List;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

@Environment(EnvType.CLIENT)
public class BindsCommand extends Command {

   public BindsCommand() {
      super("binds", "[clear all]");
   }

   @Override
   public void runCommand(String[] parameters) {
      if (parameters.length == 2 && parameters[0].equalsIgnoreCase("clear") && parameters[1].equalsIgnoreCase("all")) {
         this.clearAllBinds();
      } else if (parameters.length != 0) {
         this.sendUsage();
      } else {
         ArrayList var2 = this.getBoundModules();
         if (var2.isEmpty()) {
            this.sendChatMessage("\u00a7fNo module binds found.");
         } else {
            this.sendChunked(var2, "");
         }
      }
   }

   private void clearAllBinds() {
      ArrayList var1 = this.getBoundModules();
      if (var1.isEmpty()) {
         this.sendChatMessage("\u00a7fNo module binds to clear.");
      } else {
         for (Module var3 : CrucifixBeta.MODULE.getModules()) {
            if (var3.getBindSetting().getValue() != -1) {
               var3.getBindSetting().setValue(-1);
               var3.getBindSetting().setPressed(false);
               var3.getBindSetting().holding = false;
            }
         }

         CrucifixBeta.save();
         this.sendChatMessage(
            "§aCleared §f" + var1.size() + "§a module bind" + (var1.size() == 1 ? "" : "s") + "§a:"
         );
         this.sendChunked(var1, "\u00a7f");
      }
   }

   private ArrayList<String> getBoundModules() {
      ArrayList var1 = new ArrayList();

      for (Module var3 : CrucifixBeta.MODULE.getModules()) {
         if (var3.getBindSetting().getValue() != -1) {
            var1.add("§f" + var3.getDisplayName() + " §7- §r" + var3.getBindSetting().getKeyString());
         }
      }

      return var1;
   }

   private void sendChunked(List<String> list, String prefix) {
      int var3 = 0;
      StringBuilder var4 = new StringBuilder();

      for (String var6 : list) {
         if (var3 == 0) {
            var4 = new StringBuilder(prefix).append(var6);
         } else {
            var4.append("\u00a77, ").append(var6);
         }

         if (++var3 >= 3) {
            this.sendChatMessage(var4.toString());
            var3 = 0;
         }
      }

      if (var3 > 0) {
         this.sendChatMessage(var4.toString());
      }
   }

   @Override
   public String[] getAutocorrect(int count, List<String> seperated) {
      if (count == 1) {
         String var3 = ((String)seperated.getLast()).toLowerCase();
         if (("clear").startsWith(var3)) {
            return new String[]{"clear"};
         }
      } else if (count == 2 && seperated.size() > 1 && ((String)seperated.get(1)).equalsIgnoreCase("clear")) {
         String var4 = ((String)seperated.getLast()).toLowerCase();
         if (("all").startsWith(var4)) {
            return new String[]{"all"};
         }
      }

      return null;
   }
}
