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

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 dev.crucifixbeta.mod.modules.settings.impl.StringSetting;
import java.util.ArrayList;
import java.util.List;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.hit.HitResult;

@Environment(EnvType.CLIENT)
public class AddFriend extends Module {
   public static AddFriend INSTANCE;
   public final BooleanSetting autoNotify = this.add(new BooleanSetting("AutoNotify", false));
   public final StringSetting notifyMessage = this.add(new StringSetting("NotifyMessage", "\u6211\u5df2\u52a0\u4f60\u4e3a\u597d\u53cb\u5728CrucifiX\u5ba2\u6237\u7aef\u4e0a"));
   public final BooleanSetting antiTK = this.add(new BooleanSetting("AntiTK", false).setParent().injectTask(this::syncAntiTKState));
   public final BooleanSetting antiTKNotify = this.add(new BooleanSetting("AntiTKNotify", true, this.antiTK::isOpen));
   public final BooleanSetting autoTK = this.add(new BooleanSetting("AutoTK", false).setParent().injectTask(this::syncAutoTKState));
   public final BooleanSetting autoTKNotify = this.add(new BooleanSetting("AutoTKNotify", true, this.autoTK::isOpen));
   public final BooleanSetting autoTKInstantAdd = this.add(new BooleanSetting("InstantAdd", true, this.autoTK::isOpen));
   private final List<String> autoTKFriendsBefore = new ArrayList<>();
   private boolean antiTKApplied;
   private boolean autoTKActive;

   public AddFriend() {
      super("Friend", Module.Category.Client);
      this.setChinese("\u597d\u53cb");
      this.autoNotify.setChinese("\u81ea\u52a8\u63d0\u9192");
      this.notifyMessage.setChinese("\u63d0\u9192\u5185\u5bb9");
      this.antiTK.setChinese("\u9632\u961f\u53cb\u51fb\u6740");
      this.antiTKNotify.setChinese("\u9632TK\u63d0\u9192");
      this.autoTK.setChinese("\u81ea\u52a8TK");
      this.autoTKNotify.setChinese("\u81ea\u52a8TK\u63d0\u9192");
      this.autoTKInstantAdd.setChinese("\u5173\u95ed\u81ea\u52a8\u6062\u590d");
      INSTANCE = this;
   }

   public static void notifyFriendAdded(String playerName) {
      AddFriend var1 = INSTANCE;
      if (var1 != null && var1.autoNotify.getValue() && !nullCheck()) {
         String var2 = var1.notifyMessage.getValue();
         if (var2 != null && !var2.isBlank()) {
            var2 = var2.replace("{player}", playerName);
            if (mc.getNetworkHandler() != null) {
               mc.getNetworkHandler().sendChatCommand("msg " + playerName + " " + var2);
            }
         }
      }
   }

   @Override
   public void enable() {
      if (this.state) {
         this.addCrosshairFriend();
      } else {
         super.enable();
      }
   }

   @Override
   public void disable() {
      this.state = true;
      this.syncAntiTKState();
      this.syncAutoTKState();
   }

   @Override
   public void toggle() {
      this.state = true;
      this.addCrosshairFriend();
   }

   @Override
   public boolean isOn() {
      return true;
   }

   @Override
   public void onEnable() {
      this.state = true;
      this.syncAntiTKState();
      this.syncAutoTKState();
   }

   @Override
   public String getInfo() {
      if (this.autoTK.getValue()) {
         return "AutoTK";
      } else {
         return this.antiTK.getValue() ? "AntiTK" : null;
      }
   }

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

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

   private void syncAntiTKState() {
      if (CrucifixBeta.FRIEND != null) {
         boolean var1 = this.antiTK.getValue();
         if (CrucifixBeta.FRIEND.isPaused() != var1) {
            CrucifixBeta.FRIEND.setPaused(var1);
         }

         if (this.antiTKApplied != var1) {
            this.antiTKApplied = var1;
            if (this.antiTKNotify.getValue() && !nullCheck()) {
               CommandManager.sendMessage(var1 ? "\u00a7cFriend list temporarily disabled" : "\u00a7aFriend list restored");
            }
         }
      }
   }

   private void syncAutoTKState() {
      if (this.autoTK.getValue()) {
         if (!this.autoTKActive) {
            this.startAutoTK();
         }
      } else if (this.autoTKActive) {
         this.stopAutoTK();
      }
   }

   private void startAutoTK() {
      if (mc.player != null && CrucifixBeta.FRIEND != null && CrucifixBeta.FRIEND.friendList != null) {
         this.autoTKFriendsBefore.clear();
         this.autoTKFriendsBefore.addAll(CrucifixBeta.FRIEND.friendList);
         CrucifixBeta.FRIEND.setSaveOverride(this.autoTKFriendsBefore);
         CrucifixBeta.FRIEND.friendList.clear();
         this.autoTKActive = true;
         if (this.autoTKNotify.getValue()) {
            CommandManager.sendMessage("\u00a7aRemoved all friends for team killing!");
         }
      }
   }

   private void stopAutoTK() {
      if (this.autoTKInstantAdd.getValue()) {
         this.restoreAutoTKFriends();
      } else if (this.autoTKNotify.getValue()) {
         CommandManager.sendMessage("\u00a7eDisable InstantAdd to keep friends removed until manually restored.");
      }

      if (CrucifixBeta.FRIEND != null) {
         CrucifixBeta.FRIEND.clearSaveOverride();
      }

      this.autoTKActive = false;
   }

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

         CrucifixBeta.FRIEND.clearSaveOverride();
         if (this.autoTKNotify.getValue() && !this.autoTKFriendsBefore.isEmpty()) {
            CommandManager.sendMessage("§aRestored " + this.autoTKFriendsBefore.size() + " friends!");
         }
      }
   }

   private void addCrosshairFriend() {
      if (!nullCheck()) {
         HitResult var1 = mc.crosshairTarget;
         EntityHitResult var2;
         Entity var3;
         if (var1 instanceof EntityHitResult && (var3 = (var2 = (EntityHitResult)var1).getEntity()) instanceof PlayerEntity) {
            PlayerEntity var4 = (PlayerEntity)var3;
            CrucifixBeta.FRIEND.friend(var4);
         }
      }
   }
}
