# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
#
# Copyright (c) 2008 - 2019 by Wilbert Berendsen
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
# See http://www.gnu.org/licenses/ for more information.

"""
Customization classes for the qpageview.View.

This is used throughout Frescobaldi, to obey color settings etc.

"""

import itertools
import os
import platform

from PyQt6.QtCore import pyqtSignal, QMargins, QSettings, Qt
from PyQt6.QtWidgets import QMessageBox
from PyQt6.QtPrintSupport import QPrinter, QPrintDialog

import app
import icons
import textformats
import qpageview
import qpageview.view
import qpageview.layout
import qpageview.printing
import qpageview.magnifier
import qpageview.viewactions
import qpageview.widgetoverlay


class Magnifier(qpageview.magnifier.Magnifier):
    """A magnifier that reads and stores its settings."""
    def __init__(self):
        super().__init__()
        app.settingsChanged.connect(self.readSettings)
        self.readSettings()

    def readSettings(self):
        s = QSettings()
        s.beginGroup("musicview/magnifier")
        size = s.value("size", 350, int)
        if self.MIN_SIZE <= size <= self.MAX_SIZE:
            self.resize(size, size)
        scale = s.value("scalef", 3.0, float)
        super().setScale(scale)

    def setScale(self, scale):
        """Reimplemented to save the scale."""
        super().setScale(scale)
        QSettings().setValue("musicview/magnifier/scalef", scale)

    def resizeEvent(self, ev):
        """Reimplemented to save the size."""
        super().resizeEvent(ev)
        QSettings().setValue("musicview/magnifier/size", ev.size().width())


class PagedView(qpageview.widgetoverlay.WidgetOverlayViewMixin, qpageview.View):
    """A View based on qpageview.View.

    This View has additional features and customisation needed in Frescobaldi.

    """

    MAX_ZOOM = 8.0  # see issue #2035; this should be plenty for our needs

    def __init__(self, parent=None):
        super().__init__(parent)
        self._printer = None
        self.documentPropertyStore = qpageview.view.DocumentPropertyStore()
        self.setMagnifier(Magnifier())
        app.settingsChanged.connect(self.readSettings)
        self.readSettings()

    def readSettings(self):
        # strict paging with pageup/pagedown
        self.strictPagingEnabled = QSettings().value("musicview/strict_paging", False, bool)

        # shadow/margin
        shadow = QSettings().value("musicview/shadow", True, bool)
        oldshadow, self.dropShadowEnabled = self.dropShadowEnabled, shadow
        self.pageLayout().setMargins(QMargins(6, 6, 6, 6) if shadow else QMargins(1, 1, 1, 1))
        changed = shadow != oldshadow

        # kinetic scrolling
        kinetic = QSettings().value("musicview/kinetic_scrolling", True, bool)
        self.kineticPagingEnabled = kinetic
        self.kineticScrollingEnabled = kinetic

        # scrollbar visibility
        scrollbars = QSettings().value("musicview/show_scrollbars", True, bool)
        policy = Qt.ScrollBarPolicy.ScrollBarAsNeeded if scrollbars else Qt.ScrollBarPolicy.ScrollBarAlwaysOff
        oldpolicy = self.horizontalScrollBarPolicy()
        self.setHorizontalScrollBarPolicy(policy)
        self.setVerticalScrollBarPolicy(policy)
        if policy != oldpolicy:
            changed = True

        if not self.pageCount():
            return

        # if margin or scrollbar visibility was changed, relayout
        if changed:
            if self._viewMode:
                self.fitPageLayout()
            self.updatePageLayout()


        # set certain preferences to the existing renderers
        renderers = {page.renderer for page in self.pageLayout() if page.renderer}
        pages = {page for page in self.pageLayout() if not page.renderer}
        changed = False

        # paper color; change the existing renderer
        paperColor = textformats.formatData('editor').baseColors['paper']
        # if a page has no renderer, adjust the page itself :-)
        for rp in itertools.chain(renderers, pages):
            if rp.paperColor != paperColor:
                changed = True
                rp.paperColor = paperColor

        if changed:
            self.rerender()

    def loadPdf(self, filename, renderer=None):
        """Reimplemented to use a customized renderer by default."""
        super().loadPdf(filename, renderer or getRenderer("pdf"))

    def loadSvgs(self, filenames, renderer=None):
        """Reimplemented to use a customized renderer by default."""
        super().loadSvgs(filenames, renderer or getRenderer("svg"))

    def loadImages(self, filenames, renderer=None):
        """Reimplemented to use a customized renderer by default."""
        super().loadImages(filenames, renderer or getRenderer("image"))

    def print(self, printer=None, pageNumbers=None, showDialog=True):
        """Print the contents of the View."""
        if printer is None:
            if self._printer is None:
                self._printer = QPrinter()
            printer = self._printer
        printer.setCopyCount(1) # prevent embarrassing situations :-)
        if self.document() and self.document().filename():
            filename = os.path.basename(self.document().filename())
        else:
            filename = ""
        printer.setDocName(filename)
        if showDialog:
            dlg = QPrintDialog(printer, self)
            if filename:
                title = app.caption(_("Print {filename}").format(filename=filename))
            else:
                title = app.caption(_("Print"))
            dlg.setWindowTitle(title)
            dlg.setMinMax(1, self.pageCount())
            if not dlg.exec():
                return  # cancelled
        s = QSettings()
        printer.setResolution(s.value("printing/dpi", 300, int))

        job = super().print(printer, pageNumbers, False)
        if job:
            progress = PrintProgressDialog(job, self)
            progress.setWindowTitle(title)
            progress.setLabelText(_("Preparing to print..."))
            progress.show()


