from typing import TYPE_CHECKING

from slixmpp import Message
from slixmpp.xmlstream import StanzaBase

from ..util import DispatcherMixin, exceptions_to_xmpp_errors

if TYPE_CHECKING:
    from slidge.util.types import AnyGateway


class ChatStateMixin(DispatcherMixin):
    __slots__: list[str] = []

    def __init__(self, xmpp: "AnyGateway") -> None:
        super().__init__(xmpp)
        xmpp.add_event_handler("chatstate", self.on_chatstate)

    @exceptions_to_xmpp_errors
    async def on_chatstate(self, msg: StanzaBase) -> None:
        assert isinstance(msg, Message)
        if msg["body"]:
            # if there is a body, it's handled in on_legacy_message()
            return
        recipient, thread = await self._get_recipient_and_thread(msg)
        await recipient.on_chat_state(msg["chat_state"], thread)
