from django.conf import settings
from django.urls import path, re_path
from django.utils.translation import gettext, override
from django.views.generic.base import RedirectView

from ocyan.plugin.oscar_catalogue.catalogue.shared.views import (
    CatalogueIndexRedirectView,
    CategoryRedirectView,
)
from ocyan.plugin.oscar_catalogue.constants import SLUG_ID_SEPARATOR


def get_categories_as_disjunction():
    unique_categories = set(["category"])
    if len(settings.LANGUAGES) < 20:
        for lang in dict(settings.LANGUAGES).keys():
            with override(lang):
                unique_categories.add(gettext("category/")[:-1])

    return "|".join(unique_categories)


class CatalogueAppConfigMixin:
    """
    Because we're supporting <2.0 and >2.0 of oscar,
    we have to deal with their django app config implementation introduced in 2.0.

    In this mixin we define the urls to avoid duplicate code.
    """

    def new_urls(self):
        return [
            path(
                "<slug:product_slug>%s<int:pk>/" % SLUG_ID_SEPARATOR,
                self.detail_view.as_view(),
                name="detail",
            ),
            path(gettext("category/"), self.catalogue_view.as_view(), name="index"),
            re_path(
                r"^category/(?P<category_slug>[\w-]+(/[\w-]+)*){SLUG_ID_SEPARATOR}(?P<pk>\d+)/$".format(
                    SLUG_ID_SEPARATOR=SLUG_ID_SEPARATOR,
                ),
                self.category_view.as_view(),
                name="category",
            ),
            re_path(
                r"^({CATEGORIES_AS_DISJUNCTION})/(?P<category_slug>[\w-]+(/[\w-]+)*){SLUG_ID_SEPARATOR}(?P<pk>\d+)/$".format(
                    CATEGORIES_AS_DISJUNCTION=get_categories_as_disjunction(),
                    SLUG_ID_SEPARATOR=SLUG_ID_SEPARATOR,
                ),
                CategoryRedirectView.as_view(),
                name="redirect_category",
            ),
            re_path(
                r"^({CATEGORIES_AS_DISJUNCTION})/$".format(
                    CATEGORIES_AS_DISJUNCTION=get_categories_as_disjunction()
                ),
                CatalogueIndexRedirectView.as_view(),
                name="redirect_browse",
            ),
            path("ranges/<slug:slug>/", self.range_view.as_view(), name="range"),
        ]

    def old_urls(self):
        if SLUG_ID_SEPARATOR == "_":
            return []
        else:
            return [
                path(
                    "<slug:product_slug>_<int:pk>/",
                    RedirectView.as_view(
                        pattern_name="catalogue:detail",
                        permanent=True,
                        query_string=True,
                    ),
                ),
                re_path(
                    r"^category/(?P<category_slug>[\w-]+(/[\w-]+)*)_(?P<pk>\d+)/$",
                    RedirectView.as_view(
                        pattern_name="catalogue:category",
                        permanent=True,
                        query_string=True,
                    ),
                ),
            ]
