"""PDF export utilities."""

from __future__ import annotations

"""PDF exporter module for PDF documents."""

from pathlib import Path  # noqa: E402

from qgis.core import QgsMapRendererCustomPainterJob, QgsMapSettings  # noqa: E402
from qgis.PyQt.QtCore import QMarginsF, QRectF, QSize, QSizeF  # noqa: E402
from qgis.PyQt.QtGui import QPageSize, QPainter, QPdfWriter  # noqa: E402

from sec_interp.logger_config import get_logger  # noqa: E402

from .base_exporter import BaseExporter  # noqa: E402

logger = get_logger(__name__)


class PDFExporter(BaseExporter):
    """Exporter for PDF format."""

    def get_supported_extensions(self) -> list[str]:
        """Get supported PDF extension."""
        return [".pdf"]

    def export(self, output_path: Path, map_settings: QgsMapSettings) -> bool:
        """Export map to PDF.

        Args:
            output_path: Output file path
            map_settings: QgsMapSettings instance configured for rendering

        Returns:
            True if export successful, False otherwise

        """
        try:
            width = self.get_setting("width", 800)
            height = self.get_setting("height", 600)

            # Setup PDF writer
            writer = QPdfWriter(str(output_path))
            writer.setResolution(300)  # Set DPI
            writer.setPageSize(QPageSize(QSizeF(width, height), QPageSize.Unit.Point))
            writer.setPageMargins(QMarginsF(0, 0, 0, 0))

            # Setup painter
            painter = QPainter()
            if not painter.begin(writer):
                logger.error(
                    f"Failed to begin painting for PDF export to {output_path}"
                )
                return False

            try:
                painter.setRenderHint(QPainter.Antialiasing)

                # Update map settings with actual writer device dimensions and DPI
                dev = painter.device()
                map_settings.setOutputSize(QSize(dev.width(), dev.height()))
                map_settings.setOutputDpi(writer.resolution())

                # Render map
                job = QgsMapRendererCustomPainterJob(map_settings, painter)
                job.start()
                job.waitForFinished()

                # Draw legend if available
                show_legend = self.get_setting("show_legend", True)
                legend_renderer = self.get_setting("legend_renderer")
                if legend_renderer and show_legend:
                    legend_renderer.draw_legend(
                        painter, QRectF(0, 0, dev.width(), dev.height())
                    )

            finally:
                painter.end()
        except Exception:
            logger.exception(f"PDF export failed for {output_path}")
            return False
        else:
            return True
