// Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission.
//
// SPDX-License-Identifier: LicenseRef-CCPL

package dan200.computercraft.client.gui;

import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.client.gui.widgets.ComputerSidebar;
import dan200.computercraft.client.gui.widgets.TerminalWidget;
import dan200.computercraft.core.util.Nullability;
import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.inventory.AbstractComputerMenu;
import dan200.computercraft.shared.turtle.inventory.TurtleMenu;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.renderer.RenderPipelines;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.world.entity.player.Inventory;

import static dan200.computercraft.shared.turtle.inventory.TurtleMenu.*;

/**
 * The GUI for turtles.
 */
public class TurtleScreen extends AbstractComputerScreen<TurtleMenu> {
    private static final Identifier BACKGROUND_NORMAL = Identifier.fromNamespaceAndPath(ComputerCraftAPI.MOD_ID, "textures/gui/turtle_normal.png");
    private static final Identifier BACKGROUND_ADVANCED = Identifier.fromNamespaceAndPath(ComputerCraftAPI.MOD_ID, "textures/gui/turtle_advanced.png");

    private static final Identifier SELECTED_NORMAL = Identifier.fromNamespaceAndPath(ComputerCraftAPI.MOD_ID, "turtle_normal_selected_slot");
    private static final Identifier SELECTED_ADVANCED = Identifier.fromNamespaceAndPath(ComputerCraftAPI.MOD_ID, "turtle_advanced_selected_slot");

    private static final int TEX_WIDTH = 278;
    private static final int TEX_HEIGHT = 217;

    private static final int FULL_TEX_SIZE = 512;

    public TurtleScreen(TurtleMenu container, Inventory player, Component title) {
        super(container, player, title, BORDER, TEX_WIDTH + AbstractComputerMenu.SIDEBAR_WIDTH, TEX_HEIGHT);
    }

    @Override
    protected TerminalWidget createTerminal() {
        return new TerminalWidget(
            terminalData, computerInput, computerActions,
            leftPos + BORDER + AbstractComputerMenu.SIDEBAR_WIDTH, topPos + BORDER
        );
    }

    @Override
    public void extractBackground(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float partialTicks) {
        super.extractBackground(graphics, mouseX, mouseY, partialTicks);
        var advanced = family == ComputerFamily.ADVANCED;
        graphics.blit(
            RenderPipelines.GUI_TEXTURED, advanced ? BACKGROUND_ADVANCED : BACKGROUND_NORMAL,
            leftPos + AbstractComputerMenu.SIDEBAR_WIDTH, topPos, 0, 0,
            TEX_WIDTH, TEX_HEIGHT, FULL_TEX_SIZE, FULL_TEX_SIZE
        );

        // Render selected slot
        var slot = getMenu().getSelectedSlot();
        if (slot >= 0) {
            var slotX = slot % 4;
            var slotY = slot / 4;
            graphics.blitSprite(
                RenderPipelines.GUI_TEXTURED, advanced ? SELECTED_ADVANCED : SELECTED_NORMAL,
                leftPos + TURTLE_START_X - 2 + slotX * 18, topPos + PLAYER_START_Y - 2 + slotY * 18, 22, 22
            );
        }

        // Render sidebar
        graphics.blitSprite(
            RenderPipelines.GUI_TEXTURED, Nullability.assertNonNull(GuiSprites.getComputerTextures(family).sidebar()),
            leftPos, topPos + sidebarYOffset, AbstractComputerMenu.SIDEBAR_WIDTH, ComputerSidebar.HEIGHT
        );
    }
}
