"""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

# from formalchemy.ext.pylons import maps
# from coastal.controllers.admin import AdminController

def make_map():
    """Create, configure and return the routes Mapper"""
    map = Mapper(directory=config['pylons.paths']['controllers'],
                 always_scan=config['debug'])
    map.minimization = False
    
    # 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')

    # CUSTOM ROUTES HERE

    # Added by Shabti

    # maps.admin_map(map, AdminController, 'admin')
    
    # N.B. 'http://localhost:5000/' is masked by public/index.html until 
    # the latter is (re)moved.
    map.connect('dri', '/', 
                controller='page', action='show', id='home')

    # =======  Sign in / out =================================================
    map.connect('signout', '/login/signout', 
                controller='login', action='signout')

    map.connect('signin', '/login/signin', 
                controller='login', action='signin')

    # =======  Docs controller ===============================================
    map.connect('docindex', '/docs/index', 
                controller='docs', action="show")

    map.connect('docinstall', '/docs/install', 
                controller='docs', action='install')

    map.connect('docimages', '/docs/_images/*uri', 
                controller='docs', action='servimg')

    map.connect('docs', '/docs/*uri', 
                controller='docs', action="show")

    # =======  Pages controller ==============================================
    map.connect('pages', '/pages', 
                controller='pages', action='index')

    map.connect('new_page', '/pages/new', 
                controller='pages', action='new')

    map.connect('create_page', '/pages/create', 
                controller='pages', action='create', 
                conditions=dict(method='POST'))

    map.connect('show_page', '/pages/{id}/show', 
                controller='pages', action='show', 
                requirements=dict(id='\d+'))

    map.connect('edit_page', '/pages/{id}/edit', 
                controller='pages', action='edit', 
                requirements=dict(id='\d+'))

    map.connect('save_page', '/pages/{id}/update', 
                controller='pages', action='update', 
                requirements=dict(id='\d+'), 
                conditions=dict(method='PUT'))

    map.connect('delete_page', '/pages/{id}/delete', 
                controller='pages', action='delete', 
                requirements=dict(id='\d+'), 
                conditions=dict(method='DELETE'))

    map.connect('pages_popup', '/pages/{id}/popup', 
                controller='pages', action='popup')

    map.connect('pagegrid', '/pages/gridtest', 
                controller='pages', action='gridtest')

    map.connect('pagesprockets', '/pages/dbsprockets_test', 
                controller='pages', action='dbsprockets_test')

    map.connect('pagesprox', '/pages/sprox_test', 
                controller='pages', action='sprox_test')

    map.connect('get_page', '/pages/{id}', 
                controller='pages', action='show',
                requirements=dict(id='\w+'))

    # =======  Users controller ==============================================
    map.connect('users', '/users', 
                controller='users', action='index')

    map.connect('new_user', '/users/new', 
                controller='users', action='new')

    map.connect('create_user', '/users/create', 
                controller='users', action='create', 
                conditions=dict(method='POST'))

    map.connect('show_user', '/users/{id}/show', 
                controller='users', action='show', 
                requirements=dict(id='\d+'))

    map.connect('edit_user', '/users/{id}/edit', 
                controller='users', action='edit', 
                requirements=dict(id='\d+'))

    map.connect('save_user', '/users/{id}/update', 
                controller='users', action='update', 
                requirements=dict(id='\d+'), 
                conditions=dict(method='PUT'))

    map.connect('delete_user', '/users/{id}/delete', 
                controller='users', action='delete', 
                requirements=dict(id='\d+'), 
                conditions=dict(method='DELETE'))

    map.connect('users_popup', '/users/{id}/popup', 
                controller='users', action='popup')

    map.connect('formatted_users', '/users/{format}',
                controller='users', action='index')
    
    map.connect('formatted_edit_user', '/users/{id}.{format}/edit',
                controller='users', action='edit')
    
    map.connect('formatted_new_user', '/users/{id}.{format}/new',
                controller='users', action='new')
    
    map.connect('formatted_user', '/users/{id}.{format}/show',
                controller='users', action='show')

    # =======  Groups controller =============================================
    map.connect('groups', '/groups', 
                controller='groups', action='index')

    map.connect('new_group', '/groups/new', 
                controller='groups', action='new')

    map.connect('create_group', '/groups/create', 
                controller='groups', action='create', 
                conditions=dict(method='POST'))

    map.connect('show_group', '/groups/{id}/show', 
                controller='groups', action='show', 
                requirements=dict(id='\d+'))

    map.connect('edit_group', '/groups/{id}/edit', 
                controller='groups', action='edit', 
                requirements=dict(id='\d+'))

    map.connect('save_group', '/groups/{id}/update', 
                controller='groups', action='update', 
                requirements=dict(id='\d+'), 
                conditions=dict(method='PUT'))

    map.connect('delete_group', '/groups/{id}/delete', 
                controller='groups', action='delete', 
                requirements=dict(id='\d+'), 
                conditions=dict(method='DELETE'))

    map.connect('groups_popup', '/groups/{id}/popup', 
                controller='groups', action='popup', 
                requirements=dict(id='\d+'))

    # =======  Permissions controller ========================================
    map.connect('permissions', '/permissions', 
                controller='permissions', action='index')

    map.connect('new_permission', '/permissions/new', 
                controller='permissions', action='new')

    map.connect('create_permission', '/permissions/create', 
                controller='permissions', action='create', 
                conditions=dict(method='POST'))

    map.connect('show_permission', '/permissions/{id}/show', 
                controller='permissions', action='show', 
                requirements=dict(id='\d+'))

    map.connect('edit_permission', '/permissions/{id}/edit', 
                controller='permissions', action='edit', 
                requirements=dict(id='\d+'))

    map.connect('save_permission', '/permissions/{id}/update', 
                controller='permissions', action='update', 
                requirements=dict(id='\d+'), 
                conditions=dict(method='PUT'))

    map.connect('delete_permission', '/permissions/{id}/delete', 
                controller='permissions', action='delete', 
                requirements=dict(id='\d+'), 
                conditions=dict(method='DELETE'))

    map.connect('permissions_popup', '/permissions/{id}/popup', 
                controller='permissions', action='popup')

    map.connect('tabbedindex', '/tabber/tabbedindex',
                controller='tabber', action='tabbedindex')

    map.connect('/{controller}/index', action='index')

    map.connect('/{controller}/{id}')

    # map.connect('/{controller}/{id}/{action}')

    return map