class PrintProgressDialog(qpageview.printing.PrintProgressDialog):
    def showProgress(self, page, num, total):
        """Called by the job when printing a page."""
        self.setValue(num)
        self.setLabelText(_("Printing page {page} ({num} of {total})...").format(
                page=page, num=num, total=total))

    def showErrorMessage(self):
        """Reimplemented to show a translated error message."""
        QMessageBox.warning(self.parent(), _("Printing Error"),
                    _("Could not send the document to the printer."))


class ViewActions(qpageview.viewactions.ViewActions):
    """View actions with translated texts and icons."""
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        app.translateUI(self)

    def createActions(self):
        """Creates the actions; called by __init__()."""
        super().createActions()
        # limit the maximum zoom factor to what our PagedView allows
        self.zoomer._zoomFactors = tuple(filter(
            lambda x: x <= PagedView.MAX_ZOOM, self.zoomer._zoomFactors))

    def translateUI(self):
        """Translate our actions correctly."""
        self.setActionTexts(_)
        self.pager.setDisplayFormat(_("{num} of {total}"))

    def setActionIcons(self):
        """Set icons to our actions."""
        self.print.setIcon(icons.get('document-print'))
        self.zoom_in.setIcon(icons.get('zoom-in'))
        self.zoom_out.setIcon(icons.get('zoom-out'))
        self.zoom_original.setIcon(icons.get('zoom-original'))
        self.fit_width.setIcon(icons.get('zoom-fit-width'))
        self.fit_height.setIcon(icons.get('zoom-fit-height'))
        self.fit_both.setIcon(icons.get('zoom-fit-best'))
        self.rotate_left.setIcon(icons.get('rotate-left'))
        self.rotate_right.setIcon(icons.get('rotate-right'))
        self.next_page.setIcon(icons.get('go-next'))
        self.previous_page.setIcon(icons.get('go-previous'))
        self.magnifier.setIcon(icons.get('zoom-magnifier'))
        self.reload.setIcon(icons.get('view-refresh'))


def getRenderer(rendertype):
    """Factory to create a renderer with the paper color adjusted to the prefs.

    Currently, the rendertype can be "pdf", "svg", "image" or "diff".

    For the PDF type, the render backends are also read from the prefs.
    For image types, the paper color is not used. For diff types, the paper-
    color is not set.

    """
    if rendertype == "pdf":
        import qpageview.pdf
        r = qpageview.pdf.PdfRenderer()
    elif rendertype == "svg":
        import qpageview.svg
        r = qpageview.svg.SvgRenderer()
    elif rendertype == "image":
        import qpageview.image
        r = qpageview.image.ImageRenderer()
    elif rendertype == "diff":
        import qpageview.diff
        return qpageview.diff.DiffRenderer()
    else:
        raise ValueError("unknown render type")
    r.paperColor = textformats.formatData('editor').baseColors['paper']
    return r


def loadPdf(filename):
    """Like qpageview.loadPdf(), but uses a preconfigured renderer."""
    return qpageview.loadPdf(filename, getRenderer("pdf"))


def loadSvgs(filenames):
    """Like qpageview.loadSvgs(), but uses a preconfigured renderer."""
    return qpageview.loadSvgs(filenames, getRenderer("svg"))


def loadImages(filenames):
    """Like qpageview.loadImages(), but uses a preconfigured renderer."""
    return qpageview.loadImages(filenames, getRenderer("image"))
