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

import dev.crucifixbeta.api.events.eventbus.EventListener;
import dev.crucifixbeta.api.events.impl.MoveEvent;
import dev.crucifixbeta.api.events.impl.UpdateEvent;
import dev.crucifixbeta.api.utils.path.BaritoneUtil;
import dev.crucifixbeta.api.utils.player.EntityUtil;
import dev.crucifixbeta.api.utils.player.MovementUtil;
import dev.crucifixbeta.mod.modules.Module;
import dev.crucifixbeta.mod.modules.settings.impl.BooleanSetting;
import dev.crucifixbeta.mod.modules.settings.impl.EnumSetting;
import java.util.Objects;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.entity.effect.StatusEffects;

@Environment(EnvType.CLIENT)
public class Strafe extends Module {
   public static Strafe INSTANCE;
   private final BooleanSetting airStop = this.add(new BooleanSetting("AirStop", true));
   private final BooleanSetting slowCheck = this.add(new BooleanSetting("SlowCheck", true));
   private final BooleanSetting autoWalk = this.add(new BooleanSetting("AutoWalk", false).setParent());
   private final EnumSetting<Strafe.AutoWalkMode> autoWalkMode = this.add(
      new EnumSetting<>("AutoWalkMode", Strafe.AutoWalkMode.Forward, this.autoWalk::isOpen)
   );
   private boolean autoWalkStarted;

   public Strafe() {
      super("Strafe", "Modifies sprinting", Module.Category.Movement);
      this.setChinese("\u7075\u6d3b\u79fb\u52a8");
      INSTANCE = this;
   }

   @Override
   public void onEnable() {
      this.autoWalkStarted = false;
   }

   @Override
   public void onDisable() {
      if (this.autoWalkStarted) {
         BaritoneUtil.cancelEverything();
      }

      this.autoWalkStarted = false;
   }

   @Override
   public void onLogout() {
      if (this.autoWalkStarted) {
         BaritoneUtil.cancelEverything();
      }

      this.autoWalkStarted = false;
   }

   @EventListener
   public void onUpdate(UpdateEvent event) {
      if (this.autoWalk.getValue()) {
         if (this.autoWalkMode.is(Strafe.AutoWalkMode.Forward)) {
            mc.options.forwardKey.setPressed(true);
         } else {
            if (!this.autoWalkStarted) {
               BaritoneUtil.forward();
               this.autoWalkStarted = true;
            } else if (!BaritoneUtil.isActive()) {
               this.autoWalkStarted = false;
            }
         }
      }
   }

   @EventListener
   public void onStrafe(MoveEvent event) {
      if (!BaritoneUtil.isActive()) {
         if (!mc.player.isSneaking()
            && !Fly.INSTANCE.isOn()
            && !HoleSnap.INSTANCE.isOn()
            && !Speed.INSTANCE.isOn()
            && !mc.player.isFallFlying()
            && !EntityUtil.isInsideBlock()
            && !mc.player.isInLava()
            && !mc.player.isTouchingWater()
            && !mc.player.getAbilities().flying) {
            if (!MovementUtil.isMoving()) {
               if (this.airStop.getValue()) {
                  MovementUtil.setMotionX(0.0);
                  MovementUtil.setMotionZ(0.0);
               }
            } else {
               double[] var2 = MovementUtil.directionSpeed(this.getBaseMoveSpeed());
               event.setX(var2[0]);
               event.setZ(var2[1]);
            }
         }
      }
   }

   public double getBaseMoveSpeed() {
      double var1 = 0.2873;
      if (mc.player.hasStatusEffect(StatusEffects.SPEED) && (!this.slowCheck.getValue() || !mc.player.hasStatusEffect(StatusEffects.SLOWNESS))) {
         var1 *= 1.0 + 0.2 * (double)(Objects.requireNonNull(mc.player.getStatusEffect(StatusEffects.SPEED)).getAmplifier() + 1);
      }

      return var1;
   }

   public boolean forward() {
      return this.isOn() && this.autoWalk.getValue() && this.autoWalkMode.is(Strafe.AutoWalkMode.Forward);
   }
@Environment(EnvType.CLIENT)
   public static enum AutoWalkMode {
      Forward,
      Path;

}
}
