package com.tiedup.remake.client.gui.screens;

import com.tiedup.remake.client.gui.util.GuiTextureHelper;
import com.tiedup.remake.client.gui.widgets.EntityPreviewWidget;
import com.tiedup.remake.entities.EntityDamsel;
import com.tiedup.remake.entities.NpcInventoryMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Inventory;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.jetbrains.annotations.Nullable;

/**
 * Screen for viewing and managing NPC inventory.
 * Uses vanilla chest texture for consistent Minecraft look.
 *
 * Layout:
 * - Left: Vanilla chest-style container (176px wide)
 * - Right: Equipment panel (armor + main hand, 26px wide)
 */
@OnlyIn(Dist.CLIENT)
public class NpcInventoryScreen
    extends AbstractContainerScreen<NpcInventoryMenu>
{

    /** Equipment panel width (slot 18px + 4px padding each side) */
    private static final int EQUIP_PANEL_WIDTH = 28;

    /** Gap between main container and equipment panel */
    private static final int EQUIP_PANEL_GAP = 4;

    /** Equipment panel height (5 slots + header + padding) */
    private static final int EQUIP_PANEL_HEIGHT = 5 * 18 + 18; // 108px

    /** Entity preview widget */
    @Nullable
    private EntityPreviewWidget preview;

    /** Number of NPC inventory rows */
    private final int npcRows;

    /**
     * Create NPC inventory screen.
     *
     * @param menu            The container menu
     * @param playerInventory Player's inventory
     * @param title           Screen title (will be replaced with NPC name)
     */
    public NpcInventoryScreen(
        NpcInventoryMenu menu,
        Inventory playerInventory,
        Component title
    ) {
        super(menu, playerInventory, Component.translatable("gui.tiedup.npc_inventory.title_name", menu.getNpcName()));
        // Calculate rows from NPC inventory size
        this.npcRows = (menu.getNpcSlotCount() + 8) / 9;

        // Standard vanilla chest dimensions
        this.imageWidth = GuiTextureHelper.CHEST_WIDTH;
        this.imageHeight = GuiTextureHelper.getChestHeight(npcRows);

        // Player inventory label position (relative to container)
        // In vanilla chest, player inv label is at y = header + rows*18 + 3
        this.inventoryLabelY =
            GuiTextureHelper.CHEST_HEADER_HEIGHT +
            npcRows * GuiTextureHelper.SLOT_SIZE +
            3;
    }

    @Override
    protected void init() {
        super.init();

        // Add entity preview if NPC available and there's room
        EntityDamsel npc = this.menu.getNpc();
        if (npc != null) {
            int previewSize = 50;
            int previewX = this.leftPos - previewSize - 10;
            int previewY = this.topPos + 10;

            if (previewX > 0) {
                preview = new EntityPreviewWidget(
                    previewX,
                    previewY,
                    previewSize,
                    previewSize,
                    npc
                );
                preview.setAutoRotate(true);
                preview.setAutoRotateSpeed(0.5f);
                this.addRenderableWidget(preview);
            }
        }
    }

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

    @Override
    protected void renderBg(
        GuiGraphics graphics,
        float partialTick,
        int mouseX,
        int mouseY
    ) {
        // Render main container with vanilla chest texture
        GuiTextureHelper.renderChestBackground(
            graphics,
            this.leftPos,
            this.topPos,
            this.npcRows
        );

        // Render equipment panel on the right
        renderEquipmentPanel(graphics);
    }

    /**
     * Render the equipment panel (armor + main hand) on the right side.
     */
    private void renderEquipmentPanel(GuiGraphics graphics) {
        int panelX =
            this.leftPos + GuiTextureHelper.CHEST_WIDTH + EQUIP_PANEL_GAP;
        int panelY = this.topPos + GuiTextureHelper.CHEST_HEADER_HEIGHT;

        // Panel background (vanilla inventory style)
        graphics.fill(
            panelX - 1,
            panelY - 1,
            panelX + EQUIP_PANEL_WIDTH + 1,
            panelY + EQUIP_PANEL_HEIGHT + 1,
            0xFF000000
        );
        graphics.fill(
            panelX,
            panelY,
            panelX + EQUIP_PANEL_WIDTH,
            panelY + EQUIP_PANEL_HEIGHT,
            0xFFC6C6C6
        );

        // Title centered
        String title = "Gear";
        int titleWidth = this.font.width(title);
        graphics.drawString(
            this.font,
            title,
            panelX + (EQUIP_PANEL_WIDTH - titleWidth) / 2,
            panelY + 3,
            0x404040,
            false
        );

        // Slot backgrounds (4 armor + 1 main hand)
        // Position slots to match menu: slotX = panelX + 5, first slot at y = panelY + 12
        int slotX = panelX + 5;
        int slotStartY = panelY + 12;
        for (int i = 0; i < 4; i++) {
            renderSlot(
                graphics,
                slotX,
                slotStartY + i * GuiTextureHelper.SLOT_SIZE
            );
        }

        // Main hand slot (with small gap after armor)
        int handY = slotStartY + 4 * GuiTextureHelper.SLOT_SIZE + 4;
        renderSlot(graphics, slotX, handY);
    }

    /**
     * Render a single slot background in vanilla style.
     */
    private void renderSlot(GuiGraphics graphics, int x, int y) {
        // Vanilla slot style: dark top-left border, light bottom-right
        graphics.fill(x, y, x + 18, y + 18, 0xFF8B8B8B); // Base gray
        graphics.fill(x, y, x + 17, y + 1, 0xFF373737); // Top dark
        graphics.fill(x, y, x + 1, y + 17, 0xFF373737); // Left dark
        graphics.fill(x + 1, y + 17, x + 18, y + 18, 0xFFFFFFFF); // Bottom light
        graphics.fill(x + 17, y + 1, x + 18, y + 18, 0xFFFFFFFF); // Right light
        graphics.fill(x + 1, y + 1, x + 17, y + 17, 0xFF8B8B8B); // Inner
    }

    @Override
    protected void renderLabels(GuiGraphics graphics, int mouseX, int mouseY) {
        // NPC name in title area
        graphics.drawString(
            this.font,
            this.title,
            this.titleLabelX,
            this.titleLabelY,
            0x404040,
            false
        );

        // Player inventory label
        graphics.drawString(
            this.font,
            this.playerInventoryTitle,
            this.inventoryLabelX,
            this.inventoryLabelY,
            0x404040,
            false
        );
    }

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