"""Turn text into zalgo text."""
import logging

import irc.client
from zalgo_text import zalgo

from ircbot.lib import Plugin

logger = logging.getLogger(__name__)


class Zalgo(Plugin):
    """Zalgoify text on demand."""

    zalgo_regex = r'^!zalgo\s+(.*)$'

    def start(self):
        """Set up the handlers."""
        logger.debug("%s starting up", __name__)
        self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], self.zalgo_regex,
                                                         self.zalgofy, 0)

        super(Zalgo, self).start()

    def stop(self):
        """Tear down handlers."""
        logger.debug("%s shutting down", __name__)
        self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.zalgofy)

        super(Zalgo, self).stop()

    def zalgofy(self, connection, event, match):
        """Turn text into zalgo text."""
        who = irc.client.NickMask(event.source).nick
        what = match.group(1)
        logger.debug("%s requested zelgo text for %s", who, what)
        zalgoed = zalgo.zalgo().zalgofy(what)
        self.bot.reply(event, f"{zalgoed}")
        return 'NO MORE'


plugin = Zalgo