package dev.crucifixbeta.mod.gui.accounts;

import dev.crucifixbeta.core.impl.FontManager;
import dev.crucifixbeta.mod.gui.fonts.FontRenderer;
import dev.crucifixbeta.mod.gui.menu.EpsilonMenuStyle;
import java.awt.Color;
import java.util.ArrayList;
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.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.text.Text;

@Environment(EnvType.CLIENT)
public class AccountsScreen extends Screen {
   private final Screen parent;
   private String status = "";

   public AccountsScreen(Screen parent) {
      super(Text.literal("Accounts"));
      this.parent = parent;
   }

   @Override
   protected void init() {
      int var1 = Math.min(420, this.width - 32);
      int var2 = (this.width - var1) / 2;
      int var3 = Math.max(54, this.height / 2 - 118);
      int var4 = 0;

      for (MenuAccounts.Entry var7 : new ArrayList<>(MenuAccounts.all())) {
         int var8 = var3 + 36 + var4 * 25;
         this.addDrawableChild(ButtonWidget.builder(Text.literal("Login"), button -> {
            if (MenuAccounts.login(var7)) {
               this.status = "Logged in as " + var7.displayName();
            }
         }).dimensions(var2 + var1 - 130, var8, 60, 20).build());
         this.addDrawableChild(ButtonWidget.builder(Text.literal("Delete"), button -> {
            MenuAccounts.remove(var7);
            this.reload();
         }).dimensions(var2 + var1 - 65, var8, 60, 20).build());
         var4++;
      }

      int var9 = Math.min(this.height - 52, var3 + 52 + Math.max(4, var4) * 25);
      this.addDrawableChild(ButtonWidget.builder(Text.literal("Add Offline"), button -> {
         if (this.client != null) {
            this.client.setScreen(new AddAccountScreen(this, AddAccountScreen.Mode.OFFLINE));
         }
      }).dimensions(var2, var9, 128, 20).build());
      this.addDrawableChild(ButtonWidget.builder(Text.literal("Add Session"), button -> {
         if (this.client != null) {
            this.client.setScreen(new AddAccountScreen(this, AddAccountScreen.Mode.TOKEN));
         }
      }).dimensions(var2 + 134, var9, 128, 20).build());
      this.addDrawableChild(
         ButtonWidget.builder(Text.literal("Back"), button -> this.close()).dimensions(var2 + var1 - 100, var9, 100, 20).build()
      );
   }

   private void reload() {
      this.clearChildren();
      this.init();
   }

   @Override
   public void render(DrawContext context, int mouseX, int mouseY, float delta) {
      EpsilonMenuStyle.renderVideoBackground(context, mouseX, mouseY);
      int var5 = Math.min(420, this.width - 32);
      int var6 = Math.min(this.height - 44, 190 + Math.max(0, MenuAccounts.all().size() - 4) * 25);
      int var7 = (this.width - var5) / 2;
      int var8 = Math.max(22, this.height / 2 - var6 / 2);
      EpsilonMenuStyle.drawSoftFrame(context, (float)var7, (float)var8, (float)var5, (float)var6, 7.0F, new Color(0, 0, 0, 150), new Color(255, 255, 255, 24));
      drawText(context, "Accounts", (float)(var7 + 18), (float)(var8 + 14), -219750166, 0.86F);
      drawText(context, "Logged in as " + MinecraftClient.getInstance().getSession().getUsername(), (float)(var7 + 18), (float)(var8 + 29), -657799984, 0.67F);
      if (!this.status.isBlank()) {
         drawText(context, this.status, (float)(var7 + 18), (float)(var8 + var6 - 23), -657799984, 0.64F);
      }

      int var9 = 0;

      for (MenuAccounts.Entry var11 : MenuAccounts.all()) {
         int var12 = var8 + 39 + var9 * 25;
         String var13 = var11.displayName();
         String var14 = var11.typeName();
         drawText(context, var13, (float)(var7 + 18), (float)(var12 + 6), -286859030, 0.72F);
         drawText(context, var14, (float)(var7 + 128), (float)(var12 + 6), -1194670896, 0.64F);
         var9++;
      }

      if (MenuAccounts.all().isEmpty()) {
         drawText(context, "No accounts added", (float)(var7 + 18), (float)(var8 + 62), -1194670896, 0.72F);
      }

      super.render(context, mouseX, mouseY, delta);
   }

   @Override
   public void close() {
      if (this.client != null) {
         this.client.setScreen(this.parent);
      }
   }

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

      context.getMatrices().pop();
   }
}
