"""Assorted text transformations (e.g. rot13)."""

import base64
import codecs

from ircbot.lib import Plugin


class Transform(Plugin):

    """Do a number of text transformations, like rot13."""

    def start(self):
        """Set up the handlers."""

        self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!rot13\s+(.*)$',
                                                         self.handle_rot13, -20)
        self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!base64\s+(encode|decode)\s+(.*)$',
                                                         self.handle_base64, -20)
        self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!upper\s+(.*)$',
                                                         self.handle_upper, -20)
        self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!lower\s+(.*)$',
                                                         self.handle_lower, -20)
        self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!(de)??albhed\s+(.*)$',
                                                         self.handle_albhed, -20)

        super(Transform, self).start()

    def stop(self):
        """Tear down handlers."""

        self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_rot13)
        self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_base64)
        self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_upper)
        self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_lower)
        self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_albhed)

        super(Transform, self).stop()

    def handle_rot13(self, connection, event, match):
        """Apply a rot13 method to the text."""

        text = match.group(1)
        encoder = codecs.getencoder('rot13')
        return self.bot.reply(event, encoder(text)[0])

    def handle_base64(self, connection, event, match):
        """Apply a base64 encoding to the text."""

        direction = match.group(1)
        text = match.group(2)

        if direction == 'encode':
            return self.bot.reply(event, base64.encodebytes(bytes(text, 'utf-8')).decode('utf-8'))
        elif direction == 'decode':
            return self.bot.reply(event, base64.decodebytes(bytes(text, 'utf-8')).decode('utf-8'))

    def handle_upper(self, connection, event, match):
        """Uppercase the text."""

        text = match.group(1)
        return self.bot.reply(event, text.upper())

    def handle_lower(self, connection, event, match):
        """Lowercase the text."""

        text = match.group(1)
        return self.bot.reply(event, text.lower())

    def handle_albhed(self, connection, event, match):
        """Al-Bhedify the text."""

        reverse = match.group(1)
        text = match.group(2)

        cipher = {'A': 'Y', 'B': 'P', 'C': 'L', 'D': 'T', 'E': 'A', 'F': 'V', 'G': 'K', 'H': 'R', 'I': 'E',
                  'J': 'Z', 'K': 'G', 'L': 'M', 'M': 'S', 'N': 'H', 'O': 'U', 'P': 'B', 'Q': 'X', 'R': 'N',
                  'S': 'C', 'T': 'D', 'U': 'I', 'V': 'J', 'W': 'F', 'X': 'Q', 'Y': 'O', 'Z': 'W'}
        decipher = dict([(v, k) for (k, v) in list(cipher.items())])

        if not reverse:
            trans = []
            for i in range(len(text)):
                if text[i] in cipher:
                    trans.append(cipher[text[i]])
                elif text[i].upper() in cipher:
                    trans.append(cipher[text[i].upper()].lower())
                else:
                    trans.append(text[i])

            return self.bot.reply(event, "".join(trans))
        elif reverse == 'de':
            trans = []
            for i in range(len(text)):
                if text[i] in decipher:
                    trans.append(decipher[text[i]])
                elif text[i].upper() in decipher:
                    trans.append(decipher[text[i].upper()].lower())
                else:
                    trans.append(text[i])

            return self.bot.reply(event, "".join(trans))


plugin = Transform