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

import dev.crucifixbeta.CrucifixBeta;
import dev.crucifixbeta.api.events.eventbus.EventListener;
import dev.crucifixbeta.api.events.impl.ClientTickEvent;
import dev.crucifixbeta.api.events.impl.KeyboardInputEvent;
import dev.crucifixbeta.api.events.impl.UpdateEvent;
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.SliderSetting;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

@Environment(EnvType.CLIENT)
public class GrimLowHop extends Module {
   public static GrimLowHop INSTANCE;
   private final BooleanSetting logging = this.add(new BooleanSetting("Logging", false));
   private final SliderSetting startTicks = this.add(new SliderSetting("StartTick", 2, 0, 10));
   private final SliderSetting skipTicks = this.add(new SliderSetting("SkipTicks", 2, 1, 10));
   private final SliderSetting ticks = this.add(new SliderSetting("Ticks", 3, 1, 10));
   private int airTicks;
   private boolean canTimer;
   private boolean timer;

   public GrimLowHop() {
      super("GrimLowHop", Module.Category.Movement);
      this.setChinese("\u4e25\u5cfb\u4f4e\u8df3");
      INSTANCE = this;
   }

   @Override
   public String getInfo() {
      return this.skipTicks.getValueInt() + "+" + this.ticks.getValueInt();
   }

   @Override
   public void onEnable() {
      this.airTicks = 0;
      this.canTimer = false;
      this.timer = false;
      CrucifixBeta.TIMER.clearSkipTicks();
   }

   @Override
   public void onDisable() {
      this.airTicks = 0;
      this.canTimer = false;
      this.timer = false;
      CrucifixBeta.TIMER.clearSkipTicks();
   }

   private void log(String message) {
      if (this.logging.getValue()) {
         CommandManager.sendMessage(message);
      }
   }

   @EventListener
   public void onPreTick(ClientTickEvent event) {
      if (!event.isPost() && !nullCheck()) {
         if (this.airTicks >= this.startTicks.getValueInt() && !this.canTimer) {
            CrucifixBeta.TIMER.addSkipTicks(this.skipTicks.getValueInt());
            this.canTimer = true;
            this.timer = false;
         }
      }
   }

   @EventListener
   public void onUpdate(UpdateEvent event) {
      if (!nullCheck()) {
         if (mc.player.isOnGround()) {
            this.airTicks = 0;
            this.canTimer = false;
            this.timer = false;
         } else {
            this.airTicks++;
            if (this.canTimer && !this.timer) {
               this.timer = true;
               this.log("GrimLowHop trigger: airTicks=" + this.airTicks + " skipTicks=" + this.skipTicks.getValueInt() + " ticks=" + this.ticks.getValueInt());
               int var2 = this.ticks.getValueInt();

               for (int var3 = 0; var3 < var2; var3++) {
                  mc.player.tick();
               }

               this.log("GrimLowHop done");
            }
         }
      }
   }

   @EventListener(
      priority = -200
   )
   public void onKeyboardInput(KeyboardInputEvent event) {
      if (!nullCheck()) {
         mc.player.input.jumping = true;
      }
   }
}
