package dev.crucifixbeta.mod.gui.windows;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;

@Environment(EnvType.CLIENT)
public class WindowsScreen extends Screen {
   public static WindowBase draggingWindow;
   public static WindowBase lastClickedWindow;
   private final List<WindowBase> windows = new ArrayList<>();

   public WindowsScreen(WindowBase... windows) {
      super(Text.literal("Windows"));
      if (windows != null) {
         this.windows.addAll(Arrays.asList(windows));
      }
   }

   @Override
   public void render(DrawContext context, int mouseX, int mouseY, float delta) {
      this.renderBackground(context, mouseX, mouseY, delta);

      for (WindowBase var6 : this.windows) {
         if (var6 != null && var6.isVisible()) {
            var6.render(context, mouseX, mouseY);
         }
      }

      this.windows.removeIf(window -> window == null || !window.isVisible());
      if (this.windows.isEmpty()) {
         this.close();
      }
   }

   @Override
   public boolean mouseClicked(double mouseX, double mouseY, int button) {
      for (int var6 = this.windows.size() - 1; var6 >= 0; var6--) {
         WindowBase var7 = this.windows.get(var6);
         if (var7 != null && var7.isVisible()) {
            var7.mouseClicked(mouseX, mouseY, button);
            if (lastClickedWindow == var7) {
               this.windows.remove(var6);
               this.windows.add(var7);
               return true;
            }
         }
      }

      return super.mouseClicked(mouseX, mouseY, button);
   }

   @Override
   public boolean mouseReleased(double mouseX, double mouseY, int button) {
      for (WindowBase var7 : this.windows) {
         if (var7 != null && var7.isVisible()) {
            var7.mouseReleased(mouseX, mouseY, button);
         }
      }

      draggingWindow = null;
      return super.mouseReleased(mouseX, mouseY, button);
   }

   @Override
   public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) {
      for (int var9 = this.windows.size() - 1; var9 >= 0; var9--) {
         WindowBase var10 = this.windows.get(var9);
         if (var10 != null && var10.isVisible()) {
            var10.mouseScrolled((int)verticalAmount);
         }
      }

      return super.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount);
   }

   @Override
   public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
      for (WindowBase var5 : this.windows) {
         if (var5 != null && var5.isVisible()) {
            var5.keyPressed(keyCode, scanCode, modifiers);
         }
      }

      return super.keyPressed(keyCode, scanCode, modifiers);
   }

   @Override
   public boolean charTyped(char chr, int modifiers) {
      for (WindowBase var4 : this.windows) {
         if (var4 != null && var4.isVisible()) {
            var4.charTyped(chr, modifiers);
         }
      }

      return super.charTyped(chr, modifiers);
   }

   @Override
   public boolean shouldPause() {
      return false;
   }
}
