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

package dan200.computercraft.client.gui;

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.inventory.AbstractComputerMenu;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.renderer.RenderPipelines;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Inventory;

import static dan200.computercraft.client.render.ComputerBorderRenderer.BORDER;

/**
 * A GUI for computers which renders the terminal (and border), but with no UI elements.
 * <p>
 * This is used by computers and pocket computers.
 *
 * @param <T> The concrete type of the associated menu.
 */
public final class ComputerScreen<T extends AbstractComputerMenu> extends AbstractComputerScreen<T> {
    public ComputerScreen(T container, Inventory player, Component title) {
        super(
            container, player, title, BORDER,
            TerminalWidget.getWidth(container.getTerminal().getWidth()) + BORDER * 2 + AbstractComputerMenu.SIDEBAR_WIDTH,
            TerminalWidget.getHeight(container.getTerminal().getHeight()) + BORDER * 2
        );
    }

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

    @Override
    public void extractBackground(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float partialTicks) {
        super.extractBackground(graphics, mouseX, mouseY, partialTicks);
        // Draw a border around the terminal
        var terminal = getTerminal();
        var computerTextures = GuiSprites.getComputerTextures(family);

        graphics.blitSprite(
            RenderPipelines.GUI_TEXTURED, computerTextures.border(),
            terminal.getX() - BORDER, terminal.getY() - BORDER, terminal.getWidth() + BORDER * 2, terminal.getHeight() + BORDER * 2
        );

        graphics.blitSprite(
            RenderPipelines.GUI_TEXTURED, Nullability.assertNonNull(computerTextures.sidebar()),
            leftPos, topPos + sidebarYOffset, AbstractComputerMenu.SIDEBAR_WIDTH, ComputerSidebar.HEIGHT
        );
    }
}
