import datetime, pytz
from common import log, queries, font, userstatus

def process_event(self, connection, event):
    log.info(event) # Log to console.

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

    if event.source.nick == connection.get_nickname():  # The bot joined a channel.
        if self.channels[event.target].has_key():   # Passworded channel.
            if event.target in self.channelkeys:    # New key used to join channel.
                log.info('Saving channel key for %s.' %s)
                self.db.run('UPDATE rotbot_channels SET key=%(key)s WHERE id=%(id)s', key=self.channelkeys[event.target], id=channel.id)    # Save new key to DB.
        return   # Stop if the bot joined the channel.

    # Greeting conditions.
    timeout = 10    # Amount of minutes to let pass before greeting again.
    joins = queries.get_user_channel_joins(self, user.id, channel.id)
    chan_lastgreet = queries.get_channel_last_greet(self, channel.id)
    user_lastgreet = queries.get_user_last_greet(self, user.id)
    greet = False
    amount_greet = False
    cet = pytz.timezone('Europe/Amsterdam')

    if not chan_lastgreet:
        chan_lastgreet = cet.localize(datetime.datetime.now()) - datetime.timedelta(minutes=timeout+1)
    if not user_lastgreet:
        user_lastgreet = cet.localize(datetime.datetime.now()) - datetime.timedelta(minutes=timeout+1)
    if chan_lastgreet < cet.localize(datetime.datetime.now()) - datetime.timedelta(minutes=timeout) and user_lastgreet < cet.localize(datetime.datetime.now()) - datetime.timedelta(minutes=timeout):    # User or channel have not had greet message in the last timeout minutes, there is a curse available
        if joins % 200 == 0:    # Has joined 200, or a multiple of 200 times. (Also 0 but the join has already been saved.)
            amount_greet = True
        elif userstatus.atleast_voiced(self, event.source.nick, self.network.home_channel):  # User has atleast voice in home channel.
            curse = queries.random_curse(self)
            if curse:
                greet = True

    # Promotion conditions.
    promote = False
    print(event.source)
    if queries.is_owner(self, event.source):  # Usermask in database.
        promote = True
    elif userstatus.is_owner(self, event.source.nick, self.network.home_channel):   # Owner of homechannel
        promote = True

    # Promote.
    if promote:
        log.info('Promoting %s in %s' % (event.source.nick, event.target))
        connection.mode(event.target, "+aohv %s %s %s %s" % (event.source.nick, event.source.nick, event.source.nick, event.source.nick))

    # Greet.
    if amount_greet:
        log.info('Greeting %s in %s' % (event.source.nick, event.target))
        adjective = queries.random_adjective(self)
        if adjective[0].lower() == 'a':
            adjective = 'n ' + adjective
        else:
            adjective = ' ' + adjective
        if adjective:
            connection.privmsg(event.target, 'Welcome back %s, I have seen you join here a%s %s times.' % (event.source.nick, adjective, joins))
            connection.action(self.network.home_channel, ' has seen %s%s%s join %s%s%s a%s %s%s%s times.' % (font.red, event.source.nick, font.reset, font.red, event.target, font.reset, adjective, font.green, joins, font.reset))
        else:
            connection.privmsg(event.target, 'Welcome back %s, have seen you join here an amazing %s times.' % (event.source.nick, joins))
            Inform.home_channel(self, connection, '%s%s%s has joined %s%s%s %s%s%s times.' % (font.red, event.source.nick, font.reset, font.red, event.target, font.reset, font.green, joins, font.reset))
        queries.update_channel_last_greet(self, channel.id)
        queries.update_user_last_greet(self, user.id)
    elif greet:
        log.info('Greeting %s in %s' % (event.source.nick, event.target))
        connection.privmsg(event.target, 'Welcome back %s, you %s.' % (event.source.nick, curse))
        queries.update_channel_last_greet(self, channel.id)
        queries.update_user_last_greet(self, user.id)
