import os

# from django.conf.urls import include, url
from django.conf.urls import include
from django.urls import re_path as url

from django.conf.urls.static import *
from django.contrib import admin
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth import logout
from django.contrib.auth.views import *
from django.db.models import Q
from django.views.generic import *
from django.urls import path

import django

from rest_framework import routers

# import  autocomplete_light

from ck.models import *
from ck.views import *

import ck

pathname = os.path.dirname(__file__)
abspath = os.path.abspath(pathname)

router = routers.DefaultRouter()
# router.register(r'vevent', VEVENT_ViewSet)

# admin.autodiscover()

router.register(r'patients', PatientViewSet)

urlpatterns = [
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    # url(r'^admin/', include(admin.site.urls)),
    url(r'^admin/', admin.site.urls),
    
#     url(r'^autocomplete/', include('autocomplete_light.urls')),
    
    url(
        r'^icd10-autocomplete/$',
        ICD10Autocomplete.as_view(),
        name='icd10-autocomplete',
    ),

    path(r'icd10/top-list-dict/', ck.views.top_icd10_list_dict, name='top-icd10-list-dict'),

    
    url(r'(?P<path>images\/.+gif)$', django.views.static.serve, {'document_root': abspath}),

    url(r'^accounts/logout/', LogoutView.as_view()),
    url(r'^accounts/profile/', RedirectView.as_view(url='/')),
    url(r'^accounts/', LoginView.as_view()),


    url(r'^calendar/followup/', TemplateView.as_view(template_name='calendar/followup.html')),
    #  url(r'^calendar/', staff_member_required(TemplateView.as_view(template_name='calendar/index.html'))),
    url(r'^calendar/', staff_member_required(ck.views.CalendarIndex)),

    url(r'^wdCalendar/$', TemplateView.as_view(template_name='wdCalendar.html')),
    url(r'^datafeed/', ck.views.datafeed),

    url(r'^dojo/(?P<path>.*)$', django.views.static.serve,
    {'document_root': '/home/ntuh/domains/ck.ntuh.net/mysite/dojo'}),
    url(r'^dojo-release-1.1.0/(?P<path>.*)$', django.views.static.serve,
    {'document_root': '/home/ntuh/domains/ck.ntuh.net/mysite/dojo-release-1.1.0'}),

#     (r'^event/create/', limited_create_object, {'model': VEVENT,}),
    url(r'^event/create/(?P<treatment_id>\d+)/', staff_member_required(CreateEventForTreatment.as_view(model = VEVENT))),
    url(r'^event/delete/(?P<pk>\d+)/', DeleteView.as_view(model = VEVENT, success_url = '/treatment/detail/')),
    url(r'^event/update/(?P<pk>\d+)/', UpdateView.as_view(model=VEVENT, fields='__all__'), name='event-update'),

    url(r'^followup/create/(?P<patient_id>\d+)/', staff_member_required(CreateForPatient.as_view(model = Followup))),
    url(r'^followup/delete/(?P<pk>\d+)/', DeleteView.as_view(model = Followup, success_url = '/patient/detail/')),
    url(r'^followup/update/(?P<pk>\d+)/', UpdateView.as_view(model=Followup, fields='__all__')),
    
    url(r'^followup/newimages/', ck.views.followup_newimages),
    url(r'^followup/markimages/(?P<RequestSheetNo>\w+)/(?P<Saved>\d+)/', ck.views.followup_markimages),

    url(r'^followup/nosee/', ck.views.followup_nosee),
    url(r'^followup/image_long/', ck.views.followup_image_long),
    url(r'^followup/new_volume/(?P<object_id>\d*)', ck.views.followup_new_volume),
#     (r'^followup/delete_volume/(?P<object_id>\d+)/', limited_delete_object, 
#        {'model': LesionFollow, 'post_delete_redirect': reverse('followup_new_volume')}),
    url(r'^followup/delete_volume/(?P<pk>\d+)/', DeleteView.as_view(model = LesionFollow, success_url = '/followup/new_volume/')), 
     
#     (r'^lesion/create/', limited_create_object, {'model': Lesion}),

    #  url(r'^lesion/create/(?P<treatment_id>\d+)/' , staff_member_required(CreateForTreatment.as_view(model = Lesion))),
    #  url(r'^lesion/delete/(?P<pk>\d+)/', DeleteView.as_view(model = Lesion, success_url = '/treatment/detail/')),
    #  url(r'^lesion/update/(?P<pk>\d+)/', UpdateView.as_view(model = Lesion, fields='__all__')),

    path(r'lesion/create/<int:treatment_id>/', LesionCreate.as_view(), name='lesion-create'),
    path(r'lesion/update/<int:pk>/', LesionUpdate.as_view(), name='lesion-update'),
    path(r'lesion/delete/<int:pk>/', LesionDelete.as_view(), name='lesion-delete'),

    url(r'^prior/create/(?P<patient_id>\d+)/', staff_member_required(CreateForPatient.as_view(model = PriorTreatment))),
    url(r'^prior/delete/(?P<pk>\d+)/', DeleteView.as_view(model = PriorTreatment, success_url = '/patient/detail/')),
    url(r'^prior/update/(?P<pk>\d+)/', UpdateView.as_view(model = PriorTreatment, fields='__all__')),
    url(r'^prior/oncrt_query/(?P<object_id>\d+)/', ck.views.prior_oncrt_query),
    url(r'^prior/op_note/(?P<object_id>\d+)/', ck.views.prior_op_note),

    url(r'^prior/path_exam/(?P<object_id>\d+)/', ck.views.prior_path_exam),

    url(r'^query/heightweight/(?P<object_id>\d+)/', ck.views.query_height_weight),

    url(r'^patient/add/', ck.views.patient_add),
    url(r'^patient/delete/(?P<pk>\d+)/', DeleteView.as_view(model = Patient, success_url = '/patient/list/'), name='patient-delete'),
    url(r'^patient/detail/(?P<object_id>\d*)', ck.views.patient_detail, name='patient-detail'),
    url(r'^patient/follow/', ck.views.patient_follow),
#      url(r'^patient/list/', staff_member_required(ListView.as_view(
#             queryset=Patient.objects.order_by('-timestamp'),
# #             context_object_name='latest_poll_list',
# #             template_name='polls/index.html',
#             paginate_by = 25,
#       ))),
    url(r'^patient/print/(?P<object_id>\d*)', ck.views.patient_print),
    url(r'^patient/search/', staff_member_required(ck.views.patient_search),  name='patient-search'),
    url(r'^patient/update/(?P<pk>\d+)/', UpdateView.as_view(model = Patient, fields='__all__'), name='patient-update'),

    path('patient/list/', staff_member_required(ck.views.PatientDatatableView.as_view()), name='patient-list'),
    # path("patient/list/", PatientListView.as_view()),

    url(r'^record/weekly/(?P<date>[^/]*)', ck.views.record_weekly),

#     (r'^timetable/location/', 'ck.views.timetable_location'),
    url(r'^timetable/scheduling/(?P<date>[^/]*)', ck.views.timetable_scheduling),

    url(r'^treatment/create/(?P<patient_id>\d+)/', staff_member_required(CreateForPatient.as_view(model = Treatment, form_class=TreatmentForm))),
    url(r'^treatment/delete/(?P<pk>\d+)/', staff_member_required(DeleteView.as_view(model = Treatment, success_url = '/treatment/list/')), name='treatment-delete'),
#     (r'^treatment/delete/(?P<pk>\d+)/', staff_member_required(RequestDeleteView.as_view(model = Treatment, success_url = '/treatment/list/'))),
    url(r'^treatment/detail/(?P<object_id>\d*)', ck.views.treatment_detail, name = 'treatment-detail'),
    #  url(r'^treatment/list/', ListView.as_view(
    #         queryset=Treatment.objects.order_by('-id'),
    #         paginate_by = 25,
    #   )),
    path('treatment/list/', staff_member_required(ck.views.TreatmentDatatableView.as_view()), name='treatment-list'),
    path('treatment/finished/', staff_member_required(ck.views.TreatmentDatatableView.as_view(queryset=Treatment.objects.filter(input__id__gte=300).order_by('-id'))), name='treatment-finished'),
    path('treatment/unfinished/', staff_member_required(ck.views.TreatmentDatatableView.as_view(queryset=Treatment.objects.filter(~Q(input__id__gte=300)).order_by('-id'))), name='treatment-unfinished'),
    path('treatment/no-icd/', staff_member_required(ck.views.TreatmentDatatableView.as_view(queryset=Treatment.objects.filter(icd9__isnull=True).order_by('-id'))), name='treatment-no-icd'),
    # path('treatment/finished/', staff_member_required(ck.views.TreatmentDatatableView.as_view(queryset=Treatment.objects.filter(output__id__gte=900).order_by('-id'))), name='treatment-finished'),
    # path('treatment/unfinished/', staff_member_required(ck.views.TreatmentDatatableView.as_view(queryset=Treatment.objects.filter(output__id__lt=900).order_by('-id'))), name='treatment-finished'),

    #  url(r'^treatment/finished/', staff_member_required(ListView.as_view(
    #         queryset=Treatment.objects.filter(output__id__gte=900).order_by('-id'),
    #         paginate_by = 25,
    #   ))),
     
    #  url(r'^treatment/unfinished/', staff_member_required(ListView.as_view(
    #         queryset=Treatment.objects.filter(output__id__lt=900).order_by('-output'),
    #         paginate_by = 25,
    #   ))),
     
    url(r'^treatment/print/(?P<object_id>\d*)', ck.views.treatment_print),
    url(r'^treatment/record/(?P<object_id>\d*)', ck.views.treatment_record),
    url(r'^treatment/report/(?P<object_id>\d*)', ck.views.treatment_report),
    url(r'^treatment/update/(?P<pk>\d+)/', staff_member_required(TreatmentUpdate.as_view()), name='treatment-update'),
    # url(r'^treatment/update/(?P<pk>\d+)/', staff_member_required(UpdateView.as_view(model = Treatment, form_class=TreatmentForm)), name='treatment-update'),
#      (r'^treatment/update/(?P<pk>\d+)/', staff_member_required( autocomplete_light.views.AutocompleteView.as_view(model = Treatment))),

    url(r'^below/', ck.views.below),

    url(r'^get/followup/', ck.views.get_followup),
    url(r'^get/icd9cm/', ck.views.get_icd9cm),
    url(r'^get/pathology/', ck.views.get_pathology),
    url(r'^get/target_location/', ck.views.get_target_location),
    url(r'^get/sublocation/', ck.views.get_sublocation),
    url(r'^get/vevent/', ck.views.get_vevent),

    url(r'^update/vevent/', ck.views.update_vevent),

    url(r'^event/drop/', ck.views.event_drop),
    url(r'^event/resize/', ck.views.event_resize),

    #  path('pre-case/', ck.views.NHIOrderList.as_view(), name='pre-case'),
    path('pre-case/', login_required(ck.views.NHIOrderDatatableView.as_view()), name='pre-case'),
    path('pre-case/add', login_required(ck.views.pre_add), name='pre-add'),
    path('DoctorOption', ck.views.DoctorOption, name='DoctorOption'),


    url(r'^nsclc/', include('nsclc.urls')),
    url(r'^calculator/', include('calculator.urls')),

    url('^vevent/', VEVENT_List.as_view()),


    # url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls')),
    url('^api/', include(router.urls)),
    
    # url(r'^schedule/', include('schedule.urls')),

########## Catch the remaining     

    url(r'^(?P<path>.+)$', django.views.static.serve, {'document_root': abspath}),
#     (r'images/(?P<path>.+)$', 'django.views.static.serve', {'document_root': abspath+'/images/'}),
#     (r'(?P<path>images\/.+gif)$', 'django.views.static.serve', {'document_root': abspath}),

#     (r'.*', simple.redirect_to, {'url': '/treatment/list'}),
    url(r'.*', login_required(ck.views.NHIOrderDatatableView.as_view())),

    
 ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

if settings.DEBUG:
    import debug_toolbar
    urlpatterns = [
        url(r'^__debug__/', include(debug_toolbar.urls)),
    ] + urlpatterns
