package dev.crucifixbeta.mod.commands.impl;

import dev.crucifixbeta.CrucifixBeta;
import dev.crucifixbeta.core.Manager;
import dev.crucifixbeta.mod.commands.Command;
import dev.crucifixbeta.mod.modules.impl.combat.AutoRegear;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.PotionContentsComponent;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;

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

   public KitCommand() {
      super("kit", "[list] | [save/create/load/delete] [name]");
   }

   @Override
   public void runCommand(String[] parameters) {
      if (parameters.length == 0) {
         this.sendUsage();
      } else {
         String var2 = parameters[0].toLowerCase();
         switch (var2) {
            case "list":
               if (parameters.length != 1) {
                  this.sendUsage();
                  return;
               }

               this.listKits();
               break;
            case "save":
            case "create":
               if (parameters.length != 2) {
                  this.sendUsage();
                  return;
               }

               this.saveKit(parameters[1]);
               break;
            case "load":
               if (parameters.length != 2) {
                  this.sendUsage();
                  return;
               }

               if (AutoRegear.INSTANCE == null || !AutoRegear.INSTANCE.loadKit(parameters[1])) {
                  this.sendChatMessage("§cKit [" + parameters[1] + "] load failed");
               }
               break;
            case "delete":
               if (parameters.length != 2) {
                  this.sendUsage();
                  return;
               }

               this.deleteKit(parameters[1]);
               break;
            default:
               this.sendUsage();
         }
      }
   }

   private void listKits() {
      try {
         File[] var1 = Manager.getFolder().listFiles();
         if (var1 == null) {
            return;
         }

         for (File var5 : var1) {
            if (var5.getName().endsWith(".kit")) {
               String var6 = var5.getName();
               this.sendChatMessage("Kit: [" + var6.substring(0, var6.length() - 4) + "]");
            }
         }
      } catch (Exception var7) {
         var7.printStackTrace();
      }
   }

   private void saveKit(String name) {
      if (mc.player != null) {
         try {
            File var2 = Manager.getFile(name + ".kit");

            try (PrintWriter var3 = new PrintWriter(new OutputStreamWriter(new FileOutputStream(var2), StandardCharsets.UTF_8))) {
               for (int var4 = 0; var4 < 36; var4++) {
                  var3.println(var4 + ":" + this.getKitKey(mc.player.getInventory().getStack(var4)));
               }
            }

            this.sendChatMessage("§fKit [" + name + "] saved");
         } catch (Exception var8) {
            this.sendChatMessage("§fKit [" + name + "] save failed");
            var8.printStackTrace();
         }
      }
   }

   private void deleteKit(String name) {
      try {
         File var2 = Manager.getFile(name + ".kit");
         if (var2.exists() && !var2.delete()) {
            this.sendChatMessage("§cKit [" + name + "] delete failed");
            return;
         }

         this.sendChatMessage("§fKit [" + name + "] removed");
      } catch (Exception var3) {
         var3.printStackTrace();
      }
   }

   private String getKitKey(ItemStack stack) {
      String var2 = stack.getItem().getTranslationKey();
      if (stack.getItem() != Items.SPLASH_POTION) {
         return var2;
      } else {
         PotionContentsComponent var3 = stack.getOrDefault(DataComponentTypes.POTION_CONTENTS, PotionContentsComponent.DEFAULT);

         for (StatusEffectInstance var5 : var3.getEffects()) {
            if (var5.getEffectType().value() == StatusEffects.RESISTANCE.value()) {
               return var2 + ":turtle";
            }

            if (var5.getEffectType().value() == StatusEffects.STRENGTH.value()) {
               return var2 + ":strength";
            }
         }

         return var2;
      }
   }

   @Override
   public String[] getAutocorrect(int count, List<String> separated) {
      if (count != 1) {
         return null;
      } else {
         String var3 = ((String)separated.getLast()).toLowerCase();
         ArrayList<String> var4 = new ArrayList<>();

         for (String var6 : List.of(
            "list", "save", "create", "load", "delete"
         )) {
            if (var3.equalsIgnoreCase(CrucifixBeta.getPrefix() + "kit") || var6.toLowerCase().startsWith(var3)) {
               var4.add(var6);
            }
         }

         return var4.toArray(new String[0]);
      }
   }
}
