#from common.networkservices import ChanServ
from common import log, queries, language, userstatus#, do_everything_to, log, font, queries
#from events.common import Aggressiveness, Lastact

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

    # Get and update resources.
    reason = event.arguments[1]
    kicker = queries.create_or_get_and_update_last_event(self, 'user', 'ck', channel_name=event.target, user_name=event.source.nick, event_content=reason, event_subject_name=event.arguments[0])
    channel = queries.create_or_get_and_update_last_event(self, 'channel', 'ck', channel_name=event.target, user_name=event.source.nick, event_content=reason, event_subject_name=event.arguments[0])
    kicked = queries.create_or_get_and_update_last_event(self, 'user','ck', channel_name=event.target, user_name=event.arguments[0], event_content=reason, event_subject_name=event.source.nick)

    # Record kick event in database.
    queries.increment_kick(self, channel.id, kicker.id, kicked.id)

    if event.arguments[0] == connection.get_nickname(): # Bot kicked from channel.
        log.notice('I just got kicked from: %s' % event.target)
        connection.action(self.network.home_channel, 'just got kicked from %s%s %sby %s%s' % (font.red, event.target, font.reset, font.red, event.source.nick))
        connection.privmsg(event.source.nick, 'Sorry you needed to kick me. Bot\'s should serve those who enjoy it, without annoying anyone. Any channelOP can use the !settings command to change my settings.')
        temp_key = queries.create_tempchannelkey(self, channel.id)
        connection.privmsg(event.source.nick, 'Here is a temp 10 minute link for %s%s%s:%s %schannelsettings/%s' % (event.target, font.red, event.target, font.reset, self.webgui['base_ur'], temp_key))
        connection.privmsg(event.source.nick, 'Good bye. If you have any complaints, suggestions or want me back in %s%s%s contact an operator of: %s%s' % (font.red, event.target, font.reset, font.red, self.network.home_channel))
        return

    if self.channel[event.target].has_user(event.arguments[0]): # User resides in homechannel.
        if userstatus.is_owner(self, event.arguments[0], event.target) or queries.is_owner(self, hostmask) or event.arguments[0] == conntection.get_nickname():    # Owner or bot kicked.
            log.info('Kicking %s from %s for kicking %s.' % (event.sourcenick, event.target, event.arguments[0]))
            if reason:
                do_everything_to.kick(connection, event.target, event.source.nick, 'Revenge: %s' % reason)
            else:
                do_everything_to.kick(connection, event.target, event.source.nick, 'Revenge')

        # Mock home channel users in home channel.
        kicks = queries.get_user_channel_kicks(self, kicked.id, channel.id)
        if reason:
            reson_fill = ' for %s%s%s'
        else:
            reason_fill = ''
        message = language.add_adjective_a('mocks %s%s%s for getting kicked out of %s%s%s%s, which makes ' % (font.red, event.arguments[0], font.reset, font.red, event.target, font.reset, reason_fill), ' %s times.' % kicks)
        connection.action(self.network.home_channel, message)


    # # React.
    # print("Reacting to kick")
    # behaviour = self.db.one("SELECT aggressiveness FROM channels WHERE name='" + channel + "' AND network='" + self.network + "'")
    # if behaviour == "passive":    # Passive behaviour.
    #     if kicked == connection.get_nickname() and channel == self.homechannel: # Bot was kicked from it's home channel.
    #         ChanServ.unban(connection, channel, kicked)
    #         ChanServ.akick_del(connection, channel, kicked)
    #         connection.privmsg("ChanServ", "UNBAN " + channel)
    #         do_everything_to.join(self, connection, self.homechannel)
    # elif behaviour == "defense_only":   # Defensive behaviour.
    #     ChanServ.unban(connection, channel, kicked)
    #     ChanServ.akick_del(connection, channel, kicked)
    #     if kicked == connection.get_nickname(): # Bot was kicked.
    #         connection.privmsg("ChanServ", "UNBAN " + channel)
    #         do_everything_to.join(self, connection, channel)
    # elif behaviour == "equal_retalliation":   # Equal retalitory behaviour.
    #     ChanServ.akick_del(connection, channel, kicked)
    #
    #     # Rejoin if bot was kicked.
    #     if kicked == connection.get_nickname():
    #         connection.privmsg("ChanServ", "UNBAN " + channel)
    #         do_everything_to.join(self, connection, self.homechannel)
    #
    #     if event.source.nick == connection.get_nickname() or self.channels[self.homechannel].is_owner(kicker):
    #         return  # Stop if offender is bot or owner.
    #     if not userstatus.atleast_halfop(self, kicked, self.homechannel) or not kicked == connection.get_nickname():
    #         return  # Stop if offended is not atleast halfop and is not the bot itself.
    #     if userstatus.atleast_halfop(self, kicker, self.homechannel) and not self.channels[self.homechannel].is_owner(kicked):
    #         return  # Stop if offender is at least halfop in the home channel and the offended is not owner.
    #
    #     # Kick.
    #     do_everything_to.kick(connection, channel, kicker, Aggressiveness.retalliation_reason(self, connection, kicked, behaviour))
    #
    # # Battlebot behaviour.
    # elif behaviour == "battlebot":
    #     ChanServ.akick_del(connection, channel, kicked)
    #
    #     # Rejoin if bot was kicked.
    #     if kicked == connection.get_nickname():
    #         print("Rejoining " + channel)
    #         do_everything_to.join(self, connection, channel)
    #
    #     if event.source.nick == connection.get_nickname() or self.channels[self.homechannel].is_owner(kicker):
    #         print("Stop if offender is bot or owner")
    #         return  # Stop if offender is bot or owner.
    #     if not userstatus.atleast_halfop(self, kicked, self.homechannel) or not kicked == connection.get_nickname():
    #         print("Stop if offended is not atleast halfop and is not the bot itself")
    #         return  # Stop if offended is not atleast halfop and is not the bot itself.
    #     if userstatus.atleast_halfop(self, kicker, self.homechannel) and not self.channels[self.homechannel].is_owner(kicked):
    #         print("Stop if offender is at least halfop in the home channel and the offended is not owner")
    #         return  # Stop if offender is at least halfop in the home channel and the offended is not owner.
    #     print("Kickbanning")
    #
    #     ChanServ.tempban(connection, channel, kicker, "1h", "Aggression channel function = equal_retalliation: " + kicked)
    #     connection.mode(channel, "+e " + kicked)  # Excempt operator.
    #     ChanServ.akick_add(connection, channel, kicker) # Add kicker to ChanServs autokick.
    #     do_everything_to.bankick(connection, channel, kicker, event.source, "Aggression channel function = equal_retalliation: " + kicked)
