"""Widget for discussion display"""

# standard
from enum import Enum
from pathlib import Path

# PyQGIS
from qgis.PyQt import QtCore, uic
from qgis.PyQt.QtGui import QIcon, QPixmap
from qgis.PyQt.QtWidgets import QWidget

# project
from oslandia.gui.helpdesk.wdg_note import NoteWidget
from oslandia.helpdesk.discussion import Discussion, Note
from oslandia.toolbelt.qt_utils import localize_qdatetime, to_qdatetime


class DiscussionWidget(QWidget):
    """QWidget to display discussion

    :param parent: dialog parent, defaults to None
    :type parent: Optional[QWidget], optional
    """

    # Signal to indicate changes in discussion displayed
    qsig_discussion_changed = QtCore.pyqtSignal()

    # Signal to indicate that the discussion was updated
    qsig_discussion_updated = QtCore.pyqtSignal()

    class DisplayMode(Enum):
        READ = 1
        UPDATE = 2

    def __init__(self, parent: QWidget | None = None):
        super().__init__(parent)
        uic.loadUi(Path(__file__).parent / f"{Path(__file__).stem}.ui", self)
        self.discussion = None

        self.note_widget.set_display_mode(NoteWidget.DisplayMode.READ)
        # signals
        self.note_widget.qsig_note_changed.connect(self.qsig_discussion_changed)
        self.note_widget.qsig_note_updated.connect(self.qsig_discussion_updated)
        self.note_widget.qsig_reply_asked.connect(
            lambda: self.reply_widget.setVisible(True)
        )
        self.reply_widget.qsig_note_reply_added.connect(self._add_reply)
        self.reply_widget.qsig_note_reply_canceled.connect(
            lambda: self.reply_widget.setVisible(False)
        )

        # Don't display reply by default
        self.frm_reply.setVisible(False)

        # Don't display reply description (no reply by default)
        self.frm_reply_description.setVisible(False)

        # Define icon for button for collapse
        icon = QIcon()
        icon.addPixmap(
            QPixmap(":images/themes/default/mIconExpand.svg"),
            mode=QIcon.Mode.Normal,
            state=QIcon.State.Off,
        )
        icon.addPixmap(
            QPixmap(":images/themes/default/mIconCollapse.svg"),
            mode=QIcon.Mode.Normal,
            state=QIcon.State.On,
        )
        self.btn_reply_collapse.setIcon(icon)
        self.btn_reply_collapse.setChecked(False)
        self.btn_reply_collapse.clicked.connect(self.frm_reply.setVisible)

        self.reply_widget.setVisible(False)

    def set_discussion(self, discussion: Discussion) -> None:
        """Set note displayed

        :param note: note
        :type note: Note
        """
        self.discussion = discussion
        self.reply_widget.set_discussion(self.discussion)
        first_note = True
        for note in discussion.notes:
            if not note.system:
                # First note is a comment
                if first_note:
                    self.note_widget.set_note(note)
                    first_note = False
                # Other notes are reply
                else:
                    self._add_reply(note)

    def _add_reply(self, reply: Note) -> None:
        """Add a reply in discussion

        :param reply: reply
        :type reply: Note
        """
        reply_widget = NoteWidget(self)
        reply_widget.set_note(reply)
        reply_widget.set_display_mode(NoteWidget.DisplayMode.READ)
        reply_widget.qsig_note_changed.connect(self.qsig_discussion_changed)
        reply_widget.qsig_note_updated.connect(self.qsig_discussion_updated)
        reply_widget.qsig_reply_asked.connect(
            lambda: self.reply_widget.setVisible(True)
        )
        self.lyt_replies.addWidget(reply_widget)
        nb_replies = self.lyt_replies.count()
        grp_text = self.tr(
            "{} reply Last reply by {} - {}".format(
                nb_replies,
                reply.author.name,
                localize_qdatetime(to_qdatetime(reply.created_at)),
            )
        )

        self.lbl_reply_comment.setText(grp_text)
        self.lbl_reply_comment.setToolTip(
            localize_qdatetime(to_qdatetime(reply.created_at), date_format="long")
        )
        self.reply_widget.setVisible(False)
        self.frm_reply_description.setVisible(True)
