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

import dev.crucifixbeta.CrucifixBeta;
import dev.crucifixbeta.api.events.eventbus.EventListener;
import dev.crucifixbeta.api.events.impl.TickEvent;
import dev.crucifixbeta.core.impl.CommandManager;
import dev.crucifixbeta.mod.modules.Module;
import dev.crucifixbeta.mod.modules.settings.impl.BooleanSetting;
import java.util.ArrayList;
import java.util.List;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

@Environment(EnvType.CLIENT)
public class AutoTK extends Module {
   private final BooleanSetting notify = this.add(new BooleanSetting("Notify", true));
   private final BooleanSetting instantAdd = this.add(new BooleanSetting("InstantAdd", true));
   private List<String> friendsBefore = new ArrayList<>();
   private boolean wasEnabled = false;

   public AutoTK() {
      super("AutoTK", "Automatically removes friends when enabled and adds them back when disabled", Module.Category.Combat);
      this.setChinese("\u81ea\u52a8\u80cc\u523a");
   }

   @Override
   public void onEnable() {
      if (mc.player != null) {
         this.friendsBefore.clear();
         if (CrucifixBeta.FRIEND != null && CrucifixBeta.FRIEND.friendList != null) {
            this.friendsBefore.addAll(CrucifixBeta.FRIEND.friendList);
         }

         if (CrucifixBeta.FRIEND != null && CrucifixBeta.FRIEND.friendList != null) {
            CrucifixBeta.FRIEND.friendList.clear();
         }

         if (this.notify.getValue()) {
            CommandManager.sendMessage("\u00a7aRemoved all friends for team killing!");
         }

         this.wasEnabled = true;
      }
   }

   @Override
   public void onDisable() {
      if (mc.player != null && this.wasEnabled) {
         if (this.instantAdd.getValue()) {
            this.addFriendsBack();
         } else {
            CommandManager.sendMessage("\u00a7eType '.addfriends' to restore your friends");
         }

         this.wasEnabled = false;
      }
   }

   @EventListener
   public void onTick(TickEvent event) {
      if (this.wasEnabled && mc.player != null) {
         if (CrucifixBeta.FRIEND != null && CrucifixBeta.FRIEND.friendList != null && !CrucifixBeta.FRIEND.friendList.isEmpty()) {
            CrucifixBeta.FRIEND.friendList.clear();
            if (this.notify.getValue()) {
               CommandManager.sendMessage("\u00a7cDetected friend addition, removed again!");
            }
         }
      }
   }

   private void addFriendsBack() {
      if (CrucifixBeta.FRIEND != null) {
         for (String var2 : this.friendsBefore) {
            if (!CrucifixBeta.FRIEND.friendList.contains(var2)) {
               CrucifixBeta.FRIEND.friendList.add(var2);
            }
         }

         if (this.notify.getValue() && !this.friendsBefore.isEmpty()) {
            CommandManager.sendMessage("§aRestored " + this.friendsBefore.size() + " friends!");
         }
      }
   }

   public void restoreFriends() {
      this.addFriendsBack();
   }
}
