from django.urls import include, path, register_converter
from django.utils.translation import gettext_lazy as _
from rest_framework.routers import DefaultRouter

from . import forms, models, views, viewsets
from .converters import DateConverter, PagePathConverter, WeekConverter

__all__ = ["api", "urls"]


register_converter(PagePathConverter, "page_path")
register_converter(DateConverter, "date")
register_converter(WeekConverter, "week")


# urls = [
#    path('on_air', views.on_air, name='aircox.on_air'),
#    path('monitor', views.Monitor.as_view(), name='aircox.monitor'),
#    path('stats', views.StatisticsView.as_view(), name='aircox.stats'),
# ]


router = DefaultRouter()
router.register("images", viewsets.ImageViewSet, basename="image")
router.register("sound", viewsets.SoundViewSet, basename="sound")
router.register("track", viewsets.TrackROViewSet, basename="track")


api = [
    path("logs/", views.LogListAPIView.as_view(), name="live"),
    path(
        "user/settings/",
        viewsets.UserSettingsViewSet.as_view({"get": "retrieve", "post": "update", "put": "update"}),
        name="user-settings",
    ),
] + router.urls


urls = [
    path("", views.HomeView.as_view(), name="home"),
    path("api/", include((api, "aircox"), namespace="api")),
    # ---- ---- objects views
    # ---- articles
    path(
        _("articles/<slug:slug>/"),
        views.ArticleDetailView.as_view(),
        name="article-detail",
    ),
    path(
        _("articles/"),
        views.ArticleListView.as_view(model=models.Article),
        name="article-list",
    ),
    path(
        _("articles/c/<slug:category_slug>/"),
        views.ArticleListView.as_view(model=models.Article),
        name="article-list",
    ),
    # ---- timetable
    path(_("timetable/"), views.TimeTableView.as_view(), name="timetable-list"),
    path(
        _("timetable/<date:date>/"),
        views.TimeTableView.as_view(),
        name="timetable-list",
    ),
    # ---- pages
    path(
        _("publications/"),
        views.PageListView.as_view(model=models.Page, attach_to_value=models.StaticPage.Target.PAGES),
        name="page-list",
    ),
    path(
        _("publications/c/<slug:category_slug>"),
        views.PageListView.as_view(model=models.Page, attach_to_value=models.StaticPage.Target.PAGES),
        name="page-list",
    ),
    path(
        _("pages/<slug:slug>/"),
        views.BasePageDetailView.as_view(
            model=models.StaticPage,
            queryset=models.StaticPage.objects.filter(attach_to__isnull=True),
        ),
        name="static-page-detail",
    ),
    path(
        _("pages/"),
        views.BasePageListView.as_view(
            model=models.StaticPage,
            queryset=models.StaticPage.objects.filter(attach_to__isnull=True),
        ),
        name="static-page-list",
    ),
    # ---- programs
    path(_("programs/"), views.ProgramListView.as_view(), name="program-list"),
    path(_("programs/c/<slug:category_slug>/"), views.ProgramListView.as_view(), name="program-list"),
    path(
        _("programs/<slug:slug>"),
        views.ProgramDetailView.as_view(),
        name="program-detail",
    ),
    path(_("programs/<slug:parent_slug>/articles"), views.ArticleListView.as_view(), name="article-list"),
    path(_("programs/<slug:parent_slug>/podcasts"), views.PodcastListView.as_view(), name="podcast-list"),
    path(_("programs/<slug:parent_slug>/episodes"), views.EpisodeListView.as_view(), name="episode-list"),
    path(_("programs/<slug:parent_slug>/diffusions"), views.DiffusionListView.as_view(), name="diffusion-list"),
    path(
        _("programs/<slug:parent_slug>/publications"),
        views.PageListView.as_view(model=models.Page, attach_to_value=models.StaticPage.Target.PAGES),
        name="page-list",
    ),
    # ---- episodes
    path(_("programs/episodes/"), views.EpisodeListView.as_view(), name="episode-list"),
    path(_("programs/episodes/c/<slug:category_slug>"), views.EpisodeListView.as_view(), name="episode-list"),
    path(
        _("programs/episodes/<slug:slug>"),
        views.EpisodeDetailView.as_view(),
        name="episode-detail",
    ),
    path(
        _("programs/episodes/<pk>/edit/"),
        views.EpisodeUpdateView.as_view(),
        name="episode-edit",
    ),
    path(_("podcasts/"), views.PodcastListView.as_view(), name="podcast-list"),
    path(_("podcasts/c/<slug:category_slug>/"), views.PodcastListView.as_view(), name="podcast-list"),
    # ---- others
    path(
        _("program/<pk>/edit/"),
        views.ProgramUpdateView.as_view(),
        name="program-edit",
    ),
    path(
        "errors/no-station",
        views.errors.NoStationErrorView.as_view(),
        name="errors-no-station",
    ),
    # ---- backoffice
    path(_("edit/"), views.ProfileView.as_view(), name="profile"),
    path(
        _("edit/programs/<slug:slug>"),
        views.PageUpdateView.as_view(model=models.Program, form_class=forms.ProgramForm),
        name="program-update",
    ),
    path("accounts/profile/", views.ProfileView.as_view(), name="profile"),
]
