# -*- coding: utf-8 -*-
# gui/components/model_view/_mv_interaction_events.py
# Тонкий диспетчер событий: делегирует InteractionManager.

from __future__ import annotations

from typing import TYPE_CHECKING

from PySide6.QtCore import Qt, QEvent

if TYPE_CHECKING:  # pragma: no cover
    from gui.components.model_view_widget import ModelViewWidget


class InteractionEventsMixin:
    """Тонкий диспетчер Qt-событий → InteractionManager."""

    def eventFilter(self: "ModelViewWidget", watched, event):
        """Маршрутизатор событий interactor через менеджер сценариев."""
        if not self._is_plotter_interactor_event(watched):
            return super().eventFilter(watched, event)

        etype = event.type()
        mgr = getattr(self, "_interaction_manager", None)
        if mgr is not None:
            result = mgr.dispatch_event_filter(watched, event)
            if result is True:
                return True
            if result is False:
                # По контракту InteractionManager: False = не передавать событие дальше.
                return True

        return super().eventFilter(watched, event)

    def mousePressEvent(self: "ModelViewWidget", event):
        """Делегация mousePressEvent текущему сценарию."""
        mgr = getattr(self, "_interaction_manager", None)
        if mgr is not None and mgr.dispatch_widget_mouse_press(event):
            return
        super().mousePressEvent(event)

    def mouseMoveEvent(self: "ModelViewWidget", event):
        """Делегация mouseMoveEvent текущему сценарию."""
        mgr = getattr(self, "_interaction_manager", None)
        if mgr is not None and mgr.dispatch_widget_mouse_move(event):
            return
        super().mouseMoveEvent(event)

    def mouseReleaseEvent(self: "ModelViewWidget", event):
        """Делегация mouseReleaseEvent текущему сценарию."""
        mgr = getattr(self, "_interaction_manager", None)
        if mgr is not None and mgr.dispatch_widget_mouse_release(event):
            return
        super().mouseReleaseEvent(event)
