
import re

from apps.content.views import (ArticleDetailView, CategoryDetailView,
                                TagDetailView)
from apps.content.views.download import DownloadDetailView
from apps.content.views.draft import (draft_clear, draft_delete, load_draft,
                                      save_draft)
from apps.content.views.image_gallery import ImageDetailView, download_image
from apps.content.views.search import SearchView
from apps.content.views.wiki import WikiDetailView
from apps.core.services.route_service import get_dynamic_routes
from apps.core.views.web import HomeView
from django.conf import settings
from django.urls import include, path, re_path

url_config = settings.GLOBAL_CONFIG.get('UrlStyle', {})
custom_list = ''
custom_item = ''
urlpatterns = [
    path('', HomeView.as_view(), name='home'),
    re_path(r'^index_(?P<pagenum>\d+)(?:\.(html|shtml|htm))?/?$',
            HomeView.as_view(),
            name='homepage'),
    path('search/', SearchView.as_view(), name='search'),
] + get_dynamic_routes()
# 保存、加载、移除草稿

urlpatterns += [
    path('draft/save/', save_draft, name='save_draft'),
    path('draft/load/', load_draft, name='load_draft'),
    path('draft/clear/', draft_clear, name='draft_clear'),
    path('draft/delete/', draft_delete, name='draft_delete'),
]
#用户
urlpatterns += [
    path('users/', include(('apps.users.urls', 'users'),
                           namespace='user')),  #验证码
]
# categoryUrl
try:
    front = url_config.get('url_front', {})
except:
    front = 'model'
if not front:
    front = 'model'
try:
    custom_list = url_config.get('custom_slug', {})
except:
    custom_list = 'node'
if not custom_list:
    custom_list = 'node'
try:
    custom_item = url_config.get('custom_article', {})
except:
    custom_item = 'item'
if not custom_item:
    custom_item = 'item'

#imagegallery页面
urlpatterns += [
    path('image/<int:pk>/<str:size>/', download_image, name='image-download'),
    re_path(r'^image/(?P<pk>[\w-]+)(?:\.(?:html|shtml|htm))?/?$',
            ImageDetailView.as_view(),
            name='wiki-detail'),
    re_path(r'^image/(?P<pk>\d+)_(?P<pagenum>\d+)(?:\.(html|shtml|htm))?/?$',
            ImageDetailView.as_view(),
            name='wiki-detail-pagenum'),
]
#wiki页面
urlpatterns += [
    re_path(r'^download/(?P<pk>[\w-]+)(?:\.(?:html|shtml|htm))?/?$',
            DownloadDetailView.as_view(),
            name='wiki-detail'),
    re_path(
        r'^download/(?P<pk>\d+)_(?P<pagenum>\d+)(?:\.(html|shtml|htm))?/?$',
        DownloadDetailView.as_view(),
        name='wiki-detail-pagenum'),
]
#wiki页面
urlpatterns += [
    re_path(r'^wiki/(?P<pk>[\w-]+)(?:\.(?:html|shtml|htm))?/?$',
            WikiDetailView.as_view(),
            name='wiki-detail'),
    re_path(r'^wiki/(?P<pk>\d+)_(?P<pagenum>\d+)(?:\.(html|shtml|htm))?/?$',
            WikiDetailView.as_view(),
            name='wiki-detail-pagenum'),
]
#TAG页面
tag_identifier = url_config.get('tag_identifier', 'tag').strip()
if not tag_identifier:
    tag_identifier = 'tag'

# 修改Tag路由 - 使用精确匹配
urlpatterns += [
    re_path(r'^{}/(?P<pk>\w+)_(?P<pagenum>\d+)(?:\.(html|shtml|htm))?/?$'.format(re.escape(tag_identifier)),
            TagDetailView.as_view(),
            name='tag-detail-pagenum'),
    re_path(r'^{}/(?P<pk>[\w-]+)(?:\.(?:html|shtml|htm))?/?$'.format(re.escape(tag_identifier)),
            TagDetailView.as_view(),
            name='tag-detail'),
]

if front == 'model':
    urlpatterns += [
        re_path(
            r'^category/(?P<pk>[\w-]+)_(?P<pagenum>[\w-]+)(?:\.(?:html|shtml|htm))?/?$',
            CategoryDetailView.as_view(),
            name='category-detail'),
        re_path(r'^category/(?P<pk>[\w-]+)(?:\.(?:html|shtml|htm))?/?$',
                CategoryDetailView.as_view(),
                name='category-detail'),
        re_path(r'^article/(?P<pk>[\w-]+)(?:\.(?:html|shtml|htm))?/?$',
                ArticleDetailView.as_view(),
                name='article-detail'),
    ]
