from common import log, queries
from events.common import Lastact

bold = "\x02"
italic = "\x1D"
underline = "\x1F"
reverse = "\x16" 	# swap background and foreground colors ("reverse video")
reset = "\x0F"
blue = "\x0302"
green = "\x0303"
red = "\x0304"
grey = "\x0314"

def process_event(self, connection, event):
    log.info(event)
    
    # Update last act.
    Lastact.update(self, event.source.nick, "join", channel=event.target)
    
    # Add join event to database for statistics.
    queries.create_ifnot_onrecord(self, "channels", event.target)
    queries.create_ifnot_onrecord(self, "users", event.source.nick)
    if not self.db.one("SELECT id FROM joins WHERE LOWER(channel)=LOWER(%s) AND channel_network=%s AND LOWER(\"user\")=LOWER(%s) AND user_network='" + self.network + "'", (event.target, self.network, event.source.nick, )):  # No record yet
        self.db.run("INSERT INTO joins (channel, channel_network, \"user\", user_network, joins) VALUES (%s, %s, %s, %s, 0)", (event.target, self.network, event.source.nick, self.network, ))
    self.db.run("UPDATE joins SET joins = joins + 1 WHERE LOWER(channel)=LOWER(%s) AND channel_network=%s AND LOWER(\"user\")=LOWER(%s) AND user_network=%s", (event.target, self.network, event.source.nick, self.network, ))
    
    if event.source.nick == connection.get_nickname():  # The bot joined a channel.
        connection.who(self.homechannel)    # Get whoreplies for users of homechannel.
        if self.channels[event.target].has_key():
            if event.target in self.channelkeys:    # New key used to join channel.
                self.db.run("UPDATE channels SET key='" + self.channelkeys[event.target] + "' WHERE name='" + event.target + "' AND network='" + self.network + "'")    # Save new key to DB.
                del self.channelkeys[event.target]  # Delete entry.
        if event.target == self.homechannel:    # Home channel.
            connection.who(connection.get_nickname())   # get whoreply to add bot to protectees.
        return  # Do not greet myself.
    
    # Promote owners.
    if event.source.nick in self.channels[self.homechannel].owners():
        connection.mode(event.target, "+vhoa " + event.source.nick + " " + event.source.nick + " " + event.source.nick + " " + event.source.nick)
    
    # Stop if greeting is not wanted.
    joingreeting = self.db.one("SELECT join_greeting FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'")
    stopgreet = self.db.one("SELECT stopgreet FROM joins WHERE channel='" + event.target + "' AND channel_network='" + self.network + "' AND \"user\"='" + event.source.nick + "' AND user_network='" + self.network + "'")
    if not joingreeting or stopgreet:
        return
    
    # Show greeting.
    joins = self.db.one("SELECT joins FROM joins WHERE channel='" + event.target + "' AND channel_network='" + self.network + "' AND \"user\"='" + event.source.nick + "' AND user_network='" + self.network + "'")
    if not self.db.one("SELECT join_greeting FROM channels WHERE name='" + event.target + "' AND network ='" + self.network + "'"): # Do not greet users joining the channel.
        return
    if joins == 1:
        message = "Welcome to " + red + event.target + reset + ", " + red + event.source.nick + reset + ". For a list of command type " + blue + self.cmdchar + "cmd" + reset + "."
    if joins == 3:
        message = "Welcome back in " + red + event.target + reset + ", " + red + event.source.nick + reset + ". To turn of greetings, type " + blue + "!stopgreet" + reset + "."
    if joins == 5:
        if self.channels[event.target].has_key():   # Channel has a password.
            message = "Welcome back again " + red + event.source.nick + reset + ". To automaticly join this channel type " + blue + "/ns ajoin " + reset + "ADD " + red + event.source.nick + " " + event.target + reset + italic + " password"
        else:   # Channel does not have a password.
            message = "Welcome back again " + red + event.source.nick + reset + ". To automaticly join this channel type " + blue + "/ns ajoin " + reset + "ADD " + red + event.source.nick + " " + event.target
    if joins < 100 and str(joins)[-1:] == "0":
        message = red + event.source.nick + reset + " you have joined " + red + event.target + reset + " " + str(joins) + " times!"
    if joins < 1000 and str(joins)[-2:] == "00" or joins < 1000 and str(joins)[-2:] == "50":
        message = "Epic! " + red + event.source.nick + reset + " you have joined " + red + event.target + reset + " " + str(joins) + " times!"
    if joins < 10000 and str(joins)[-2:] == "00":
        message = "AMAZING! " + red + event.source.nick + reset + " you have joined " + red + event.target + reset + " " + str(joins) + " times!"
    try:
        connection.privmsg(event.target, message)
    except:
        pass
