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

import dev.crucifixbeta.api.events.eventbus.EventListener;
import dev.crucifixbeta.api.events.impl.ClientTickEvent;
import dev.crucifixbeta.api.utils.math.AnimateUtil;
import dev.crucifixbeta.api.utils.math.MathUtil;
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 MotionCamera extends Module {
   public static MotionCamera INSTANCE;
   public final BooleanSetting noFirstPerson = this.add(new BooleanSetting("NoFirstPerson", true));
   public final SliderSetting firstPersonSpeed = this.add(new SliderSetting("FirstPersonSpeed", 0.6, 0.0, 1.0, 0.01));
   public final SliderSetting speed = this.add(new SliderSetting("Speed", 0.3, 0.0, 1.0, 0.01));
   private double fakeX;
   private double fakeY;
   private double fakeZ;
   private double prevFakeX;
   private double prevFakeY;
   private double prevFakeZ;

   public MotionCamera() {
      super("MotionCamera", Module.Category.Render);
      INSTANCE = this;
      this.setChinese("\u8fd0\u52a8\u76f8\u673a");
   }

   public boolean on() {
      return this.isOn() && (!this.noFirstPerson.getValue() || !mc.options.getPerspective().isFirstPerson());
   }

   @Override
   public void onEnable() {
      if (!nullCheck()) {
         this.fakeX = mc.player.getX();
         this.fakeY = mc.player.getY() + (double)mc.player.getEyeHeight(mc.player.getPose());
         this.fakeZ = mc.player.getZ();
         this.prevFakeX = this.fakeX;
         this.prevFakeY = this.fakeY;
         this.prevFakeZ = this.fakeZ;
      }
   }

   @EventListener
   public void onUpdate(ClientTickEvent event) {
      if (!event.isPre() && !nullCheck()) {
         this.prevFakeX = this.fakeX;
         this.prevFakeY = this.fakeY;
         this.prevFakeZ = this.fakeZ;
         double var2 = mc.options.getPerspective().isFirstPerson() ? this.firstPersonSpeed.getValue() : this.speed.getValue();
         this.fakeX = AnimateUtil.animate(this.fakeX, mc.player.getX(), var2);
         this.fakeY = AnimateUtil.animate(this.fakeY, mc.player.getY() + (double)mc.player.getEyeHeight(mc.player.getPose()), var2);
         this.fakeZ = AnimateUtil.animate(this.fakeZ, mc.player.getZ(), var2);
      }
   }

   public double getFakeX() {
      return MathUtil.interpolate(this.prevFakeX, this.fakeX, (double)mc.getRenderTickCounter().getTickDelta(true));
   }

   public double getFakeY() {
      return MathUtil.interpolate(this.prevFakeY, this.fakeY, (double)mc.getRenderTickCounter().getTickDelta(true));
   }

   public double getFakeZ() {
      return MathUtil.interpolate(this.prevFakeZ, this.fakeZ, (double)mc.getRenderTickCounter().getTickDelta(true));
   }
}