elif front == 'slug':
    urlpatterns += [
        re_path(
            r'^(?P<slug>[\w-]+)/index_(?P<pagenum>\d+)(?:\.(?:html|shtml|htm))?$',
            CategoryDetailView.as_view(),
            name='slug_style'),
        re_path(r'^(?P<slug>[\w-]+)/?$',
                CategoryDetailView.as_view(),
                name='slug_style'),
        re_path(r'^(?P<slug>[\w-]+)/index(?:\.(?:html|shtml|htm))?/?$',
                CategoryDetailView.as_view(),
                name='slug_style'),
        re_path(
            r'^(?P<slug>[\w-]+)/(?P<pk>[\w-]+)(?:\.(?:html|shtml|htm))?/?$',
            ArticleDetailView.as_view(),
            name='article-detail'),
    ]
elif front == 'moreslug':
    urlpatterns += [
        re_path(
            r'^(?P<moreslug>[\w-]+)/index_(?P<pagenum>\d+)(?:\.(?:html|shtml|htm))?$',
            CategoryDetailView.as_view(),
            name='slug_style'),
        re_path(r'^(?P<moreslug>[\w\-/]+)/?$',
                CategoryDetailView.as_view(),
                name='slug_style'),
        re_path(r'^(?P<moreslug>[\w\-/]+)/index(?:\.(?:html|shtml|htm))?/?$',
                CategoryDetailView.as_view(),
                name='slug_style'),
        re_path(r'^[\w-]+/(?P<pk>[\w-]+)(?:\.(?:html|shtml|htm))?/?$',
                ArticleDetailView.as_view(),
                name='article-detail'),
    ]
elif front == 'random':
    urlpatterns += [
        re_path(
            r'^(?P<slug>[\w-]+)/index_(?P<pagenum>\d+)(?:\.(?:html|shtml|htm))?$',
            CategoryDetailView.as_view(),
            name='slug_style'),
        re_path(r'^(?P<slug>[\w-]+)/index(?:\.(?:html|shtml|htm))?$',
                CategoryDetailView.as_view(),
                name='slug_style'),
        re_path(r'^(?P<slug>[\w-]+)/?$',
                CategoryDetailView.as_view(),
                name='slug_style'),
        re_path(r'^[\w-]+/(?P<pk>[\w-]+)(?:\.(?:html|shtml|htm))?$',
                ArticleDetailView.as_view(),
                name='article-detail'),
    ]
elif front == 'custom':
    urlpatterns += [
        re_path(
            rf'^{custom_list}/(?P<pk>[\w-]+)_(?P<pagenum>\d+)(?:\.(?:html|shtml|htm))?/?$',
            CategoryDetailView.as_view(),
            name='category-detail'),
        re_path(rf'^{custom_list}/(?P<pk>[\w-]+)(?:\.(?:html|shtml|htm))?/?$',
                CategoryDetailView.as_view(),
                name='category-detail'),
        re_path(rf'^{custom_item}/(?P<pk>[\w-]+)(?:\.(?:html|shtml|htm))?/?$',
                ArticleDetailView.as_view(),
                name='article-detail'),
    ]

#     [('model', '模型路径'), ('slug', '单级Slug'),
#                          ('moreslug', '多级Slug'), ('random', '随机字符'),
#                          ('custom', '自定义路径')]

# urlpatterns += [
#     re_path(r'category/(?P<pk>\d+)(?:\.(html|shtml|htm))?/?$',
#             CategoryDetailView.as_view(),
#             name='category-detail'),
#     re_path(r'category/(?P<pk>\d+)_(?P<pagenum>\d+)(?:\.(html|shtml|htm))?/?$',
#             CategoryDetailView.as_view(),
#             name='category-detail'),
#     path('<str:slug>/', CategoryDetailView.as_view(), name='category-detail'),
#     #slug/p_1
#     path('<str:slug>/p_<int:pagenum>',
#          CategoryDetailView.as_view(),
#          name='category-detail'),
#     re_path(r'^(?P<slug>[^/]+)/index\.(html|shtml|htm)',
#             CategoryDetailView.as_view(),
#             name='category-detail'),
#     re_path(r'^(?P<slug>[^/]+)/index_(?P<pagenum>\d+)\.(html|shtml|htm)',
#             CategoryDetailView.as_view(),
#             name='category-detail'),
# ]
