import commands.public, commands.admin, commands.games, commands.statistics
from common import log, queries


def process_event(self, connection, event):
    # Let's not log all public messages to the console.

    # Get and update resources.
    channel = queries.create_or_get_and_update_last_event(self, 'channel', 'cm', channel_name=event.target, user_name=event.source.nick)
    user = queries.create_or_get_and_update_last_event(self, 'user', 'cm', channel_name=event.target, user_name=event.source.nick)

    queries.update_message_statistics(self, 'message', channel, user)   # Update message statistics

    commands.public.do_command(self, connection, event, user, channel)
    commands.admin.do_command(self, connection, event, user, channel)
    commands.statistics.do_command(self, connection, event, user, channel)
    commands.games.do_command(self, connection, event, user, channel)

    # # Stop if channelfunction chat if off.
    # if not self.db.one("SELECT chat FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'"):
    #     return
    #
    # if connection.get_nickname().lower() in event.arguments[0].lower() and event.source.nick is not connection.get_nickname(): # Bot's name was mentioned
    #     if event.arguments[0].startswith(self.cmdchar):
    #         return  # Stop if it's a command.
    #     Replyto.name(connection, event)
    #
    # # Character lame.
    # elif event.arguments[0] == len(event.arguments[0]) * event.arguments[0][0]:   # Trigger exclusively same character.
    #
    #     # Do not say KKK.
    #     if event.arguments[0] == "kk":
    #         return
    #
    #     # Stop if lamed recently.
    #     lastlame = self.db.one("SELECT last_lame FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'")
    #     if lastlame and lastlame > datetime.datetime.now() - datetime.timedelta(minutes=2):    # In the last 2 minutes.
    #         return
    #
    #     # Update lastlame.
    #     self.db.run("UPDATE channels SET last_lame='" + str(datetime.datetime.now()) + "' WHERE name='" + event.target + "' AND network='" + self.network + "'")
    #
    #     connection.privmsg(event.target, event.arguments[0] + event.arguments[0][:1])
