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

import dev.crucifixbeta.api.events.eventbus.EventListener;
import dev.crucifixbeta.api.events.impl.LookDirectionEvent;
import dev.crucifixbeta.api.utils.math.MathUtil;
import dev.crucifixbeta.mod.modules.Module;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.math.MathHelper;

@Environment(EnvType.CLIENT)
public class FreeLook extends Module {
   public static FreeLook INSTANCE;
   private float fakeYaw;
   private float fakePitch;
   private float prevFakeYaw;
   private float prevFakePitch;

   public FreeLook() {
      super("FreeLook", Module.Category.Player);
      this.setChinese("\u81ea\u7531\u89c6\u89d2");
      INSTANCE = this;
   }

   @Override
   public void onEnable() {
      if (nullCheck()) {
         this.disable();
      } else {
         this.fakePitch = mc.player.getPitch();
         this.fakeYaw = mc.player.getYaw();
         this.prevFakePitch = this.fakePitch;
         this.prevFakeYaw = this.fakeYaw;
      }
   }

   @EventListener
   public void onLookDirection(LookDirectionEvent event) {
      this.applyMouseDelta(event.getCursorDeltaX(), event.getCursorDeltaY());
      event.cancel();
   }

   public void applyMouseDelta(double cursorDeltaX, double cursorDeltaY) {
      this.prevFakeYaw = this.fakeYaw;
      this.prevFakePitch = this.fakePitch;
      this.fakeYaw += (float)cursorDeltaX * 0.15F;
      this.fakePitch = MathHelper.clamp(this.fakePitch + (float)cursorDeltaY * 0.15F, -90.0F, 90.0F);
   }

   @Override
   public void onRender3D(MatrixStack matrixStack) {
      this.prevFakeYaw = this.fakeYaw;
      this.prevFakePitch = this.fakePitch;
   }

   public float getFakeYaw() {
      return MathUtil.interpolate(this.prevFakeYaw, this.fakeYaw, mc.getRenderTickCounter().getTickDelta(true));
   }

   public float getFakePitch() {
      return MathUtil.interpolate(this.prevFakePitch, this.fakePitch, mc.getRenderTickCounter().getTickDelta(true));
   }
}
