# standard
from __future__ import annotations

from functools import partial
from pathlib import Path

# PyQGIS
from qgis.PyQt import uic
from qgis.PyQt.QtCore import QUrl
from qgis.PyQt.QtWidgets import QWidget

# project
from oslandia.constants import UTM_CONTENT_SETTINGS
from oslandia.toolbelt import PlgLogger, PlgOptionsManager
from oslandia.toolbelt.network_manager import NetworkRequestsManager


class WdgSettingsNotifications(QWidget):
    def __init__(self, parent: QWidget | None = None) -> None:
        """Widget for the notifications section in the settings dialog.

        :param parent: parent widget, defaults to None
        :type parent: QObject, optional
        """
        super().__init__(parent)
        uic.loadUi(Path(__file__).parent / f"{Path(__file__).stem}.ui", self)

        self.log = PlgLogger().log
        self.ntwk_manager = NetworkRequestsManager()
        self.plg_settings = PlgOptionsManager()

        self.initGui()

    def initGui(self) -> None:  # noqa: N802
        """Initialize notification-related UI elements."""
        self.opt_enable_notification.setChecked(False)
        self.sbx_notification_frequency.setEnabled(False)

        def enable_notification_options(enable: bool) -> None:
            """Enable/disable notification options

            :param enable: True to enable options, False to disable
            :type enable: bool
            """
            self.lne_excluded_username_list.setEnabled(enable)
            self.sbx_notification_frequency.setEnabled(enable)

        self.opt_enable_notification.clicked.connect(enable_notification_options)

        # Maximum notification frequency 1h
        self.sbx_notification_frequency.setMaximum(3600)
        self.sbx_notification_frequency.setSingleStep(5)

    def load_settings(self) -> None:
        """Load notification settings into the widget."""
        settings = self.plg_settings.get_plg_settings()
        # Enable smaller notification frequency in debug mode
        self.sbx_notification_frequency.setMinimum(5 if settings.debug_mode else 30)

        self.opt_enable_notification.setChecked(settings.enable_notification)
        self.sbx_notification_frequency.setEnabled(settings.enable_notification)
        self.sbx_notification_frequency.setValue(settings.notification_frequency)
        self.lne_excluded_username_list.setText(settings.excluded_username_list)
        self.lne_excluded_username_list.setEnabled(settings.enable_notification)

        # signal depending on loaded settings
        self.btn_open_gitlab_notifications_settings.clicked.connect(
            partial(
                self.ntwk_manager.open_url,
                QUrl(f"{settings.gitlab_url_instance}-/profile/notifications"),
                UTM_CONTENT_SETTINGS,
            )
        )

    def save_settings(self) -> None:
        """Save notification settings from the widget."""
        settings = self.plg_settings.get_plg_settings()
        settings.enable_notification = self.opt_enable_notification.isChecked()
        settings.notification_frequency = self.sbx_notification_frequency.value()
        settings.excluded_username_list = self.lne_excluded_username_list.text()
        self.plg_settings.save_from_object(settings)
