package dev.crucifixbeta.mod.gui.menu;

import dev.crucifixbeta.api.utils.render.Render2DUtil;
import dev.crucifixbeta.core.impl.FontManager;
import dev.crucifixbeta.mod.gui.EpsilonMenuBackgroundRenderer;
import dev.crucifixbeta.mod.gui.TokenLoginScreen;
import dev.crucifixbeta.mod.gui.accounts.AccountsScreen;
import dev.crucifixbeta.mod.gui.accounts.AddAccountScreen;
import dev.crucifixbeta.mod.gui.fonts.FontRenderer;
import java.awt.Color;
import java.util.Locale;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.DownloadingTerrainScreen;
import net.minecraft.client.gui.screen.ProgressScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.multiplayer.AddServerScreen;
import net.minecraft.client.gui.screen.multiplayer.ConnectScreen;
import net.minecraft.client.gui.screen.multiplayer.DirectConnectScreen;
import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen;
import net.minecraft.client.gui.screen.world.LevelLoadingScreen;
import net.minecraft.client.gui.screen.world.SelectWorldScreen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableTextContent;

@Environment(EnvType.CLIENT)
public final class EpsilonMenuStyle {

   private EpsilonMenuStyle() {
   }

   public static boolean isStyledScreen(Screen screen) {
      return screen == null
         ? false
         : screen instanceof SelectWorldScreen
            || screen instanceof MultiplayerScreen
            || screen instanceof AddServerScreen
            || screen instanceof DirectConnectScreen
            || screen instanceof ConnectScreen
            || screen instanceof DownloadingTerrainScreen
            || screen instanceof ProgressScreen
            || screen instanceof LevelLoadingScreen
            || screen instanceof TokenLoginScreen
            || screen instanceof AccountsScreen
            || screen instanceof AddAccountScreen
            || screen.getClass().getName().startsWith("de.florianmichael.viafabricplus.screen.");
   }

   public static boolean isCurrentStyledScreen() {
      MinecraftClient var0 = MinecraftClient.getInstance();
      return var0 != null && isStyledScreen(var0.currentScreen);
   }

   public static void renderVideoBackground(DrawContext context, int mouseX, int mouseY) {
      EpsilonMenuBackgroundRenderer.render(context, mouseX, mouseY);
   }

   public static boolean updateButtonText(ButtonWidget button) {
      if (button == null) {
         return false;
      } else {
         String var1 = englishButtonText(button.getMessage());
         if (var1 != null && !var1.equals(button.getMessage().getString())) {
            button.setMessage(Text.literal(var1));
            return true;
         } else {
            return false;
         }
      }
   }

   public static void renderButton(ButtonWidget button, DrawContext context, int mouseX, int mouseY, float delta) {
      updateButtonText(button);
      float var5 = (float)button.getX();
      float var6 = (float)button.getY();
      float var7 = (float)button.getWidth();
      float var8 = (float)button.getHeight();
      boolean var9 = button.isHovered();
      boolean var10 = button.active;
      float var11 = var10 ? 1.0F : 0.48F;
      float var12 = var9 && var10 ? 1.0F : 0.0F;
      float var13 = Math.min(var8 * 0.22F, 4.0F);
      Color var14 = withAlpha(new Color(255, 255, 255), (0.12F + var12 * 0.18F) * var11);
      Color var15 = withAlpha(new Color(0, 0, 0, 170), (0.48F + var12 * 0.16F) * var11);
      Color var16 = withAlpha(var9 && var10 ? new Color(234, 221, 255) : new Color(230, 224, 233), var10 ? 0.96F : 0.55F);
      drawSoftFrame(context, var5, var6, var7, var8, var13, var15, var14);
      drawCenteredText(context, button.getMessage().getString(), var5, var6, var7, var8, var16.getRGB());
   }

   public static void drawSoftFrame(DrawContext context, float x, float y, float width, float height, float radius, Color fill, Color border) {
      if (!(width <= 0.0F) && !(height <= 0.0F)) {
         float var8 = Math.min(radius, Math.min(width, height) * 0.5F);
         Render2DUtil.drawRoundedRectForcedAA(context.getMatrices(), x, y, width, height, var8, fill);
         Render2DUtil.drawRoundedStrokeForcedAA(context.getMatrices(), x, y, width, height, var8, 1.0F, border);
      }
   }

