from commands.common import CommandHelpers as CH
from common import queries, font

def do_command(self, connection, event, user, channel):
    cmdtype, trigger, command, replyto = CH.disect_command(self, event)

    # Do nothing if it's not a type of command.
    if not cmdtype:
        return

    # Do nothing if there is no command.
    if not command:
        return

    # The first word of the command sting with arguments, is just the command without arguments.
    try:
        one = command.split()[0]    # Get raw command.
    except:
        return

    # Do noting if the chat channel setting is off and it's a channel message.
    if event.target != connection.get_nickname():   # Command issued to channel.
        if not queries.get_channel_setting_chat:   # Games are turned off in channel settigns.
            return  # Do nothing.

    if command == 'cmd' or command == 'cmds' or command == 'commands':
        if cmdtype == 'cmd':
            connection.privmsg(replyto, '%sChat: %s %s' % (font.grey, CH.ccc(self, "curse")[:-2]))

    elif one == 'curse':
        curse = queries.random_curse(self)
        if command.split() == 1:    # Command without arguments
            if cmdtype == 'help':
                if curse:
                    connection.privmsg(replyto, 'Add, lookup, or display a random %s curse word. The word argument is optional, to add a new word%s %s%s add%s %sword%s, or an adjective: %s%s%s adj' % (curse, font.blue, self.network.command_character, one, font.reset, font.italic, font.reset, font.blue, self.network.command_character, one))
                else:
                    connection.privmsg(replyto, 'Add, lookup, or display random curse word. The word argument is optional, to add a new word%s %s%s add%s %sword%s, or an adjective: %s%s%s add adj' % (font.blue, self.network.command_character, one, font.reset, font.italic, font.reset, font.blue, self.network.command_character, one))
                connection.privmsg(replyto, '%sUsage:%s %s%s%s %sword' % (font.grey, font.blue, self.network.command_character, one, font.reset, font.italic))
            else:   # Actual command without arguments.
                curseword = queries.random_curseword(self)
                connection.privmsg(replyto, curseword)
        elif command.split() == 2:   # Command with one argument.
            if cmdtype == 'help':
                if swear:
                    connection.privmsg(replyto, 'Look up a %s swear word: %s%s' % (swear, font.italic, command.split()[1]))
                else:
                    connection.privmsg(replyto, 'Look up swear word: %s%s' % (font.italic, command.split()[1]))
            else:   # Actual command with one argument.
                curseword = quiries.get_curseword(self, command.split()[1])
                if curseword.banned:
                    if curse:
                        connection.privmsg(replyto, 'Sorry, %s this word is banned!' % curse)
                    else:
                        connection.privmsg(replyto, 'Sorry, this word is banned.')
                elif curseword.irc_user:
                    user_name = quiries.get_user_name(self, curseword.irc_user.id)
                    connection.privmsg(replyto, 'Added %s by %s%s%s.' % (curseword.created, font.red, user_name, font.reset))
                elif curseword.web_user:
                    #user_name = quiries.get_user_name(self, curseword.irc_user.id)
                    connection.privmsg(replyto, 'Added %s by %s%s%s.' % (curseword.created, font.red, 'a web user', font.reset))
        elif command.split() == 3:  # Command with two arguments.
            if command.split()[1] == 'add': # Add a curse word.
                if cmdtype == 'help':
                    connection.privmsg(replyto, 'Adds curse word %s to my vocabulary.' % (command.split()[2]))
                else:   # Actual command.
                    if command.split()[2] == 'adj':
                        curse = queries.random_curse(self)
                        if curse:
                            connection.privmsg(replyto, '%sAdj%s is not a %s swearword!' % (font.italic, font.reset, curse))
                        else:
                            connection.privmsg(replyto, '%sAdj%s is not a swearword.' % (font.italic, font.reset))
                        return
                    queries.add_curseword(self, command.split()[2], user.id)
                    if curse:
                        connection.privmsg(replyto, '%s!' % curse)
            elif command.split()[1] == 'adj':   # Add an adjective.
                if cmdtype == 'help':
                    connection.privmsg(replyto, 'Adds adjective %s to my vocabulary.' % (command.split()[2]))
                else:   # Actual command.
                    queries.add_adjective(self, command.split()[2], user.id)
                    if curse:
                        connection.privmsg(replyto, '%s!'curse)
            else:   # Syntax error.
                if curse:
                    connection.privmsg(replyto, 'Invalid syntax, for %s help type: %s%s%s' % (curse, font.blue, self.network.command_character, one))
                else:
                    connection.privmsg(replyto, 'Invalid syntax, for help type: %s%s%s' % (font.blue, self.network.command_character, one))
        elif command.split() >= 4:  # Too many arguments.
            connection.privmsg(replyto, 'Too many arguments, for help type: %s%s%s' % (font.blue, self.network.command_character, one))
