package dev.crucifixbeta.mod.modules.impl.client.hud;

import dev.crucifixbeta.mod.gui.clickgui.ClickGuiScreen;
import dev.crucifixbeta.mod.modules.HudModule;
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;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.ChatScreen;
import net.minecraft.client.gui.screen.ingame.InventoryScreen;
import org.joml.Quaternionf;
import org.joml.Vector3f;
import wtf.opal.client.screen.click.dropdown.DropdownClickGUI;

@Environment(EnvType.CLIENT)
public class PlayerModule extends HudModule {
   public static PlayerModule INSTANCE;
   public final SliderSetting scale = this.add(new SliderSetting("Scale", 1.0, 0.25, 3.0, 0.05));
   public final SliderSetting yaw = this.add(new SliderSetting("Yaw", 180.0, 0.0, 360.0, 1.0));
   public final SliderSetting pitch = this.add(new SliderSetting("Pitch", 0.0, -35.0, 35.0, 1.0));
   public final BooleanSetting showInChat = this.add(new BooleanSetting("ShowInChat", true));
   public final BooleanSetting showInInventory = this.add(new BooleanSetting("ShowInInventory", true));
   public final BooleanSetting showInEscape = this.add(new BooleanSetting("ShowInEscape", true));
   public final BooleanSetting showInClickGui = this.add(new BooleanSetting("ShowInClickGui", true));

   public PlayerModule() {
      super("PlayerModule", "", "PlayerModule", 24, 24, HudModule.Corner.RightBottom);
      INSTANCE = this;
   }

   @Override
   public void onRender2D(DrawContext context, float tickDelta) {
      if (this.shouldShow() && mc.player != null) {
         float var3 = this.scale.getValueFloat();
         int var4 = Math.max(34, Math.round(58.0F * var3));
         int var5 = Math.max(50, Math.round(82.0F * var3));
         int var6 = this.getHudRenderX(var4);
         int var7 = this.getHudRenderY(var5);
         int var8 = Math.max(18, Math.round(32.0F * var3));
         Quaternionf var9 = new Quaternionf().rotateZ((float) Math.PI).rotateY((float)Math.toRadians((double)this.yaw.getValueFloat()));
         Quaternionf var10 = new Quaternionf().rotateX((float)Math.toRadians((double)this.pitch.getValueFloat()));
         var9.mul(var10);
         Vector3f var11 = new Vector3f(0.0F, mc.player.getHeight() / 2.0F, 0.0F);
         InventoryScreen.drawEntity(context, (float)(var6 + var4 / 2), (float)(var7 + var5 - 4), (float)var8, var11, var9, var10, mc.player);
         this.setHudBounds(var6, var7, var4, var5);
      } else {
         this.clearHudBounds();
      }
   }

   private boolean shouldShow() {
      if (mc.currentScreen == null) {
         return false;
      } else if (this.showInChat.getValue() && mc.currentScreen instanceof ChatScreen) {
         return true;
      } else if (this.showInInventory.getValue() && mc.currentScreen instanceof InventoryScreen) {
         return true;
      } else {
         return !this.showInClickGui.getValue() || !(mc.currentScreen instanceof ClickGuiScreen) && !(mc.currentScreen instanceof DropdownClickGUI)
            ? this.showInEscape.getValue()
               && !(mc.currentScreen instanceof ChatScreen)
               && !(mc.currentScreen instanceof InventoryScreen)
               && !(mc.currentScreen instanceof ClickGuiScreen)
               && !(mc.currentScreen instanceof DropdownClickGUI)
            : true;
      }
   }
}
