# -*- coding: utf-8 -*-

"""Helper component to show messages from the Navigator."""

__all__ = ['NotificationsComponent']

import typing

from anaconda_navigator.utils import notifications
from anaconda_navigator.widgets import dialogs
from . import common

if typing.TYPE_CHECKING:
    from qtpy import QtWidgets
    from anaconda_navigator.widgets import main_window


class NotificationsComponent(common.Component):
    """Component for notification management."""

    __alias__ = 'notifications'

    def __init__(self, parent: 'main_window.MainWindow') -> None:
        """Initialize new :class:`NotificationsComponent` instance."""
        super().__init__(parent=parent)
        self.__queue: typing.Final[notifications.NotificationQueue] = notifications.NOTIFICATIONS_QUEUE
        self.__ready: bool = False

    def __message_box(self, *args: typing.Optional['QtWidgets.QDialog']) -> None:
        """
        Show all pending message boxes.

        Additional dialogs might be provided as `args`. If application is not ready to show message boxes - they will
        be postponed until application will be initialized.
        """
        self.__queue.queue.extend(args)

        if not self.__ready:
            return

        while self.__queue.queue:
            dialog: 'QtWidgets.QDialog' = self.__queue.pop(0)
            dialog.exec_()

    def information_message_box(self, text: str = '', title: str = '') -> None:
        """Show a dialog with information message."""
        dialog: 'QtWidgets.QDialog' = dialogs.MessageBoxInformation(text=text, title=title, parent=self.main_window)
        dialog.setMinimumWidth(480)
        self.__message_box(dialog)

    def setup(self, worker: typing.Any, output: typing.Any, error: str, initial: bool) -> None:
        """Perform component configuration from `conda_data`."""
        self.__ready = True
        self.__queue.sig_item_added.connect(self.__message_box)
        self.__message_box()
