#! python3  # noqa: E265

# standard library
import logging
from collections.abc import Callable
from functools import partial

# PyQGIS
from qgis.core import Qgis, QgsMessageLog, QgsMessageOutput
from qgis.gui import QgsMessageBar
from qgis.PyQt.QtWidgets import QPushButton, QWidget
from qgis.utils import iface

# project package
import oslandia.toolbelt.preferences as plg_prefs_hdlr
from oslandia.__about__ import __title__


# ############################################################################
# ########## Classes ###############
# ##################################


class PlgLogger(logging.Handler):
    """Python logging handler supercharged with QGIS useful methods."""

    @staticmethod
    def log(
        message: str | Exception,
        application: str = __title__,
        log_level: Qgis.MessageLevel = Qgis.MessageLevel.Info,
        push: bool = False,
        duration: int | None = None,
        # widget
        button: bool = False,
        button_text: str | None = None,
        button_more_text: str | None = None,
        button_connect: Callable | None = None,
        # parent
        parent_location: QWidget | None = None,
        nb_parent_max_for_message_bar: int | None = None,
    ) -> None:
        """Send messages to QGIS messages windows and to the user as a message bar. \
        Plugin name is used as title. If debug mode is disabled, only warnings (1) and \
        errors (2) or with push are sent.

        :param message: message to display
        :type message: Union[str, Exception]
        :param application: name of the application sending the message. \
        Defaults to __about__.__title__
        :type application: str, optional
        :param log_level: message level. Possible values: any values of enum \
            `Qgis.MessageLevel`. Defaults to Qgis.MessageLevel.Info
        :type log_level: Qgis.MessageLevel, optional
        :param push: also display the message in the QGIS message bar in addition to \
        the log, defaults to False
        :type push: bool, optional
        :param duration: duration of the message in seconds. If not set, the \
        duration is calculated from the log level: `(log_level + 1) * 3`. seconds. \
        If set to 0, then the message must be manually dismissed by the user. \
        Defaults to None.
        :type duration: int, optional
        :param button: display a button in the message bar. Defaults to False.
        :type button: bool, optional
        :param button_text: text label of the button. Defaults to None.
        :type button_text: str, optional
        :param button_more_text: text to display within the QgsMessageOutput
        :type button_more_text: str, optional
        :param button_connect: function to be called when the button is pressed. \
        If not set, a simple dialog (QgsMessageOutput) is used to dislay the message. \
        Defaults to None.
        :type button_connect: Callable, optional
        :param parent_location: parent location widget. \
        If not set, QGIS canvas message bar is used to push message, \
        otherwise if a QgsMessageBar is available in parent_location it is used instead. \
        Defaults to None.
        :type parent_location: Widget, optional
        :param nb_parent_max_for_message_bar: define maximum recursivity to get parent message bar. Defaults None for no limits
        :type nb_parent_max_for_message_bar: int, optional

        :Example:

        .. code-block:: python

            # using enums from Qgis:
            # Qgis.Info, Qgis.MessageLevel.Warning, Qgis.MessageLevel.Critical, Qgis.MessageLevel.Success, Qgis.MessageLevel.NoLevel
            from qgis.core import Qgis

            log(message="Plugin loaded - INFO", log_level=Qgis.MessageLevel.Info, push=False)
            log(
                message="Something went wrong but it's not blocking",
                log_level=Qgis.MessageLevel.Warning
            )
            log(
                message="Plugin failed to load - CRITICAL",
                log_level=Qgis.MessageLevel(2),
                push=True
            )
        """
        # if not debug mode and not push, let's ignore INFO, SUCCESS and TEST
        debug_mode = plg_prefs_hdlr.PlgOptionsManager.get_plg_settings().debug_mode
        if (
            not debug_mode
            and not push
            and (
                log_level < Qgis.MessageLevel.Warning
                or log_level > Qgis.MessageLevel.Critical
            )
        ):
            return

        # ensure message is a string
        if not isinstance(message, str):
            try:
                message = str(message)
            except Exception as err:
                err_msg = "Log message must be a string, not: {}. Trace: {}".format(
                    type(message), err
                )
                message = err_msg

        # send it to QGIS messages panel
        QgsMessageLog.logMessage(
            message=message, tag=application, notifyUser=push, level=log_level
        )

        # optionally, display message on QGIS Message bar (above the map canvas)
        if push and iface is not None:
            msg_bar = None

            def _find_msg_bar(
                widget: QWidget, nb_parent_max: int | None = None
            ) -> QgsMessageBar | None:
                """Find QgsMessageBar in widget or his parents

                :param widget: widget to search
                :type widget: QWidget
                :param nb_parent_max: define maximum recursivity to get parent message bar.
                    Defaults None for no limits
                :type nb_parent_max: int, optional

                :return: founded QgsMessageBar, None if not found
                :rtype: Optional[QgsMessageBar]
                """
                msg_bar = widget.findChild(QgsMessageBar)
                if isinstance(msg_bar, QgsMessageBar):
                    return msg_bar
                if nb_parent_max is not None:
                    nb_parent_max = nb_parent_max - 1
                if widget.parent() is not None and (
                    nb_parent_max is None or nb_parent_max > 0
                ):
                    return _find_msg_bar(widget.parent(), nb_parent_max)
                return None

            # QGIS or custom dialog
            if parent_location and isinstance(parent_location, QWidget):
                msg_bar = _find_msg_bar(parent_location, nb_parent_max_for_message_bar)

            if not msg_bar:
                msg_bar = iface.messageBar()

            # calc duration
            if duration is None:
                duration = (int(log_level) + 1) * 3

            # create message with/out a widget
            if button:
                # create output message
                notification = iface.messageBar().createMessage(
                    title=application, text=message
                )
                widget_button = QPushButton(button_text or "More...")
                if button_connect:
                    widget_button.clicked.connect(button_connect)
                else:
                    mini_dlg: QgsMessageOutput = QgsMessageOutput.createMessageOutput()
                    mini_dlg.setTitle(application)
                    mini_dlg.setMessage(
                        f"{message}\n{button_more_text}",
                        QgsMessageOutput.MessageType.MessageText,
                    )
                    widget_button.clicked.connect(partial(mini_dlg.showMessage, False))

                notification.layout().addWidget(widget_button)
                msg_bar.pushWidget(
                    widget=notification, level=log_level, duration=duration
                )

            else:
                # send simple message
                msg_bar.pushMessage(
                    title=application,
                    text=message,
                    level=log_level,
                    duration=duration,
                )
