package cz.jzitnik.tui;

/**
 * Utility class that calculates the visible portion of the game world
 * based on player position and terminal dimensions.
 */
public class ScreenMovingCalculationProvider {
    /**
     * Calculates the boundaries of the visible world area (in block coordinates)
     * that should be rendered based on the terminal size and the player's position.
     *
     * @param x              Player's X coordinate in the world.
     * @param y              Player's Y coordinate in the world.
     * @param terminalHeight Height of the terminal in characters.
     * @param terminalWidth  Width of the terminal in characters.
     * @param worldX         Width of the game world in blocks.
     * @param worldY         Height of the game world in blocks.
     * @return An array of four integers: [startX, endX, startY, endY],
     *         representing the visible boundaries of the world.
     */
    public static int[] calculate(int x, int y, int terminalHeight, int terminalWidth, int worldX, int worldY) {
        int spriteWidth = 50;
        int spriteHeight = 25;

        int viewXRadius = (terminalWidth / 2) / spriteWidth;
        int viewYRadius = ((terminalHeight - 30 - 9) / 2) / spriteHeight;

        // Ensure at least one sprite is visible
        viewXRadius = Math.max(viewXRadius, 1);
        int viewUpRadius = Math.max(viewYRadius, 1);
        int viewDownRadius = Math.max(viewYRadius, 1);

        // Calculate visible area boundaries
        int startX = Math.max(0, x - viewXRadius);
        int endX = Math.min(worldX, x + viewXRadius + 1);
        int startY = Math.max(0, y - viewUpRadius);
        int endY = Math.min(worldY, y + viewDownRadius + 1);

        return new int[] { startX, endX, startY, endY };
    }
}
