"""This module contains the urls for the mhr project"""

from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.http import HttpResponse
from django.views.generic import RedirectView
from django.views.static import serve as static_serve
from django.views.i18n import javascript_catalog

from mtp.apps.accounts.forms import (
    PasswordResetForm,
    SetPasswordFormWithMinLength
)
from mtp.apps.accounts.views import LoginUserView

admin.autodiscover()


# api
# ---

urlpatterns = [
    url(r'^api/', include('mtp.apps.api.urls')),
]

# cms
# ---

urlpatterns += [
    url(r'^cms/accounts/', include('mtp.apps.accounts.urls')),
    url(r'^cms/assessments/', include('mtp.apps.assessments.urls')),
    url(r'^cms/brainstormers/', include('mtp.apps.brainstormers.urls')),
    url(r'^cms/documents/', include('mtp.apps.documents.urls')),
    url(r'^cms/evaluations/', include('mtp.apps.evaluations.urls')),
    url(r'^cms/ideas/', include('mtp.apps.ideas.urls')),
    url(r'^cms/media/', include('mtp.apps.media.urls', namespace='media')),
    url(r'^cms/metaplans/', include('mtp.apps.metaplans.urls')),
    url(r'^cms/photostories/', include('mtp.apps.photostories.urls')),
    url(r'^cms/questionnaires/', include('mtp.apps.questionnaires.urls')),
    url(r'^cms/roleplays/', include('mtp.apps.roleplays.urls')),
    url(r'^cms/stats/', include('mtp.apps.stats.urls')),
    url(r'^cms/surveys/', include('mtp.apps.surveys.urls')),
    url(r'^cms/sync/', include('mtp.apps.sync.urls')),

    url(r'^cms/trainings/', include('mtp.apps.trainings.urls')),
    url(r'^cms/translation/', include('mtp.apps.translation.urls')),
    url(r'^cms/workshops/', include('mtp.apps.workshops.urls')),
    url(r'^cms/tasks/', include('mtp.apps.tasks.urls')),
    url(r'^cms/', include('mtp.apps.cms.urls')),
]

# root --> redirect to cms home
# -----------------------------

urlpatterns += [
    url(r'^$', RedirectView.as_view(pattern_name='cms:index', permanent=True)),
]

# i18n
# -----------------------------

urlpatterns += [
    url(r'^i18n/', include('django.conf.urls.i18n')),
    url(r'^jsi18n/$', javascript_catalog, name='javascript-catalog'),
]

# admin & auth
# ------------

urlpatterns += [
    url(r'^admin/', admin.site.urls),
    url(r'^auth/login', LoginUserView.as_view(), name='login'),
    url(
        r'^auth/password_reset/$',
        auth_views.password_reset,
        {
            'email_template_name': 'pinax/notifications/password_reset/body.txt',
            'subject_template_name': 'pinax/notifications/password_reset/subject.txt',
            'html_email_template_name': 'pinax/notifications/password_reset/body.html',
            'password_reset_form': PasswordResetForm,
        },
        name='password_reset'
    ),
    url(
        r'^auth/reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
        auth_views.password_reset_confirm,
        {'set_password_form': SetPasswordFormWithMinLength},
        name='password_reset_confirm'
    ),
    url(r'^auth/', include('django.contrib.auth.urls')),
]

# robots.txt
# ----------

urlpatterns += [
    url(
        r'^robots\.txt$',
        lambda r: HttpResponse(
            'User-agent: *\nDisallow: /', content_type='text/plain'
        )
    ),
]


# django-markdown
# ---------------

if 'django_markdown' in settings.INSTALLED_APPS:

    # We redefine the markdown urls here to avoid using `patterns` which is
    # deprecated and the django-markdown library was not updated to take
    # this into account.
    # So we include our own patterns list instead of 'django_markdown.urls'

    # pylint: disable=wrong-import-order,wrong-import-position
    from django_markdown.views import preview
    django_markdown_urlpatterns = [
        url('preview/$', preview, name='django_markdown_preview')
    ]

    urlpatterns += [
        url('^markdown/', include(django_markdown_urlpatterns)),
    ]

# rosetta
# -------

if 'rosetta' in settings.INSTALLED_APPS:
    urlpatterns += [
        url(r'^rosetta/', include('rosetta.urls'))
    ]

# django-select2
# --------------

if 'django_select2' in settings.INSTALLED_APPS:
    urlpatterns += [
        url(r'^select2/', include('django_select2.urls')),
    ]

# static files serving in debug mode
# ----------------------------------

if settings.DEBUG:
    urlpatterns += [
        url(r'^media/(?P<path>.*)$', static_serve, {
            'document_root': settings.MEDIA_ROOT
        }),
        url(r'^static/(?P<path>.*)$', static_serve, {
            'document_root': settings.STATIC_ROOT
        }),
    ]
