"""
publishes speial events to the irc
thepolm3
requested by blender man
"""
from pyspades.constants import *

PLAYER_DIED = "{player} died" #given player
PLAYER_KILLED = "{killer} killed {player}" #given player and killer
PLAYER_SUICIDED = "{player} commmited suicide" #given player
PLAYER_ENTERED_COMMAND = "{player} : /{command} {parameters}" #given player, command and parameters
PLAYER_ENTERED = { #given player and parameters
     "squad" : "{player} joined squad {parameters}", 
    "pm" : "{player} sends the following message to {parameters}"
}
PLAYER_CHANGED_WEAPON = "{player} switched to the {weapon}" #given player and weapon
PLAYER_CHANGED_TEAM = "{player} joined the {team} team" #given player and team
PLAYER_HIT = False #given player and hitter
PLAYER_FALLS = False #given player
PLAYER_TAKES_FLAG = False #given player
PLAYER_DROPS_FLAG = False #given player
PLAYER_CAPTURES_FLAG = False #given player
PLAYER_LOGS_IN = False #given player and login

def apply_script(protocol,connection,config):
    class IRCPublishConnection(connection):

        def on_kill(self,by,type,grenade):
            if type==TEAM_CHANGE_KILL and PLAYER_CHANGED_TEAM:
                self.protocol.irc_say(PLAYER_CHANGED_TEAM.format(player = self.name,team=self.team.name))
            elif type==CLASS_CHANGE_KILL and PLAYER_CHANGED_WEAPON:
                self.protocol.irc_say(PLAYER_CHANGED_WEAPON.format(player = self.name,weapon=self.weapon))
            elif not by and PLAYER_DIED:
                self.protocol.irc_say(PLAYER_DIED.format(player = self.name))
            elif self==by and PLAYER_SUICIDED:
                self.protocol.irc_say(PLAYER_SUICIDED.format(player = self.name))
            return connection.on_kill(self,by,type,grenade)

        def on_hit(self,victim,damage,type,b):
            if PLAYER_HIT and damage<self.hp:
                self.protocol.irc_say(PLAYER_HIT.format(player = victim.name,hitter = self.name))
            return connection.on_hit(self,victim,damage,type,b)

        def on_fall(self,a):
            if PLAYER_FALLS:
                self.protocol.irc_say(PLAYER_FALLS.format(player = self.name))
            return connection.on_fall(self,a)

        def on_flag_take(self):
            if PLAYER_TAKES_FLAG:
                self.protocol.irc_say(PLAYER_TAKES_FLAG.format(player = self.name))
            return connection.on_flag_take(self)

        def on_flag_cpature(self):
            if PLAYER_CAPTURES_FLAG:
                self.protocol.irc_say(PLAYER_CAPTURES_FLAG.format(player = self.name))
            return connection.on_flag_capture(self)

        def on_flag_drop(self):
            if PLAYER_DROPS_FLAG:
                self.protocol.irc_say(PLAYER_DROPS_FLAG.format(player = self.name))
            return connection.on_flag_drop(self)

        def on_user_login(self,rank):
            if PLAYER_LOGS_IN:
                self.protocol.irc_say(PLAYER_LOGS_IN.format(player = self.name,login = rank))
            return connection.on_user_login(self,rank)
        
        def on_command(self,command,parameters):
            if PLAYER_ENTERED:
                for com in PLAYER_ENTERED:
                    if com==command.lower():
                        self.protocol.irc_say(PLAYER_ENTERED[com].format(player = self.name,parameters = " ".join(parameters)))
            elif PLAYER_ENTERED_COMMAND:
                self.protocol.irc_say(PLAYER_ENTERED_COMMAND.format(player = self.name,command = command,parameters = " ".join(parameters)))
            return connection.on_command(self,command,parameters)
    class IRCPublishProtocol(protocol):
        pass
    return IRCPublishProtocol,IRCPublishConnection
