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.concurrent.CompletableFuture;
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.client.gui.widget.TextFieldWidget;
import net.minecraft.text.Text;

@Environment(EnvType.CLIENT)
public class AddAccountScreen extends Screen {
   private final Screen parent;
   private final AddAccountScreen.Mode mode;
   private TextFieldWidget input;
   private String status = "";

   public AddAccountScreen(Screen parent, AddAccountScreen.Mode mode) {
      super(Text.literal(mode == AddAccountScreen.Mode.OFFLINE ? "Add Offline Account" : "Add Session Account"));
      this.parent = parent;
      this.mode = mode;
   }

   @Override
   protected void init() {
      int var1 = Math.min(360, this.width - 32);
      int var2 = (this.width - var1) / 2;
      int var3 = this.height / 2 - 46;
      this.input = new TextFieldWidget(
         this.textRenderer,
         var2 + 18,
         var3 + 34,
         var1 - 36,
         20,
         Text.literal(this.mode == AddAccountScreen.Mode.OFFLINE ? "Username" : "Access Token")
      );
      this.input.setMaxLength(this.mode == AddAccountScreen.Mode.OFFLINE ? 16 : 32767);
      this.input.setFocused(true);
      this.addDrawableChild(this.input);
      this.addDrawableChild(
         ButtonWidget.builder(Text.literal("Add"), button -> this.addAccount()).dimensions(var2 + 18, var3 + 64, 100, 20).build()
      );
      this.addDrawableChild(
         ButtonWidget.builder(Text.literal("Back"), button -> this.close()).dimensions(var2 + var1 - 118, var3 + 64, 100, 20).build()
      );
   }

   @Override
   public void render(DrawContext context, int mouseX, int mouseY, float delta) {
      EpsilonMenuStyle.renderVideoBackground(context, mouseX, mouseY);
      int var5 = Math.min(360, this.width - 32);
      int var6 = (this.width - var5) / 2;
      int var7 = this.height / 2 - 66;
      EpsilonMenuStyle.drawSoftFrame(context, (float)var6, (float)var7, (float)var5, 132.0F, 7.0F, new Color(0, 0, 0, 150), new Color(255, 255, 255, 24));
      drawText(context, this.title.getString(), (float)(var6 + 18), (float)(var7 + 15), -219750166, 0.82F);
      drawText(
         context,
         this.mode == AddAccountScreen.Mode.OFFLINE ? "Username" : "Access token",
         (float)(var6 + 18),
         (float)(var7 + 36),
         -657799984,
         0.64F
      );
      if (!this.status.isBlank()) {
         drawText(context, this.status, (float)(var6 + 18), (float)(var7 + 108), -657799984, 0.64F);
      }

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

   private void addAccount() {
      String var1 = this.input.getText().trim();
      if (var1.isEmpty()) {
         this.status = this.mode == AddAccountScreen.Mode.OFFLINE ? "Enter username" : "Enter token";
      } else if (this.mode == AddAccountScreen.Mode.OFFLINE) {
         if (!validUsername(var1)) {
            this.status = "Invalid username";
         } else {
            MenuAccounts.addOffline(var1);
            this.close();
         }
      } else {
         this.status = "Validating token...";
         CompletableFuture.runAsync(() -> {
            try {
               MenuAccounts.Entry var2 = MenuAccounts.addToken(var1);
               MinecraftClient.getInstance().execute(() -> {
                  MenuAccounts.login(var2);
                  this.close();
               });
            } catch (Exception var3) {
               MinecraftClient.getInstance().execute(() -> this.status = "Invalid token");
            }
         });
      }
   }

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

   private static boolean validUsername(String username) {
      return username.length() >= 3
         && username.length() <= 16
         && username.chars().allMatch(c -> c == 95 || c >= 48 && c <= 57 || c >= 65 && c <= 90 || c >= 97 && c <= 122);
   }

   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();
   }
@Environment(EnvType.CLIENT)
   public static enum Mode {
      OFFLINE,
      TOKEN;

}
}
