package dev.crucifixbeta.mod.gui;

import com.mojang.blaze3d.systems.RenderSystem;
import dev.crucifixbeta.api.utils.Wrapper;
import dev.crucifixbeta.mod.modules.impl.misc.ShulkerViewer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.ShulkerBoxBlock;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.ingame.BookScreen;
import net.minecraft.client.gui.screen.ingame.ShulkerBoxScreen;
import net.minecraft.client.gui.screen.ingame.BookScreen.Contents;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.screen.ShulkerBoxScreenHandler;
import net.minecraft.util.DyeColor;
import net.minecraft.util.Identifier;

@Environment(EnvType.CLIENT)
public class PeekScreen extends ShulkerBoxScreen {
   private static final Identifier TEXTURE;
   private final ItemStack[] contents;
   private final ItemStack storageBlock;
   private final int colorArgb;

   public PeekScreen(ItemStack storageBlock, ItemStack[] contents) {
      super(
         new ShulkerBoxScreenHandler(0, Wrapper.mc.player.getInventory(), new SimpleInventory(contents)),
         Wrapper.mc.player.getInventory(),
         storageBlock.getName()
      );
      this.contents = contents;
      this.storageBlock = storageBlock;
      this.colorArgb = getShulkerColorArgb(storageBlock);
   }

   private static int getShulkerColorArgb(ItemStack shulkerItem) {
      if (shulkerItem.getItem() instanceof BlockItem var2 && var2.getBlock() instanceof ShulkerBoxBlock var4) {
         DyeColor var5 = var4.getColor();
         if (var5 == null) {
            return -1;
         }

         int var6 = var5.getEntityColor();
         return 0xFF000000 | var6;
      }

      return -1;
   }

   @Override
   public boolean mouseClicked(double mouseX, double mouseY, int button) {
      if (button == 2
         && this.focusedSlot != null
         && !this.focusedSlot.getStack().isEmpty()
         && Wrapper.mc.player.currentScreenHandler.getCursorStack().isEmpty()) {
         ItemStack var6 = this.focusedSlot.getStack();
         if (ShulkerViewer.hasItems(var6) || var6.getItem() == Items.ENDER_CHEST) {
            return ShulkerViewer.openContainer(this.focusedSlot.getStack(), this.contents, false);
         }

         if (var6.get(DataComponentTypes.WRITTEN_BOOK_CONTENT) != null || var6.get(DataComponentTypes.WRITABLE_BOOK_CONTENT) != null) {
            this.close();
            Wrapper.mc.setScreen(new BookScreen(Contents.create(var6)));
            return true;
         }
      }

      return false;
   }

   @Override
   public boolean mouseReleased(double mouseX, double mouseY, int button) {
      return false;
   }

   @Override
   public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
      if (keyCode != 256 && !Wrapper.mc.options.inventoryKey.matchesKey(keyCode, scanCode)) {
         return false;
      } else {
         this.close();
         return true;
      }
   }

   @Override
   public boolean keyReleased(int keyCode, int scanCode, int modifiers) {
      if (keyCode == 256) {
         this.close();
         return true;
      } else {
         return false;
      }
   }

   @Override
   protected void drawBackground(DrawContext context, float delta, int mouseX, int mouseY) {
      float var5 = (float)(this.colorArgb >> 16 & 0xFF) / 255.0F;
      float var6 = (float)(this.colorArgb >> 8 & 0xFF) / 255.0F;
      float var7 = (float)(this.colorArgb & 0xFF) / 255.0F;
      float var8 = (float)(this.colorArgb >> 24 & 0xFF) / 255.0F;
      RenderSystem.setShader(GameRenderer::getPositionTexProgram);
      RenderSystem.setShaderColor(var5, var6, var7, var8);
      int var9 = (this.width - this.backgroundWidth) / 2;
      int var10 = (this.height - this.backgroundHeight) / 2;
      context.drawTexture(TEXTURE, var9, var10, 0, 0, this.backgroundWidth, this.backgroundHeight);
      RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
   }

   static {

      TEXTURE = Identifier.of("textures/gui/container/shulker_box.png");
   }
}
