package dev.crucifixbeta.mod.commands.impl;

import dev.crucifixbeta.core.impl.RotationManager;
import dev.crucifixbeta.mod.commands.Command;
import java.text.DecimalFormat;
import java.util.List;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.util.math.Vec3d;

@Environment(EnvType.CLIENT)
public class AimCommand extends Command {

   public AimCommand() {
      super("aim", "[x] [y] [z]");
   }

   @Override
   public void runCommand(String[] parameters) {
      if (parameters.length != 3) {
         this.sendUsage();
      } else {
         double var2;
         if (this.isNumeric(parameters[0])) {
            var2 = Double.parseDouble(parameters[0]);
         } else {
            if (!parameters[0].startsWith("~")) {
               this.sendUsage();
               return;
            }

            if (this.isNumeric(parameters[0].replace("~", ""))) {
               var2 = mc.player.getX() + Double.parseDouble(parameters[0].replace("~", ""));
            } else {
               if (!parameters[0].replace("~", "").isEmpty()) {
                  this.sendUsage();
                  return;
               }

               var2 = mc.player.getX();
            }
         }

         double var4;
         if (this.isNumeric(parameters[1])) {
            var4 = Double.parseDouble(parameters[1]);
         } else {
            if (!parameters[1].startsWith("~")) {
               this.sendUsage();
               return;
            }

            if (this.isNumeric(parameters[1].replace("~", ""))) {
               var4 = mc.player.getY() + Double.parseDouble(parameters[1].replace("~", ""));
            } else {
               if (!parameters[1].replace("~", "").isEmpty()) {
                  this.sendUsage();
                  return;
               }

               var4 = mc.player.getY();
            }
         }

         double var6;
         if (this.isNumeric(parameters[2])) {
            var6 = Double.parseDouble(parameters[2]);
         } else {
            if (!parameters[2].startsWith("~")) {
               this.sendUsage();
               return;
            }

            if (this.isNumeric(parameters[2].replace("~", ""))) {
               var6 = mc.player.getZ() + Double.parseDouble(parameters[2].replace("~", ""));
            } else {
               if (!parameters[2].replace("~", "").isEmpty()) {
                  this.sendUsage();
                  return;
               }

               var6 = mc.player.getZ();
            }
         }

         float[] var8 = RotationManager.getRotation(new Vec3d(var2, var4, var6));
         mc.player.setYaw(var8[0]);
         mc.player.setPitch(var8[1]);
         DecimalFormat var9 = new DecimalFormat("0.0");
         this.sendChatMessage("§fAim to §eX:" + var9.format(var2) + " Y:" + var9.format(var4) + " Z:" + var9.format(var6));
      }
   }

   private boolean isNumeric(String str) {
      return str.matches("-?\\d+(\\.\\d+)?");
   }

   @Override
   public String[] getAutocorrect(int count, List<String> seperated) {
      return new String[]{"~ "};
   }
}
