package com.matt.forgehax.mods;

import com.matt.forgehax.Helper;
import com.matt.forgehax.events.Render2DEvent;
import com.matt.forgehax.util.Utils;
import com.matt.forgehax.util.command.Setting;
import com.matt.forgehax.util.draw.SurfaceHelper;
import com.matt.forgehax.util.mod.Category;
import com.matt.forgehax.util.mod.ToggleMod;
import com.matt.forgehax.util.mod.loader.RegisterMod;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import org.apache.commons.lang3.ArrayUtils;

/** Created by Babbaj on 10/28/2017. */
@RegisterMod
public class CompassMod extends ToggleMod {

  public final Setting<Double> scale =
      getCommandStub()
          .builders()
          .<Double>newSettingBuilder()
          .name("scale")
          .description("size of the compass")
          .defaultTo(3.D)
          .build();

  private final double HALF_PI = Math.PI / 2;

  private final String[] DIRECTIONS = {"N", "W", "S", "E"};

  public CompassMod() {
    super(Category.RENDER, "Compass", false, "cool compass overlay");
  }

  @SubscribeEvent
  public void onRender(Render2DEvent event) {
    final double centerX = event.getScreenWidth() / 2;
    final double centerY = event.getScreenHeight() * 0.8;

    for (String dir : DIRECTIONS) {
      double rad = getPosOnCompass(dir);
      SurfaceHelper.drawTextShadowCentered(
          dir,
          (float) (centerX + getX(rad)),
          (float) (centerY + getY(rad)),
          dir.equals("N") ? Utils.Colors.RED : Utils.Colors.WHITE);
    }
  }

  private double getX(double rad) {
    return Math.sin(rad) * (scale.getAsDouble() * 10);
  }

  private double getY(double rad) {
    double pitch = Math.toRadians(Helper.getLocalPlayer().rotationPitch); // player pitch
    return Math.cos(rad) * Math.sin(pitch) * (scale.getAsDouble() * 10);
  }

  // return the position on the circle in radians
  private double getPosOnCompass(String s) {
    double yaw =
        Math.toRadians(
            MathHelper.wrapDegrees(Helper.getLocalPlayer().rotationYaw - 90)); // player yaw
    int index = ArrayUtils.indexOf(DIRECTIONS, s) + 1;
    return yaw + (index * HALF_PI);
  }
}

@@ -882,24 +882,31 @@
 %0A%0A  private 
+static 
 final double
@@ -944,57 +944,55 @@
 ate 
-final String%5B%5D DIRECTIONS = %7B%22N%22, %22W%22, %22S%22, %22E%22%7D;
+enum Direction %7B%0A    N,%0A    W,%0A    S,%0A    E%0A  %7D
 %0A%0A  
@@ -1279,31 +1279,42 @@
 or (
-String dir : DIRECTIONS
+Direction dir : Direction.values()
 ) %7B%0A
@@ -1411,16 +1411,23 @@
      dir
+.name()
 ,%0A      
@@ -1519,20 +1519,23 @@
  dir
-.equals(%22N%22)
+ == Direction.N
  ? U
@@ -1577,14 +1577,16 @@
 E);%0A
+%0A
     %7D%0A
+%0A
   %7D%0A
@@ -1726,17 +1726,27 @@
     
+final 
 double 
+e
 pi
+cPi
 tch 
@@ -1751,26 +1751,28 @@
 h = Math
-.toRadians
+Helper.clamp
 (Helper.
@@ -1801,16 +1801,92 @@
 ionPitch
+ + 30f, -90f, 90f);%0A    final double pitchRadians = Math.toRadians(epicPitch
 ); // pl
@@ -1937,16 +1937,23 @@
 in(pitch
+Radians
 ) * (sca
@@ -2033,32 +2033,39 @@
 dians%0A  private 
+static 
 double getPosOnC
@@ -2075,16 +2075,21 @@
 ass(
-String s
+Direction dir
 ) %7B%0A
@@ -2242,40 +2242,20 @@
 x = 
-ArrayUtils.indexOf(DIRECTIONS, s
+dir.ordinal(
 ) + 

improve compass