#!/usr/bin/env python

import sys
import time
import argparse
import StringIO

import catsnap
from catsnap import Client
import catsnap.config.file_config
from catsnap.config.base import Config
from catsnap.config import MetaConfig
from catsnap.image_truck import ImageTruck
from catsnap.document.image import Image
from catsnap.batch.image_batch import get_images
from catsnap.batch.tag_batch import get_tags, add_image_to_tags

from boto.exception import DynamoDBResponseError

def add(args):
    truck = ImageTruck.new_from_something(args.path)
    truck.upload()
    image = Image(truck.calculate_filename(), truck.source_url)
    image.add_tags(args.tags)
    add_image_to_tags(truck.calculate_filename(), args.tags)

    print truck.url()

def find(args):
    filenames = set()
    tags = get_tags(args.tags)
    for tag in tags:
        filenames.update(tag['filenames'])
    images = get_images(filenames)
    for image in images:
        filename = image['filename']
        image_tags = ' '.join(image['tags'])
        url = ImageTruck.url_for_filename(filename)
        print '%s   |   %s' % (url, image_tags)

def setup(args):
    update_config(args)
    created_tables = Client().setup()
    if created_tables:
        sys.stdout.write("""
I've created image and tag tables. Dynamodb tables take some time after being
created before they're available for use--please wait a minute to add an image.
""")

def update_config(args):
    config = catsnap.config.file_config.FileConfig()
    if 'settings' in args:
        config.collect_settings(settings_to_get=args.settings)
    else:
        config.collect_settings()

def main():
    orig_stdout = sys.stdout
    stdout_capture = StringIO.StringIO()
    sys.stdout = stdout_capture
    error = None
    try:
        config = MetaConfig()
    except SystemExit:
        #the argparse inside the ArgumentConfig will print to
        #stdout and then throw a SYSTEM GODDAMN EXIT.
        error = stdout_capture.getvalue()
    finally:
        sys.stdout = orig_stdout
    parser = argparse.ArgumentParser(
            usage='',
            description='Store and search for images by tag.')
    subparsers = parser.add_subparsers(help='Actions')

    add_command = subparsers.add_parser('add', help='add an image')
    add_command.add_argument('path', help='url or filename to add')
    add_command.add_argument('tags', nargs='+', help='tags for this image')
    add_command.set_defaults(func=add)

    find_command = subparsers.add_parser('find', help='find an image')
    find_command.add_argument('tags', nargs='+', help='tags to find')
    find_command.set_defaults(func=find)

    setup_command = subparsers.add_parser('setup', help='do first-time setup')
    setup_command.set_defaults(func=setup)

    config_command = subparsers.add_parser('config',
            help='update configuration')
    #we have to manually add a "no choice" option because of what appears to
    #be an argparse bug: choices=[...] requires some choice even though
    #nargs='*' says "no choice is required."
    choices = map(str, Config.ALL_SETTINGS)
    choices.append([])
    config_command.add_argument('settings', nargs='*', help='settings to '
            'update (or leave blank, to loop through all available settings)',
            choices=choices)
    #we had to map str() on the settings because of an argparse bug:
    #if the user specifies an invalid option here, the error message
    #uses the __repr__ of the choices rather than their __str__.
    config_command.set_defaults(func=update_config)

    #this help command is necessary because the argparse inside MetaConfig
    #doesn't see the `-h` in `catsnap find -h` as a flag on find, but as a
    #flag on catsnap itself. find never gets to see the -h and we don't get
    #its help from it.
    help_command = subparsers.add_parser('help',
            help='show help for an individual command')
    parsers = {
            'add': add_command,
            'find': find_command,
            'setup': setup_command,
            'help': help_command,
            'config': config_command}
    help_command.add_argument('command', nargs=1,
            choices=parsers.keys())
    def show_command_help(args):
        sys.stderr.write(parsers[args.command[0]].format_help())
    help_command.set_defaults(func=show_command_help)

    if error is not None:
        sys.stderr.write(error)
        sys.stderr.write(parser.format_help())
        sys.exit(2)
    args = parser.parse_args(config['arg'])
    args.func(args)

try:
    main()
except DynamoDBResponseError, e:
    if e.status == 400 and e.error_code == u'ResourceNotFoundException':
        sys.stderr.write("""
######
I caught a ResourceNotFoundException while trying to interact with DynamoDB.
This may mean that you need to run `catsnap setup`. This can also happen
sometimes right after creating a new DB table--it takes a while before the
table is ready for queries. If you've run setup recently, please wait a bit
and try again...
######
""")
        sys.exit(1)
    else:
        raise

