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 games channel function 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(self, channel.id):   # Games are turned off in channel settigns.
            return  # Do nothing.

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

    elif one == 'vocabulary' or one == 'vocab':
        curse = queries.random_curse(self)
        categories = ['curseword', 'adjective', 'nickreply']
        if len(command.split()) == 1:    # Command without arguments
            if cmdtype == 'help':
                connection.privmsg(replyto, 'Add, display or ban a vocabulary item. Option and item are optional arguments.')
                connection.privmsg(replyto, '%sUsage:%s %s%s %scatagory option item' % (font.grey, font.blue, self.network.command_character, one, font.italic))
                connection.privmsg(replyto, 'List categories: %s%s%slist categories%s List options: %s%s%slist options' % (font.blue, self.network.command_character, one, font.reset, font.blue, self.network.command_character, one))
            else:   # Actual command without arguments.
                connection.privmsg(replyto, 'Insuficcient arguments. For help type: %s%s%s' % (font.blue, self.network.help_character, one))
        elif len(command.split()) == 2:   # Command with one argument.
            if command.split()[1] == 'list':
                connection.privmsg(replyto, 'List categories: %s%s%s list categories%s List options: %s%s%slist options' % (font.blue, self.network.command_character, one, font.reset, font.blue, self.network.command_character, one))
            elif command.split()[1] in categories:
                if cmdtype == 'help':
                    connection.privmsg(replyto, 'Show a random %s' % command.split()[1])
                else:
                    connection.privmsg(replyto, queries.random_vocabulary_item(self, command.split()[1].lower()))
            else:
                connection.privmsg(replyto, 'Invalid argument, for help type: %s%s%s' % (font.blue, self.network.command_character, one))
        elif len(command.split()) == 3:  # Command with two arguments.
            if command.split()[1] == 'list':
                if command.split()[2] == 'categories':
                    if cmdtype == 'help':
                        connection.privmsg(replyto, 'Lists available categories.')
                    else:   # List categories
                        connection.privmsg(replyto, ' ,'.join(categories))
                elif command.split()[2] == 'options':
                    if cmdtype == 'help':
                        connection.privmsg(replyto, 'Lists available options.')
                    else:
                        connection.privmsg(replyto, 'add, info, ban')
                else:
                    connection.privmsg(replyto, 'Unkown list, options are %scategories%s and %soptions%s.' % (font.blue, font.reset,font.blue, font.reset))
            elif command.split()[1] in catagories:
                if command.split()[2] in ['add', 'info', 'ban']:
                    if cmdtype == 'help':
                        connection.privmsg(replyto, 'Without specifiying add, display or ban, this displays a random %s' % command,split()[1])
                    else:
                        connection.privmsg(replyto, queries.random_vocabulary_item(self, commands.spit()[1]))
                else:
                    connection.privmsg(replyto, 'Invalid option. Options are: add info ban')
            else:
                connection.privmsg(replyto, 'Invalid catagory. To list valid categories use: %s%s%s list categories' % (font.blue, self.network.command_character, one))
        elif len(command.split()) >= 4:  # Command with three or more arguments.
            if command.split()[1] in catagories:
                if command.split()[2] == 'add':
                    if cmdtype == 'help':
                        connection.privmsg(replyto, 'Adds %s to %s' % (command.split()[3:], command.split()[1]))
                    else:
                        queries.add_vocabulary_item(self, table_suffix, user.id, command.split()[3:])
                        connection.privmsg(replyto, '%s added to %s' % (command.split()[3:]), commands.split()[1])
                elif command.split()[2] == 'info':
                    if cmdtype == 'help':
                        connection.privmsg(replyto, 'Displays info on %s from %s' % (command.split()[3:], command.split()[1]))
                    else:
                        item = queries.get_vocabulary_item(self, command.split()[1], command.split()[3:])
                        if not item:
                            connection.privmsg(replyto, '%s is not in my vocabulary.' % command.split()[3:])
                        else:
                            if item.irc_user:
                                item_user = item.irc_user
                            else:
                                item_user = item.web_user
                            if item.banned:
                                if item.banned_by_irc_user:
                                    item_ban_user = queries.get_user_name(self, item.banned_by_irc_user)
                                else:
                                    item_ban_user = item.banned_by_web_user
                                connection.privmsg(replyto, 'Created by %s on %s. Banned by %s on %s.' % (item_user, item.created, item_ban_user, item.ban_created))
                elif command.split()[1] == 'ban':
                    if cmdtype == 'help':
                        connection.privmsg(replyto, 'Bans bot from uttering %s from %s' % (command.split()[3:], command.split()[1]))
                    else:
                        item = queries.get_vocabulary_item(self, table_suffix, command.split()[3:])
                        if not item:
                            connection.privmsg(replyto, '%s is not in my vocabulary.' % command.split()[3:])
                        else:
                            queries.ban_vocabulary_item(self, command.split()[1], user.id, item.id)
                            connection.privmsg(replyto, '%s banned from %s' % (command.split()[3:]), commands.split()[1])
                else:
                    connection.privmsg(replyto, 'Invalid catagory. To list valid categories use: %s%s%s list categories' % (font.blue, self.network.command_character, one))
            else:
                connection.privmsg(replyto, 'Invalid catagory. To list valid categories use: %s%s%s list categories' % (font.blue, self.network.command_character, one))

    # 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))
