import webbrowser

from PySide6 import QtGui
from PySide6.QtCore import Qt
from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QMainWindow

from config.config_manager import ConfigManager
from src.constants import URLS, NO_STAFF
from controllers.audio_controller import AudioController
from controllers.glow_animator import GlowAnimator
from controllers.window_dragger import WindowDragger
from fake_patch_notes import patch_note


class MainWindow(QMainWindow):
    def __init__(self, bundle_dir):
        super().__init__()

        self.config = ConfigManager()

        # UI
        self.ui = QUiLoader().load(f"{bundle_dir}/ui/mainwindow.ui", self)
        self.setCentralWidget(self.ui.centralWidget())
        self.setWindowFlags(Qt.WindowType.FramelessWindowHint | Qt.WindowType.Window)
        self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)

        if NO_STAFF:
            self.ui.staff_btn.hide()
            self.ui.spacer_substitution.hide()

        self.ui.info_text.setMarkdown(patch_note)

        # Sous-systèmes
        self._audio   = AudioController(self.config, self.ui.audio_volume_adjust, self.ui.mute_btn)
        self._glow    = GlowAnimator(self.ui.connexion_btn)
        self._dragger = WindowDragger(self)

        self._connect_signals()
        self._center_window()
        self.show()

    # ------------------------------------------------------------------
    # Setup
    # ------------------------------------------------------------------

    def _connect_signals(self) -> None:
        self.ui.close_btn.clicked.connect(self.close)
        self.ui.minimize_btn.clicked.connect(self.showMinimized)
        self.ui.connexion_btn.clicked.connect(self._on_connexion)
        self.ui.discord_btn.clicked.connect(self._on_discord)
        self.ui.intranet_btn.clicked.connect(self._on_intranet)

    def _center_window(self) -> None:
        self.adjustSize()
        screen = (
            QtGui.QGuiApplication.screenAt(QtGui.QCursor.pos())
            or QtGui.QGuiApplication.primaryScreen()
        )
        rect = self.frameGeometry()
        rect.moveCenter(screen.availableGeometry().center())
        self.move(rect.topLeft())

    # ------------------------------------------------------------------
    # Button handlers
    # ------------------------------------------------------------------

    def _on_connexion(self) -> None:
        pass  # à implémenter

    @staticmethod
    def _on_discord() -> None:
        webbrowser.open(URLS["discord"])

    def _on_intranet(self) -> None:
        webbrowser.open(URLS["intranet"])
        self._glow.start()

    # ------------------------------------------------------------------
    # Mouse events → délégués au WindowDragger
    # ------------------------------------------------------------------

    def mousePressEvent(self, event: QtGui.QMouseEvent) -> None:
        self._dragger.mouse_press(event)
        super().mousePressEvent(event)

    def mouseMoveEvent(self, event: QtGui.QMouseEvent) -> None:
        self._dragger.mouse_move(event)
        super().mouseMoveEvent(event)

    def mouseReleaseEvent(self, event: QtGui.QMouseEvent) -> None:
        self._dragger.mouse_release(event)
        super().mouseReleaseEvent(event)

    # ------------------------------------------------------------------
    # Close
    # ------------------------------------------------------------------

    def closeEvent(self, event) -> None:
        self.config.save()
        super().closeEvent(event)
