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

import com.tiedup.remake.client.gui.util.GuiTextureHelper;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

/**
 * Base class for interaction screens (Command Wand, Dialogue, Conversation, Pet Request).
 * Provides common layout functionality and reduces code duplication.
 */
@OnlyIn(Dist.CLIENT)
public abstract class BaseInteractionScreen extends Screen {

    protected int leftPos;
    protected int topPos;
    protected final int panelWidth;
    protected final int panelHeight;

    protected BaseInteractionScreen(
        Component title,
        int panelWidth,
        int panelHeight
    ) {
        super(title);
        this.panelWidth = panelWidth;
        this.panelHeight = panelHeight;
    }

    @Override
    protected void init() {
        super.init();
        // Center the panel on screen
        this.leftPos = (this.width - panelWidth) / 2;
        this.topPos = (this.height - panelHeight) / 2;
    }

    @Override
    public void render(
        GuiGraphics graphics,
        int mouseX,
        int mouseY,
        float partialTick
    ) {
        // Dark background
        this.renderBackground(graphics);

        // Main panel with vanilla-style bevel
        GuiTextureHelper.renderBeveledPanel(
            graphics,
            leftPos,
            topPos,
            panelWidth,
            panelHeight
        );

        // Let subclasses render their content
        renderContent(graphics, mouseX, mouseY, partialTick);

        // Render widgets (buttons) on top
        super.render(graphics, mouseX, mouseY, partialTick);
    }

    /**
     * Render the screen's content. Called after the panel background is drawn.
     *
     * @param graphics Graphics context
     * @param mouseX Mouse X position
     * @param mouseY Mouse Y position
     * @param partialTick Partial tick for interpolation
     */
    protected abstract void renderContent(
        GuiGraphics graphics,
        int mouseX,
        int mouseY,
        float partialTick
    );

    /**
     * Get the X position for content with the given margin.
     *
     * @param margin Margin from the left edge
     * @return Content X position
     */
    protected int getContentX(int margin) {
        return leftPos + margin;
    }

    /**
     * Get the width available for content with the given margin.
     *
     * @param margin Margin on both sides
     * @return Content width
     */
    protected int getContentWidth(int margin) {
        return panelWidth - margin * 2;
    }

    /**
     * Render a centered title at the given Y position.
     *
     * @param graphics Graphics context
     * @param title Title component
     * @param y Y position
     * @param color Text color
     */
    protected void renderTitle(
        GuiGraphics graphics,
        Component title,
        int y,
        int color
    ) {
        graphics.drawCenteredString(
            this.font,
            title,
            leftPos + panelWidth / 2,
            y,
            color
        );
    }

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