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

import com.mojang.blaze3d.systems.RenderSystem;
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.EnumSetting;
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 net.minecraft.client.render.BufferBuilder;
import net.minecraft.client.render.BufferRenderer;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.render.Tessellator;
import net.minecraft.client.render.VertexFormats;
import net.minecraft.client.render.VertexFormat.DrawMode;
import net.minecraft.util.Identifier;
import org.joml.Matrix4f;

@Environment(EnvType.CLIENT)
public class GUICharacter extends HudModule {
   public static GUICharacter INSTANCE;
   public final EnumSetting<GUICharacter.CharacterType> characterType = this.add(
      new EnumSetting<>("Character", GUICharacter.CharacterType.Character1)
   );
   public final SliderSetting scale = this.add(new SliderSetting("Scale", 1.0, 0.1, 3.0, 0.1));
   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 GUICharacter() {
      super("GUICharacter", "", "GUI Character", 24, 24, HudModule.Corner.RightBottom);
      INSTANCE = this;
   }

   @Override
   public void onRender2D(DrawContext context, float tickDelta) {
      if (!this.shouldShow()) {
         this.clearHudBounds();
      } else {
         float var3 = this.scale.getValueFloat();
         int var4 = Math.max(10, Math.round(100.0F * var3));
         int var5 = Math.max(10, Math.round(100.0F * var3));
         int var6 = this.getHudRenderX(var4);
         int var7 = this.getHudRenderY(var5);
         drawTexture(context, this.characterType.getValue().texture, (float)var6, (float)var7, (float)var4, (float)var5);
         this.setHudBounds(var6, var7, var4, var5);
      }
   }

   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
            ? true
            : this.showInEscape.getValue()
               && !(mc.currentScreen instanceof ChatScreen)
               && !(mc.currentScreen instanceof InventoryScreen)
               && !(mc.currentScreen instanceof ClickGuiScreen);
      }
   }

   private static void drawTexture(DrawContext context, Identifier texture, float x, float y, float width, float height) {
      RenderSystem.enableBlend();
      RenderSystem.setShader(GameRenderer::getPositionTexColorProgram);
      RenderSystem.setShaderTexture(0, texture);
      Matrix4f var6 = context.getMatrices().peek().getPositionMatrix();
      BufferBuilder var7 = Tessellator.getInstance().begin(DrawMode.QUADS, VertexFormats.POSITION_TEXTURE_COLOR);
      var7.vertex(var6, x, y + height, 0.0F).texture(0.0F, 1.0F).color(255, 255, 255, 255);
      var7.vertex(var6, x + width, y + height, 0.0F).texture(1.0F, 1.0F).color(255, 255, 255, 255);
      var7.vertex(var6, x + width, y, 0.0F).texture(1.0F, 0.0F).color(255, 255, 255, 255);
      var7.vertex(var6, x, y, 0.0F).texture(0.0F, 0.0F).color(255, 255, 255, 255);
      BufferRenderer.drawWithGlobalProgram(var7.end());
      RenderSystem.disableBlend();
   }
@Environment(EnvType.CLIENT)
   public static enum CharacterType {
      Character1("character_1.png"),
      Character2("character_2.png"),
      Character3("character_3.png"),
      Character4("character_4.png"),
      Character5("character_5.png"),
      Character7("character_7.png"),
      Character9("character_9.png"),
      Character10("character_10.png");

      private final Identifier texture;

      private CharacterType(String file) {
         this.texture = Identifier.of("alien", "textures/character/" + file);
      }

}
}
