package dev.crucifixbeta.mod.gui.menu;

import com.mojang.blaze3d.systems.RenderSystem;
import dev.crucifixbeta.core.impl.FontManager;
import dev.crucifixbeta.mod.gui.fonts.FontRenderer;
import java.awt.Color;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.network.ServerAddress;
import net.minecraft.client.network.ServerInfo;
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.client.util.math.MatrixStack;
import org.joml.Matrix4f;

@Environment(EnvType.CLIENT)
public final class LoadingScreenStyle {
   private static String connectingIp;

   private LoadingScreenStyle() {
   }

   public static void setConnectingServer(ServerAddress address, ServerInfo info) {
      if (info != null && info.address != null && !info.address.isBlank()) {
         connectingIp = info.address;
      } else if (address != null && address.getAddress() != null && !address.getAddress().isBlank()) {
         connectingIp = address.getAddress() + ":" + address.getPort();
      } else {
         connectingIp = "Unknown Server";
      }
   }

   public static void renderConnectOverlay(DrawContext context) {
      int var1 = context.getScaledWindowWidth();
      int var2 = context.getScaledWindowHeight();
      float var3 = (float)var1 * 0.5F;
      float var4 = (float)var2 * 0.5F;
      MatrixStack var5 = context.getMatrices();
      float var6 = Math.min(260.0F, Math.max(190.0F, (float)var1 - 48.0F));
      float var7 = 92.0F;
      float var8 = var3 - var6 * 0.5F;
      float var9 = var4 - 24.0F;
      EpsilonMenuStyle.drawSoftFrame(context, var8, var9, var6, var7, 7.0F, new Color(0, 0, 0, 148), new Color(255, 255, 255, 22));
      drawSpinner(var5, var3, var9 + 25.0F, 13.0F);
      drawCenteredText(context, "Connecting to", var3, var9 + 47.0F, -422918960, 0.72F);
      drawCenteredText(context, connectingIp, var3, var9 + 62.0F, -186195734, 0.82F);
   }

   private static void drawCenteredText(DrawContext context, String text, float centerX, float y, int color, float scale) {
      FontRenderer var6 = FontManager.mainMenu != null ? FontManager.mainMenu : FontManager.ui;
      if (var6 != null) {
         float var8 = var6.getWidth(text) * scale;
         context.getMatrices().push();
         context.getMatrices().scale(scale, scale, 1.0F);
         var6.drawString(context.getMatrices(), text, (double)((centerX - var8 * 0.5F) / scale), (double)(y / scale), color, false);
         context.getMatrices().pop();
      } else {
         MinecraftClient var7 = MinecraftClient.getInstance();
         context.getMatrices().push();
         context.getMatrices().scale(scale, scale, 1.0F);
         context.drawTextWithShadow(
            var7.textRenderer, text, Math.round((centerX - (float)var7.textRenderer.getWidth(text) * scale * 0.5F) / scale), Math.round(y / scale), color
         );
         context.getMatrices().pop();
      }
   }

   private static void drawSpinner(MatrixStack matrices, float cx, float cy, float radius) {
      long var4 = System.currentTimeMillis();
      double var6 = (double)(var4 % 1200L) / 1200.0 * Math.PI * 2.0;
      byte var8 = 44;
      Matrix4f var9 = matrices.peek().getPositionMatrix();
      RenderSystem.enableBlend();
      RenderSystem.disableCull();
      RenderSystem.setShader(GameRenderer::getPositionColorProgram);
      BufferBuilder var10 = Tessellator.getInstance().begin(DrawMode.DEBUG_LINE_STRIP, VertexFormats.POSITION_COLOR);

      for (int var11 = 0; var11 <= var8; var11++) {
         double var12 = (double)var11 / (double)var8;
         double var14 = var6 + var12 * Math.PI * 1.55;
         float var16 = cx + (float)Math.cos(var14) * radius;
         float var17 = cy + (float)Math.sin(var14) * radius;
         int var18 = Math.round(48.0F + 207.0F * (float)var12);
         var10.vertex(var9, var16, var17, 0.0F).color(var18 << 24 | 15130858);
      }

      BufferRenderer.drawWithGlobalProgram(var10.end());
      RenderSystem.enableCull();
      RenderSystem.disableBlend();
   }

   static {

      connectingIp = "Unknown Server";
   }
}
