"""
Print Settings Dialog for pyPhotoAlbum

Project-level bleed and safe-area configuration applied uniformly to all pages.
"""

from typing import Any, Dict
from PyQt6.QtWidgets import (
    QDialog,
    QVBoxLayout,
    QHBoxLayout,
    QLabel,
    QDoubleSpinBox,
    QPushButton,
    QGroupBox,
)
from pyPhotoAlbum.project import Project


class PrintSettingsDialog(QDialog):
    """Dialog for configuring project-wide print settings (bleed and safe area)."""

    def __init__(self, parent, project: Project):
        super().__init__(parent)
        self.project = project
        self._setup_ui()

    def _setup_ui(self):
        self.setWindowTitle("Print Settings")
        self.setMinimumWidth(340)

        layout = QVBoxLayout()

        group = QGroupBox("Bleed && Safe Area (applied to all pages)")
        group_layout = QVBoxLayout()

        # Bleed
        bleed_layout = QHBoxLayout()
        bleed_layout.addWidget(QLabel("Bleed Margin:"))
        self.bleed_spinbox = QDoubleSpinBox()
        self.bleed_spinbox.setRange(0.0, 20.0)
        self.bleed_spinbox.setSingleStep(0.5)
        self.bleed_spinbox.setDecimals(1)
        self.bleed_spinbox.setSuffix(" mm")
        self.bleed_spinbox.setValue(self.project.page_bleed_mm)
        self.bleed_spinbox.setToolTip(
            "Extra white space added around each page in the exported PDF.\n"
            "The printer cuts here — 3 mm is standard."
        )
        bleed_layout.addWidget(self.bleed_spinbox)
        group_layout.addLayout(bleed_layout)

        # Safe area
        safe_layout = QHBoxLayout()
        safe_layout.addWidget(QLabel("Safe Area:"))
        self.safe_spinbox = QDoubleSpinBox()
        self.safe_spinbox.setRange(0.0, 50.0)
        self.safe_spinbox.setSingleStep(0.5)
        self.safe_spinbox.setDecimals(1)
        self.safe_spinbox.setSuffix(" mm")
        self.safe_spinbox.setValue(self.project.page_safe_area_mm)
        self.safe_spinbox.setToolTip(
            "Keep text and important content inside this distance from the cut/trim line.\n"
            "Shown as a red guide in the editor."
        )
        safe_layout.addWidget(self.safe_spinbox)
        group_layout.addLayout(safe_layout)

        group.setLayout(group_layout)
        layout.addWidget(group)

        # Buttons
        btn_layout = QHBoxLayout()
        cancel_btn = QPushButton("Cancel")
        cancel_btn.clicked.connect(self.reject)
        ok_btn = QPushButton("OK")
        ok_btn.clicked.connect(self.accept)
        ok_btn.setDefault(True)
        btn_layout.addStretch()
        btn_layout.addWidget(cancel_btn)
        btn_layout.addWidget(ok_btn)
        layout.addLayout(btn_layout)

        self.setLayout(layout)

    def get_values(self) -> Dict[str, Any]:
        return {
            "page_bleed_mm": self.bleed_spinbox.value(),
            "page_safe_area_mm": self.safe_spinbox.value(),
        }
