# sumaexcel/page.py
from typing import Optional, Dict, Any, Union
from openpyxl.worksheet.worksheet import Worksheet
from openpyxl.worksheet.page import PageMargins, PrintPageSetup

# sumaexcel/page.py (continued)

class PageManager:
    """Handle page setup and header/footer settings""" 

    def __init__(self, worksheet: Worksheet):
        self.worksheet = worksheet

    def set_page_setup(
        self,
        orientation: str = 'portrait',
        paper_size: int = 9,  # A4
        fit_to_height: Optional[int] = None,
        fit_to_width: Optional[int] = None,
        scale: Optional[int] = None
    ) -> None:
        """Configure page setup

        Args:
            orientation: 'portrait' or 'landscape'
            paper_size: paper size (e.g., 9 for A4)
            fit_to_height: number of pages tall
            fit_to_width: number of pages wide
            scale: zoom scale (1-400)
        """ 
        setup = PrintPageSetup(
            orientation=orientation,
            paperSize=paper_size,
            scale=scale,
            fitToHeight=fit_to_height,
            fitToWidth=fit_to_width
        )
        self.worksheet.page_setup = setup

    def set_margins(
        self,
        left: float = 0.7,
        right: float = 0.7,
        top: float = 0.75,
        bottom: float = 0.75,
        header: float = 0.3,
        footer: float = 0.3
    ) -> None:
        """Set page margins in inches""" 
        margins = PageMargins(
            left=left,
            right=right,
            top=top,
            bottom=bottom,
            header=header,
            footer=footer
        )
        self.worksheet.page_margins = margins

    def set_header_footer(
        self,
        odd_header: Optional[str] = None,
        odd_footer: Optional[str] = None,
        even_header: Optional[str] = None,
        even_footer: Optional[str] = None,
        first_header: Optional[str] = None,
        first_footer: Optional[str] = None,
        different_first: bool = False,
        different_odd_even: bool = False
    ) -> None:
        """Set headers and footers

        Format codes:
        - &P: Page number
        - &N: Total pages
        - &D: Date
        - &T: Time
        - &[Tab]: Sheet name
        - &[Path]: File path
        - &[File]: File name
        - &[Tab]: Worksheet name
        """ 
        self.worksheet.oddHeader.left = odd_header or "" 
        self.worksheet.oddFooter.left = odd_footer or "" 

        if different_odd_even:
            self.worksheet.evenHeader.left = even_header or "" 
            self.worksheet.evenFooter.left = even_footer or "" 

        if different_first:
            self.worksheet.firstHeader.left = first_header or "" 
            self.worksheet.firstFooter.left = first_footer or "" 

        self.worksheet.differentFirst = different_first
        self.worksheet.differentOddEven = different_odd_even

    def set_print_area(self, range_string: str) -> None:
        """Set print area

        Args:
            range_string: Cell range in A1 notation (e.g., 'A1:H42')
        """ 
        self.worksheet.print_area = range_string

    def set_print_title_rows(self, rows: str) -> None:
        """Set rows to repeat at top of each page

        Args:
            rows: Row range (e.g., '1:3')
        """ 
        self.worksheet.print_title_rows = rows

    def set_print_title_columns(self, cols: str) -> None:
        """Set columns to repeat at left of each page

        Args:
            cols: Column range (e.g., 'A:B')
        """ 
        self.worksheet.print_title_cols = cols

    def set_print_options(
        self,
        grid_lines: bool = False,
        horizontal_centered: bool = False,
        vertical_centered: bool = False,
        headers: bool = False
    ) -> None:
        """Set print options""" 
        self.worksheet.print_gridlines = grid_lines
        self.worksheet.print_options.horizontalCentered = horizontal_centered
        self.worksheet.print_options.verticalCentered = vertical_centered
        self.worksheet.print_options.headers = headers

class PaperSizes:
    """Standard paper size constants""" 
    LETTER = 1
    LETTER_SMALL = 2
    TABLOID = 3
    LEDGER = 4
    LEGAL = 5
    STATEMENT = 6
    EXECUTIVE = 7
    A3 = 8
    A4 = 9
    A4_SMALL = 10
    A5 = 11
    B4 = 12
    B5 = 13
