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

import com.tiedup.remake.client.gui.util.GuiColors;
import com.tiedup.remake.client.gui.util.GuiLayoutConstants;
import com.tiedup.remake.client.gui.util.GuiRenderUtil;
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 abstract class for TiedUp! GUI screens.
 * Handles responsive layout, background rendering, and common utilities.
 *
 * Refactored for cleaner architecture.
 */
@OnlyIn(Dist.CLIENT)
public abstract class BaseScreen extends Screen {

    // Content area bounds
    protected int leftPos;
    protected int topPos;
    protected int imageWidth;
    protected int imageHeight;

    protected BaseScreen(Component title) {
        super(title);
    }

    /**
     * Defines the preferred percentage width of the screen (0.0 to 1.0)
     * or a fixed pixel width if context requires.
     * Default implementation uses a responsive approach.
     */
    protected int getPreferredWidth() {
        // Default: 60% of screen, min 300px, max 450px
        return GuiLayoutConstants.getResponsiveWidth(
            this.width,
            0.6f,
            300,
            450
        );
    }

    /**
     * Defines the preferred percentage height of the screen.
     */
    protected int getPreferredHeight() {
        // Default: 70% of screen, min 220px, max 400px
        return GuiLayoutConstants.getResponsiveHeight(
            this.height,
            0.7f,
            220,
            400
        );
    }

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

        // Calculate dimensions
        this.imageWidth = getPreferredWidth();
        this.imageHeight = getPreferredHeight();

        // Center the panel
        this.leftPos = (this.width - this.imageWidth) / 2;
        this.topPos = (this.height - this.imageHeight) / 2;
    }

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

        // Draw Main Panel Background
        graphics.fill(
            leftPos,
            topPos,
            leftPos + imageWidth,
            topPos + imageHeight,
            GuiColors.BG_MEDIUM
        );

        // Draw Borders
        GuiRenderUtil.drawBorder(
            graphics,
            leftPos,
            topPos,
            imageWidth,
            imageHeight,
            GuiColors.BORDER_LIGHT
        );

        // Title
        graphics.drawCenteredString(
            this.font,
            this.title,
            this.width / 2,
            this.topPos + GuiLayoutConstants.MARGIN_M,
            GuiColors.TEXT_WHITE
        );

        // Template method: subclasses render custom content between background and widgets
        renderContent(graphics, mouseX, mouseY, partialTick);

        super.render(graphics, mouseX, mouseY, partialTick);
    }

    /**
     * Template method for subclasses to render custom content between the
     * background/title and the widgets. Called after background, panel fill,
     * border, and title are drawn, but before {@code super.render()} draws
     * widgets and tooltips.
     *
     * <p>Default implementation is a no-op so existing subclasses that
     * override {@code render()} directly are unaffected.</p>
     *
     * <p><b>Important:</b> Subclasses should use EITHER this method OR a
     * {@code render()} override, not both — combining them would render
     * custom content twice.</p>
     */
    protected void renderContent(
        GuiGraphics graphics,
        int mouseX,
        int mouseY,
        float partialTick
    ) {
        // No-op by default — subclasses override to render custom content
    }

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

    /**
     * Truncate text with ellipsis if too long.
     */
    protected String truncateText(String text, int maxWidth) {
        if (this.font.width(text) <= maxWidth) {
            return text;
        }
        String ellipsis = "...";
        int ellipsisWidth = this.font.width(ellipsis);
        int availableWidth = maxWidth - ellipsisWidth;
        if (availableWidth <= 0) {
            return ellipsis;
        }
        // Basic truncation
        return this.font.plainSubstrByWidth(text, availableWidth) + ellipsis;
    }
}
