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

from . import 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("user", viewsets.UserViewSet, basename="user")
router.register("group", viewsets.GroupViewSet, basename="group")
router.register("usergroup", viewsets.UserGroupViewSet, basename="usergroup")

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


api = [
    path("logs/", views.log.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.home.HomeView.as_view(), name="home"),
    path("api/", include((api, "aircox"), namespace="api")),
    # ---- ---- objects views
    # ---- articles
    path(
        _("articles/<slug:slug>/"),
        views.article.ArticleDetailView.as_view(),
        name="article-detail",
    ),
    path(
        _("articles/"),
        views.article.ArticleListView.as_view(model=models.article.Article),
        name="article-list",
    ),
    path(
        _("articles/c/<slug:category_slug>/"),
        views.article.ArticleListView.as_view(model=models.article.Article),
        name="article-list",
    ),
    # ---- timetable
    path(_("timetable/"), views.diffusion.TimeTableView.as_view(), name="timetable-list"),
    path(
        _("timetable/<date:date>/"),
        views.diffusion.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.program.ProgramListView.as_view(), name="program-list"),
    path(_("programs/c/<slug:category_slug>/"), views.program.ProgramListView.as_view(), name="program-list"),
    path(
        _("programs/<slug:slug>/"),
        views.program.ProgramDetailView.as_view(),
        name="program-detail",
    ),
    path(_("programs/<slug:parent_slug>/articles/"), views.article.ArticleListView.as_view(), name="article-list"),
    path(_("programs/<slug:parent_slug>/podcasts/"), views.episode.PodcastListView.as_view(), name="podcast-list"),
    path(_("programs/<slug:parent_slug>/episodes/"), views.episode.EpisodeListView.as_view(), name="episode-list"),
    path(
        _("programs/<slug:parent_slug>/diffusions/"), views.diffusion.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.episode.EpisodeListView.as_view(), name="episode-list"),
    path(_("programs/episodes/c/<slug:category_slug>/"), views.episode.EpisodeListView.as_view(), name="episode-list"),
    path(
        _("programs/episodes/<slug:slug>/"),
        views.episode.EpisodeDetailView.as_view(),
        name="episode-detail",
    ),
    path(_("podcasts/"), views.episode.PodcastListView.as_view(), name="podcast-list"),
    path(_("podcasts/c/<slug:category_slug>/"), views.episode.PodcastListView.as_view(), name="podcast-list"),
    # ---- dashboard
    path(_("dashboard/"), views.dashboard.DashboardView.as_view(), name="dashboard"),
    path(_("dashboard/program/<pk>/"), views.program.ProgramUpdateView.as_view(), name="program-edit"),
    path(_("dashboard/episodes/<pk>/"), views.episode.EpisodeUpdateView.as_view(), name="episode-edit"),
    path(_("dashboard/statistics/"), views.dashboard.StatisticsView.as_view(), name="dashboard-statistics"),
    path(_("dashboard/statistics/<date:date>/"), views.dashboard.StatisticsView.as_view(), name="dashboard-statistics"),
    path(_("dashboard/users/"), views.auth.UserListView.as_view(), name="user-list"),
    # ---- others
    path(_("errors/no-station/"), views.errors.NoStationErrorView.as_view(), name="errors-no-station"),
]
