# Path: ui/utils.py
# class MethodDispatcher(object):
#     def __init__(self, method_map):
#         self.methods= method_map
# 
#     def resolve(self, request):
#         view_func = self.methods.get(request.method, None)
#         return view_func
# 
#     def __call__(self, request, *args, **kwargs):
#         view_func = self.resolve(request)
#         if view_func:
#             return view_func(request, *args, **kwargs)
#         return http.HttpResponseNotAllowed(self.methods.keys())


from django.conf.urls import patterns
from django.conf import settings
from ui.views import *
from ui.utils import MethodDispatcher
from widget_views import *
    # record and carenet handling
    (r'^records/$', record_create),
    (r'^records/(?P<record_id>[^/]+)/carenets/$', record_carenet_create),
    
    # carenet handling
    (r'^carenets/(?P<carenet_id>[^/]+)/rename$', carenet_rename),
    (r'^carenets/(?P<carenet_id>[^/]+)$', MethodDispatcher({'DELETE': carenet_delete})),

    # indivo api calls
    (r'^indivoapi/delete_record_app/$', indivo_api_call_delete_record_app),
    (r'^indivoapi/(?P<relative_path>.+)$', indivo_api_call_get),

    # oauth
    (r'^oauth/authorize$', authorize),
    (r'^accounts/(?P<account_id>[^/]+)/apps/(?P<app_email>[^/]+)/connect_credentials', get_connect_credentials),

    # widgets
    (r'^lib/(?P<path>[^/]*)$', 'django.views.static.serve', {'document_root': settings.SERVER_ROOT_DIR + '/ui/lib'}),
    (r'^widgets/DocumentAccess$', document_access),

    # this could be improved: we could provide shortcuts for things like css, js, etc for less
    # verbose paths in templates, etc., but for now let's keep things very literal
    # for instance:
    # <script type="text/javascript" src="/jmvc/ui/resources/js/jquery-x.js"></script>
    # could be
    # "/js/jquery-x.js" -> /jmvc/ui/resources/js/jquery-x.js

    # This servers JMVC view templates, but passes them trough django's template engine.
    # This allows localization with standart django template tags - trangs, blocktrans
    (r'^jmvc/(?P<path>.*ejs)$', localize_jmvc_template, {'document_root': settings.SERVER_ROOT_DIR + '/ui/jmvc/'}),

# Path: ui/utils.py
# class MethodDispatcher(object):
#     def __init__(self, method_map):
#         self.methods= method_map
# 
#     def resolve(self, request):
#         view_func = self.methods.get(request.method, None)
#         return view_func
# 
#     def __call__(self, request, *args, **kwargs):
#         view_func = self.resolve(request)
#         if view_func:
#             return view_func(request, *args, **kwargs)
#         return http.HttpResponseNotAllowed(self.methods.keys())


# Path: ui/urls.py
from django.conf.urls import patterns
from django.conf import settings
from ui.views import *
from ui.utils import MethodDispatcher
from widget_views import *

    # record and carenet handling
    (r'^records/$', record_create),
    (r'^records/(?P<record_id>[^/]+)/carenets/$', record_carenet_create),
    
    # carenet handling
    (r'^carenets/(?P<carenet_id>[^/]+)/rename$', carenet_rename),
    (r'^carenets/(?P<carenet_id>[^/]+)$', MethodDispatcher({'DELETE': carenet_delete})),

    # indivo api calls
    (r'^indivoapi/delete_record_app/$', indivo_api_call_delete_record_app),
    (r'^indivoapi/(?P<relative_path>.+)$', indivo_api_call_get),

    # oauth
    (r'^oauth/authorize$', authorize),
    (r'^accounts/(?P<account_id>[^/]+)/apps/(?P<app_email>[^/]+)/connect_credentials', get_connect_credentials),

    # widgets
    (r'^lib/(?P<path>[^/]*)$', 'django.views.static.serve', {'document_root': settings.SERVER_ROOT_DIR + '/ui/lib'}),
    (r'^widgets/DocumentAccess$', document_access),

    # this could be improved: we could provide shortcuts for things like css, js, etc for less
    # verbose paths in templates, etc., but for now let's keep things very literal
    # for instance:
    # <script type="text/javascript" src="/jmvc/ui/resources/js/jquery-x.js"></script>
    # could be
    # "/js/jquery-x.js" -> /jmvc/ui/resources/js/jquery-x.js

    # This servers JMVC view templates, but passes them trough django's template engine.
    # This allows localization with standart django template tags - trangs, blocktrans
    (r'^jmvc/(?P<path>.*ejs)$', localize_jmvc_template, {'document_root': settings.SERVER_ROOT_DIR + '/ui/jmvc/'}),

    (r'^jmvc/(?P<path>ui/controllers/.*js)$', localize_jmvc_template, {'document_root': settings.SERVER_ROOT_DIR + '/ui/jmvc/'}),