package asm.mixins;

import asm.accessors.IScreen;
import dev.crucifixbeta.CrucifixBeta;
import dev.crucifixbeta.api.events.impl.ServerConnectBeginEvent;
import dev.crucifixbeta.mod.gui.menu.LoadingScreenStyle;
import java.util.Locale;
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.Drawable;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.multiplayer.ConnectScreen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.network.CookieStorage;
import net.minecraft.client.network.ServerAddress;
import net.minecraft.client.network.ServerInfo;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableTextContent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Environment(EnvType.CLIENT)
@Mixin({ConnectScreen.class})
public abstract class MixinConnectScreen {
   @Inject(
      method = {"connect(Lnet/minecraft/client/gui/screen/Screen;Lnet/minecraft/client/MinecraftClient;Lnet/minecraft/client/network/ServerAddress;Lnet/minecraft/client/network/ServerInfo;ZLnet/minecraft/client/network/CookieStorage;)V"},
      at = {@At("HEAD")}
   )
   private static void tryConnectEvent(
      Screen screen, MinecraftClient client, ServerAddress address, ServerInfo info, boolean quickPlay, CookieStorage cookieStorage, CallbackInfo ci
   ) {
      CrucifixBeta.EVENT_BUS.post(ServerConnectBeginEvent.get(address, info));
      LoadingScreenStyle.setConnectingServer(address, info);
   }

   @Inject(
      method = {"connect(Lnet/minecraft/client/MinecraftClient;Lnet/minecraft/client/network/ServerAddress;Lnet/minecraft/client/network/ServerInfo;Lnet/minecraft/client/network/CookieStorage;)V"},
      at = {@At("HEAD")}
   )
   private void tryConnectEvent(MinecraftClient client, ServerAddress address, ServerInfo info, CookieStorage cookieStorage, CallbackInfo ci) {
      CrucifixBeta.EVENT_BUS.post(ServerConnectBeginEvent.get(address, info));
      LoadingScreenStyle.setConnectingServer(address, info);
   }

   @Inject(
      method = {"init"},
      at = {@At("TAIL")}
   )
   private void crucifix$moveBackButtonDown(CallbackInfo ci) {
      MinecraftClient var2 = MinecraftClient.getInstance();
      int var3 = var2 == null ? 0 : var2.getWindow().getScaledHeight();

      for (Drawable var5 : ((IScreen)this).getDrawables()) {
         if (var5 instanceof ButtonWidget) {
            ButtonWidget var6 = (ButtonWidget)var5;
            if (crucifix$isConnectBackButton(var6)) {
               int var7 = var6.getY() + var6.getHeight() * 2 + 24;
               if (var3 > 0) {
                  var7 = Math.min(var7, var3 - var6.getHeight() - 8);
               }

               var6.setY(var7);
            }
         }
      }
   }

   @Inject(
      method = {"render"},
      at = {@At("TAIL")}
   )
   private void crucifix$renderConnectingOverlay(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci) {
      LoadingScreenStyle.renderConnectOverlay(context);
   }

   private static boolean crucifix$isConnectBackButton(ButtonWidget button) {
      Text var1 = button.getMessage();
      if (var1 instanceof MutableText var2 && var2.getContent() instanceof TranslatableTextContent var4) {
         String var6 = var4.getKey();
         if ("gui.cancel".equals(var6) || "gui.back".equals(var6)) {
            return true;
         }
      }

      String var5 = var1.getString().trim().toLowerCase(Locale.ROOT);
      return var5.equals("cancel") || var5.equals("back") || var5.equals("取消") || var5.equals("返回");
   }
}
