"""Window divider utility for creating multi-pane layouts."""
from PySide6.QtCore import QRect


class WindowDivider:
    """Divides a screen geometry into multiple rectangular areas with spacing."""

    def __init__(self, screen_geometry: QRect, spacing: int = 10):
        """
        Initialize WindowDivider.

        Args:
            screen_geometry: The available screen geometry to divide
            spacing: Spacing between windows in pixels
        """
        self.screen_geometry = screen_geometry
        self.spacing = spacing
        self.x = screen_geometry.x()
        self.y = screen_geometry.y()
        self.width = screen_geometry.width()
        self.height = screen_geometry.height()

    def get_subrect(self, rows: int, cols: int, index: int) -> QRect:
        """
        Get a subrectangle from a grid layout.

        Args:
            rows: Number of rows in the grid
            cols: Number of columns in the grid
            index: 1-based index of the desired area (1 to rows*cols)

        Returns:
            QRect representing the requested area
        """
        # Convert 1-based index to 0-based
        idx = index - 1

        # Calculate row and column from index
        row = idx // cols
        col = idx % cols

        # Calculate dimensions with spacing
        total_h_spacing = self.spacing * (cols - 1)
        total_v_spacing = self.spacing * (rows - 1)

        cell_width = (self.width - total_h_spacing) // cols
        cell_height = (self.height - total_v_spacing) // rows

        # Calculate position
        x = self.x + col * (cell_width + self.spacing)
        y = self.y + row * (cell_height + self.spacing)

        return QRect(x, y, cell_width, cell_height)