   private static void drawCenteredText(DrawContext context, String text, float x, float y, float width, float height, int color) {
      FontRenderer var7 = FontManager.mainMenu != null ? FontManager.mainMenu : FontManager.ui;
      float var8 = 0.74F;
      if (var7 != null) {
         float var11 = var7.getWidth(text) * var8;
         float var12 = var7.getFontHeight(text) * var8;
         context.getMatrices().push();
         context.getMatrices().scale(var8, var8, 1.0F);
         var7.drawString(
            context.getMatrices(), text, (double)((x + (width - var11) * 0.5F) / var8), (double)((y + (height - var12) * 0.5F) / var8), color, false
         );
         context.getMatrices().pop();
      } else {
         TextRenderer var9 = MinecraftClient.getInstance().textRenderer;
         int var10 = var9.getWidth(text);
         context.drawTextWithShadow(var9, text, Math.round(x + (width - (float)var10) * 0.5F), Math.round(y + (height - 8.0F) * 0.5F), color);
      }
   }

   private static String englishButtonText(Text text) {
      String var1 = translationKey(text);
      if (var1 != null) {
         String var2 = englishByKey(var1);
         if (var2 != null) {
            return var2;
         }
      }

      String var5 = normalize(text.getString());

      return switch (var5) {
         case "\u8fdb\u5165\u9009\u4e2d\u7684\u4e16\u754c", "\u6e38\u73a9\u9009\u4e2d\u7684\u4e16\u754c", "\u9009\u4e2d\u4e16\u754c", "play selected world", "play selected" -> "Play Selected World";
         case "\u521b\u5efa\u65b0\u7684\u4e16\u754c", "\u521b\u5efa\u65b0\u4e16\u754c", "create new world" -> "Create New World";
         case "\u7f16\u8f91", "edit" -> "Edit";
         case "\u5220\u9664", "delete" -> "Delete";
         case "\u91cd\u5efa", "re-create", "recreate" -> "Re-Create";
         case "\u8fd4\u56de", "back", "done" -> "Back";
         case "\u52a0\u5165\u670d\u52a1\u5668", "join server", "join" -> "Join Server";
         case "\u76f4\u63a5\u8fde\u63a5", "direct connection", "direct connect" -> "Direct Connect";
         case "\u6dfb\u52a0\u670d\u52a1\u5668", "add server" -> "Add Server";
         case "\u5237\u65b0", "refresh" -> "Refresh";
         case "tokenlogin", "token login" -> "Token Login";
         case "viafabricplus", "via fabric plus", "force version" -> "ViaFabricPlus";
         case "select version", "set version" -> "ViaFabricPlus";
         default -> null;
      };
   }

   private static String englishByKey(String key) {
      return switch (key) {
         case "selectWorld.select" -> "Play Selected World";
         case "selectWorld.create", "selectWorld.createDemo" -> "Create New World";
         case "selectWorld.edit" -> "Edit";
         case "selectWorld.delete" -> "Delete";
         case "selectWorld.recreate" -> "Re-Create";
         case "gui.back", "gui.done", "gui.cancel" -> "Back";
         case "selectServer.select", "selectServer.selectServer" -> "Join Server";
         case "selectServer.direct" -> "Direct Connect";
         case "selectServer.add" -> "Add Server";
         case "selectServer.edit" -> "Edit";
         case "selectServer.delete" -> "Delete";
         case "selectServer.refresh" -> "Refresh";
         case "addServer.add", "selectServer.addServer" -> "Add Server";
         case "addServer.save", "addServer.edit" -> "Edit";
         case "base.viafabricplus.refresh" -> "Refresh";
         case "base.viafabricplus.cancel", "base.viafabricplus.cancel_and_reset" -> "Back";
         case "base.viafabricplus.set_version", "screen.viafabricplus.force_version" -> "ViaFabricPlus";
         default -> null;
      };
   }

   private static String translationKey(Text text) {
      if (text instanceof MutableText var1 && var1.getContent() instanceof TranslatableTextContent var3) {
         return var3.getKey();
      }

      return null;
   }

   private static String normalize(String value) {
      return value == null
         ? ""
         : value.trim().replace(" ", "").toLowerCase(Locale.ROOT);
   }

   private static Color withAlpha(Color color, float factor) {
      float var2 = Math.max(0.0F, Math.min(1.0F, factor));
      return new Color(color.getRed(), color.getGreen(), color.getBlue(), Math.round((float)color.getAlpha() * var2));
   }
}
