from common import queries, userstatus, font#, queries
#from events.common import Replyto, Lastact, MessageStatistics

def process_event(self, connection, event):
    # Let's not log all actions. log.info(event) # Log to console.

    # Get and update resources.
    user = queries.create_or_get_and_update_last_event(self, 'user', 'ca', channel_name=event.target, user_name=event.source.nick)
    if event.target != connection.get_nickname():   # Channel action.
        channel = queries.create_or_get_and_update_last_event(self, 'channel', 'ca', channel_name=event.target, user_name=event.source.nick)
        queries.update_message_statistics(self, 'action', channel.id, user.id)   # Update message statistics

    # Stop if chat setting is disabled.
    if not queries.get_channel_setting_chat(self, channel.id):
        return

    # Slaps
    if event.arguments[0].lower().startswith('slaps ') and not event.target == connection.get_nickname():   # Channel action that stats with "slaps ".
        takeitlikeaman = False
        takeoffense = False
        protect = False
        joinin = False
        if event.arguments[0].split(" ")[1].lower() == connection.get_nickname().lower(): # Bot slapped
            if userstatus.atleast_voiced(self, event.source.nick, self.network.home_channel):   # Slapper has atleast voice in home channel.
                takeitlikeaman = True
            else:   # Bot slapped by a pleb.
                takeoffense = True

        elif userstatus.atleast_voiced(self, event.arguments[0].split(" ")[1], self.network.home_channel):    # Only protect the worthy.
            if not userstatus.atleast_voiced(self, event.source.nick, self.network.home_channel):   # But not from the worthy
                protect = True
            else: # Worthy slappning the worthy.
                pass

        elif userstatus.atleast_voiced(self, event.source.nick, self.network.home_channel):   # Join the worthy.
            if not userstatus.atleast_voiced(self, event.arguments[0].split(" ")[1], self.network.home_channel):    # But not against teh worthy.
                joinin = True

        if " with a " in event.arguments[0]:    # Slapping with utensil.
            utensil = event.arguments[0].split(" with a ", maxsplit=1)[1]
            if takeitlikeaman == True:
                connection.action(event.target, "takes the " + utensil + " like a robot.")
            elif takeoffense == True:
                connection.action(event.target, "is covered in %s bruises and shame." % utensil)
            elif protect == True:
                connection.action(event.target, "swiftly jumps in front of " + font.red + event.arguments[0].split(" ")[1] + font.reset + " to block the " + utensil)
            elif joinin == True:
                connection.action(event.target, 'takes another %s and joins in slapping %s%s%s profusely.' % (utensil, font.red, event.arguments[0].split(" ")[1], font.reset))
        else:   # Slapping without utensil.
            if takeitlikeaman == True:
                connection.action(event.target, "stands proud, presenting his cheek to %s%s" % (font.red, event.source.nick))
            elif takeoffense == True:
                connection.action(event.target, 'cries.')
            elif protect == True:
                connection.action(event.target, "swiftly jumps in front of " + font.red + event.arguments[0].split(" ")[1] + font.reset + " to block the slap.")
            elif joinin == True:
                connection.action(event.target, 'checks his hand and joins in slapping %s%s%s profusely.' % (font.red, event.arguments[0].split(" ")[1], font.reset))

    # # Respond to own name.
    # elif connection.get_nickname().lower() in event.arguments[0].lower() and event.source.nick is not connection.get_nickname(): # Bot's name was mentioned, not by the bot itself.
    #     Replyto.name(connection, event)
