package com.trunksbomb.minetriad.client.screen;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.math.Axis;
import com.trunksbomb.minetriad.MineTriad;
import com.trunksbomb.minetriad.client.render.TriadCardItemRenderer;
import com.trunksbomb.minetriad.menu.CardBinderMenu;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;

public class CardBinderScreen extends AbstractContainerScreen<CardBinderMenu> {
    private static final ResourceLocation TEXTURE = ResourceLocation.fromNamespaceAndPath(MineTriad.MOD_ID, "textures/gui/card_binder.png");
    private static final ResourceLocation SELECTOR_TEXTURE = ResourceLocation.fromNamespaceAndPath(MineTriad.MOD_ID, "textures/gui/card_binder_selector.png");

    private Button previousPageButton;
    private Button nextPageButton;
    private Button handsButton;

    public CardBinderScreen(CardBinderMenu menu, Inventory playerInventory, Component title) {
        super(menu, playerInventory, title);
        imageWidth = 248;
        imageHeight = 222;
        inventoryLabelY = imageHeight - 92;
    }

    @Override
    protected void init() {
        super.init();
        previousPageButton = addRenderableWidget(Button.builder(Component.literal("<"), button -> pressMenuButton(CardBinderMenu.BUTTON_PREVIOUS_PAGE))
                .bounds(leftPos + 48, topPos + 108, 20, 20)
                .build());
        nextPageButton = addRenderableWidget(Button.builder(Component.literal(">"), button -> pressMenuButton(CardBinderMenu.BUTTON_NEXT_PAGE))
                .bounds(leftPos + 72, topPos + 108, 20, 20)
                .build());
        handsButton = addRenderableWidget(Button.builder(Component.translatable("gui.minetriad.card_binder.hands"), button -> pressMenuButton(CardBinderMenu.BUTTON_HANDS))
                .bounds(leftPos + 48, topPos + 86, 44, 20)
                .build());
        handsButton.active = false;
        updateButtons();
    }

    @Override
    public void containerTick() {
        super.containerTick();
        updateButtons();
    }

    @Override
    protected void renderBg(GuiGraphics guiGraphics, float partialTick, int mouseX, int mouseY) {
        RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
        guiGraphics.blit(TEXTURE, leftPos, topPos, 0, 0, imageWidth, imageHeight, imageWidth, imageHeight);
    }

    @Override
    protected void renderLabels(GuiGraphics guiGraphics, int mouseX, int mouseY) {
        guiGraphics.drawString(font, title, 8, 6, 0x2E2418, false);
        guiGraphics.drawString(font, Component.translatable("gui.minetriad.card_binder.level", menu.currentLevel()), 48, 76, 0x2E2418, false);
        guiGraphics.drawString(font, Component.translatable("gui.minetriad.card_binder.preview"), 138, 12, 0x2E2418, false);
        guiGraphics.drawString(font, playerInventoryTitle, inventoryLabelX, inventoryLabelY, 0x2E2418, false);
    }

    @Override
    public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
        renderBackground(guiGraphics, mouseX, mouseY, partialTick);
        super.render(guiGraphics, mouseX, mouseY, partialTick);
        renderSelection(guiGraphics);
        renderPreview(guiGraphics, partialTick);
        renderTooltip(guiGraphics, mouseX, mouseY);
    }

    private void pressMenuButton(int buttonId) {
        if (minecraft != null && minecraft.gameMode != null) {
            minecraft.gameMode.handleInventoryButtonClick(menu.containerId, buttonId);
            updateButtons();
        }
    }

    private void updateButtons() {
        previousPageButton.active = menu.currentPage() > 0;
        nextPageButton.active = menu.currentPage() < CardBinderMenu.PAGE_COUNT - 1;
    }

    private void renderSelection(GuiGraphics guiGraphics) {
        int selectedSlotIndex = menu.selectedBinderSlot();
        if (selectedSlotIndex < 0 || selectedSlotIndex >= menu.slots.size()) {
            return;
        }

        Slot slot = menu.slots.get(selectedSlotIndex);
        if (!slot.isActive() || !slot.hasItem()) {
            return;
        }

        guiGraphics.blit(SELECTOR_TEXTURE, leftPos + slot.x, topPos + slot.y, 0, 0, 16, 16, 16, 16);
    }

    private void renderPreview(GuiGraphics guiGraphics, float partialTick) {
        ItemStack previewStack = menu.previewStack();
        if (previewStack.isEmpty() || minecraft == null || minecraft.level == null) {
            return;
        }

        float rotation = ((minecraft.level.getGameTime() + partialTick) * 2.4F) % 360.0F;

        guiGraphics.pose().pushPose();
        guiGraphics.pose().translate(leftPos + 184.0F, topPos + 71.0F, 200.0F);
        guiGraphics.pose().mulPose(Axis.XP.rotationDegrees(18.0F));
        guiGraphics.pose().mulPose(Axis.YP.rotationDegrees(rotation));
        guiGraphics.pose().scale(84.0F, -84.0F, 84.0F);
        TriadCardItemRenderer.renderCard(previewStack, guiGraphics.pose(), minecraft.renderBuffers().bufferSource(), TriadCardItemRenderer.neutralPalette());
        minecraft.renderBuffers().bufferSource().endBatch();
        guiGraphics.pose().popPose();
    }
}
