"""Routes configuration

The more specific and detailed routes should be defined first so they
may take precedent over the more generic routes. For more information
refer to the routes manual at http://routes.groovie.org/docs/
"""
from pylons import config
from routes import Mapper

def make_map():
    """Create, configure and return the routes Mapper"""
    map = Mapper(directory=config['pylons.paths']['controllers'],
                 always_scan=config['debug'], explicit=True)
    map.minimization = False

    # explicit = True disables a broken feature called "route memory",
    # where it adds everything matched in the current request as default variables
    # for the next one.  This is wrong because it doesn't invalidate things lower down in
    # the hierarchy when higher up things change.  Rails port failure.
    # NOTE: this also disables actions defaulting to index, sorry.

    require_POST = dict(conditions={'method': ['POST']})

    # get rid of trailing slashes
    map.redirect('/*(url)/', '/{url}',
                 _redirect_code='301 Moved Permanently')


    # The ErrorController route (handles 404/500 error pages); it should
    # likely stay at the top, ensuring it can always be resolved
    map.connect('/error/{action}', controller='error')
    map.connect('/error/{action}/{id}', controller='error')

    map.connect('home', '/', controller='main', action='index')

    # Account stuff
    with map.submapper(controller="account") as sub:
        sub.connect('login',            '/account/login',           action='login')
        sub.connect('login_begin',      '/account/login_begin',     action='login_begin', **require_POST)
        sub.connect('login_finish',     '/account/login_finish',    action='login_finish')
        sub.connect('logout',           '/account/logout',          action='logout', **require_POST)
        sub.connect('register',         '/account/register',        action='register')
        sub.connect('register_finish',  '/account/register_finish', action='register_finish', **require_POST)

    # Specific user stuff
    with map.submapper(controller='users') as sub:
        sub.connect(                '/users',               action='list')
        sub.connect('user_page',    '/users/{name}',        action='view')
    with map.submapper(controller='user_settings') as sub:
        sub.connect(                '/users/{name}/settings/relationships/toggle',
                                                            action='rel_toggle', **require_POST)

    # Comments
    with map.submapper(controller='comments') as sub:
        sub.connect('/*owner_url/comments', action='thread')
        sub.connect('/*owner_url/comments/reply', action='reply')
        sub.connect('/*owner_url/comments/reply_done', action='reply_done', **require_POST)
        sub.connect('/*owner_url/comments/{id}', action='thread')
        sub.connect('/*owner_url/comments/{id}/reply', action='reply')
        sub.connect('/*owner_url/comments/{id}/reply_done', action='reply_done', **require_POST)

    # Art
    with map.submapper(controller="art") as sub:
        sub.connect('new_art',      '/art/new',         action="new")
        sub.connect('create_art',   '/art/create',      action="create")
        sub.connect('rate_art',     '/art/{id}/rate',   action="rate")
        sub.connect('show_art',     '/art/{id}',        action="show")

    # Some art pages pertain to a specific user, but still belong in the art
    # controller
    map.connect('/users/{name}/watchstream', controller='art', action='watchstream')

    with map.submapper(controller='tag') as sub:
        sub.connect('delete_tag', '/art/{art_id}/tag/{id}')
        sub.connect('create_tag', '/art/{art_id}/tag')

    with map.submapper(controller='relation') as sub:
        sub.connect('create_relation', '/art/{art_id}/relations/{kind}/create', action="create")
        # TODO: conditions: kind = by|for|of|character?

    map.resource('tag','tags', controller="tag",
        parent_resource=dict(member_name='art', collection_name='art'))
        # Yeah, parent resources are specified kinda dumb-ly.  Would be better if you could pass in the
        # real parent resource instead of mocking it up with a silly dict.  We should file a feature request.

    # I think resources is the right way to go for most things.  It ensures all of our actions have the right
    # methods on them, at least.  It does require the use of silly _method="delete" post parameters though.

    # One sticking point though is, it'll happily allow you to add any formatting string you want, like art/1.json
    # I wonder if there's a way to place requirements on that, or disable it until we actually have formats.
    # It just serves the same action as usual but with a format argument in the context.

    # map.connect('/art/new', controller='art', action='new')
    # map.connect('/art/upload', controller='art', action='upload')
    # map.connect('show_art', '/art/{id}', controller='art', action='show')
    # map.connect('/art/{id}/tag', controller='art', action='tag')

    # map.connect('/tag/{id}/delete', controller='tag', action='delete')

    map.connect('search', '/search', controller='search', action='index')
    # map.connect( '/search/{query}', controller='search', action='index')
    map.connect('saved_searches', '/search/list', controller='search', action='list')


    # default routing is back so we can test stuff.
    # please don't take it away until we have some more core features in.
    map.connect('/{controller}/{action}')
    map.connect('/{controller}/{action}/{id}')

    return map
