from django.conf import settings
from django.urls import path, re_path, reverse_lazy
from django.views.generic.base import RedirectView

from oscar.views.decorators import login_forbidden

from ocyan.plugin.oscar.settings import SHOP_BASE_URL
from ocyan.plugin.oscar.views import RedirectOldApp, RedirectOldURL


class OscarAppConfigMixin:
    def get_old_urls(self):
        urls = [
            path(
                "%s/catalogue/" % SHOP_BASE_URL,
                RedirectView.as_view(pattern_name="catalogue:index"),
            ),
            re_path(
                r"%s/catalogue/(?P<path>.*)" % SHOP_BASE_URL,
                RedirectOldURL.as_view(url="/%(path)s"),
            ),
            re_path(
                r"%s/dashboard/(?P<path>.*)" % SHOP_BASE_URL,
                RedirectOldURL.as_view(url="/admin/shop/%(path)s"),
            ),
            re_path(
                r"%s/(?P<app_base_url>(basket|checkout|accounts|search|offers))/(?P<path>.*)"
                % SHOP_BASE_URL,
                RedirectOldApp.as_view(),
            ),
            path(
                "%s/password-reset/" % SHOP_BASE_URL,
                RedirectView.as_view(pattern_name="password-reset", permanent=True),
            ),
            path(
                "%s/password-reset/done/" % SHOP_BASE_URL,
                RedirectView.as_view(
                    pattern_name="password-reset-done", permanent=True
                ),
            ),
            re_path(
                r"^%s/password-reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$"
                % SHOP_BASE_URL,
                RedirectView.as_view(
                    pattern_name="password-reset-confirm", permanent=True
                ),
            ),
            path(
                "%s/password-reset/complete/" % SHOP_BASE_URL,
                RedirectView.as_view(
                    pattern_name="password-reset-complete", permanent=True
                ),
            ),
        ]
        if SHOP_BASE_URL != "shop":
            urls += [
                path(
                    "shop/catalogue/",
                    RedirectView.as_view(pattern_name="catalogue:index"),
                ),
                re_path(
                    r"shop/catalogue/(?P<path>.*)",
                    RedirectOldURL.as_view(url="/%(path)s"),
                ),
                re_path(
                    r"shop/dashboard/(?P<path>.*)",
                    RedirectOldURL.as_view(url="/admin/shop/%(path)s"),
                ),
                re_path(
                    r"shop/(?P<app_base_url>(basket|checkout|accounts|search|dashboard|offers))/(?P<path>.*)",
                    RedirectOldApp.as_view(),
                ),
                path(
                    "shop/password-reset/",
                    RedirectView.as_view(pattern_name="password-reset", permanent=True),
                ),
                path(
                    "shop/password-reset/done/",
                    RedirectView.as_view(
                        pattern_name="password-reset-done", permanent=True
                    ),
                ),
                re_path(
                    r"^shop/password-reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$",
                    RedirectView.as_view(
                        pattern_name="password-reset-confirm", permanent=True
                    ),
                ),
                path(
                    "shop/password-reset/complete/",
                    RedirectView.as_view(
                        pattern_name="password-reset-complete", permanent=True
                    ),
                ),
            ]

        return urls

    def get_new_urls(self):
        from django.contrib.auth import views as auth_views

        urls = [
            path("", self.catalogue_app.urls),
            path("basket/", self.basket_app.urls),
            path("checkout/", self.checkout_app.urls),
            path("accounts/", self.customer_app.urls),
            path("search/", self.search_app.urls),
            path("admin/shop/", self.dashboard_app.urls),
            path("offers/", self.offer_app.urls),
            # Password reset - as we're using Django's default view functions,
            # we can't namespace these urls as that prevents
            # the reverse function from working.
            path(
                "password-reset/",
                login_forbidden(
                    auth_views.PasswordResetView.as_view(
                        form_class=self.password_reset_form,
                        success_url=reverse_lazy("password-reset-done"),
                    )
                ),
                name="password-reset",
            ),
            path(
                "password-reset/done/",
                login_forbidden(auth_views.PasswordResetDoneView.as_view()),
                name="password-reset-done",
            ),
            re_path(
                r"^password-reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$",
                login_forbidden(
                    auth_views.PasswordResetConfirmView.as_view(
                        form_class=self.set_password_form,
                        success_url=reverse_lazy("password-reset-complete"),
                    )
                ),
                name="password-reset-confirm",
            ),
            path(
                "password-reset/complete/",
                login_forbidden(auth_views.PasswordResetCompleteView.as_view()),
                name="password-reset-complete",
            ),
        ]

        urls.append(path("%s/wishlists/" % SHOP_BASE_URL, self.wishlists_app.urls))

        if settings.OSCAR_PROMOTIONS_ENABLED:
            urls.append(path("promotions/", self.promotions_app.urls))
        return urls

    def get_urls(self):
        return self.get_new_urls() + self.get_old_urls()

    @property
    def urls(self):
        return self.get_urls(), None, None
