from plumeria.command import CommandError, commands
from plumeria.message import Response, Message, ProxyMessage
from plumeria.util.message import parse_list


@commands.register('push', 'psh', cost=0.05, category="Operations")
async def push(message):
    """
    Pushes a message to the stack.

    Example::

        /push
    """
    message.stack.append(message)


@commands.register('pop', cost=0.05, category="Operations")
async def pop(message):
    """
    Pops a message from the stack.

    Example::

        /push
    """
    try:
        ret = message.stack.pop()
        return Response(ret.content, attachments=ret.attachments[:])
    except IndexError:
        raise CommandError("stack is empty")


@commands.register('put', 'store', 'stor', cost=0.05, category="Operations")
async def put(message):
    """
    Puts a message into a given register.

    Example::

        /put eax
    """
    parts = message.content.split(" ", 2)
    key = parts[0].strip()
    message_copy = ProxyMessage(message)
    message_copy.content = parts[1]
    message.registers[key] = message_copy


@commands.register('get', cost=0.05, category="Operations")
async def get(message):
    """
    Reads a message from a given register.

    Example::

        /get eax
    """
    parts = message.content.split(" ", 2)
    key = parts[0].strip()
    try:
        ret = message.registers[key]
        return Response(ret.content, attachments=ret.attachments[:])
    except KeyError:
        raise CommandError("no key '{}' in registers".format(key))


@commands.register('argparse', cost=0.05, category="Operations")
async def argparse(message):
    """
    Parse arguments from input and save them into registers. To use the command,
    the list of parameter names (register names) must be provided, separated
    by spaces, and then this list must be ended with a standalone @ symbol.
    After the @ symbol must be the arguments that will be parsed.
    If there are not enough arguments, an error will be raised.

    Example::

        /echo Bobby 63 | argparse name age @
    """
    parts = message.content.split(" ")
    params = []
    input = None
    for i, part in enumerate(parts):
        if part == "@":
            if len(parts) > i + 1:
                input = " ".join(parts[i + 1:])
            else:
                input = ""
            break
        else:
            params.append(part)
    args = parse_list(input, allow_spaces=True)
    if len(args) < len(params):
        raise CommandError("not enough arguments (expected {})".format(", ".join(params)))
    else:
        for i, key in enumerate(params):
            new_message = ProxyMessage(message)
            new_message.content = args[i]
            new_message.attachments = []
            message.registers[key] = new_message

@@ -933,33 +933,33 @@
 tent.split(%22 %22, 
-2
+1
 )%0A    key = part
@@ -1090,16 +1090,69 @@
 ge_copy%0A
+    return Response(%22%22, registers=message.registers)%0A
 %0A%0A@comma

Fix 'put' not properly working.