# -*- coding: utf-8 -*-
from django.urls import include, path
from django.conf.urls.i18n import i18n_patterns

from ocyan.plugin.oscar_checkout.plugins import PaymentPluginConfig
from ocyan.core.fender import config

from .constants import APP_CODE, AVAILABLE_PAYMENT_METHODS, PRETTY_PAYMENT_METHOD_NAMES

SHOP_BASE_URL = config.get("oscar", "shop_base_url", "shop")


class OcyanInvoicePaymentPluginConfig(PaymentPluginConfig):
    def contribute_to_installed_apps(self, installed_apps):
        installed_apps.ocyan.append("ocyan.plugin.payment_invoice")
        return installed_apps

    def contribute_to_payment_methods(self, payment_methods):
        return payment_methods + [("%s:%s" % (APP_CODE, method), PRETTY_PAYMENT_METHOD_NAMES.get(method)) for method in AVAILABLE_PAYMENT_METHODS]

    def contribute_to_urlpatterns(self, urlpatterns):
        if config.i18n_enabled:
            new_patterns = i18n_patterns(
                path(
                    r"%s/" % SHOP_BASE_URL,
                    include("ocyan.plugin.payment_invoice.urls", namespace=APP_CODE),
                ),
                prefix_default_language=False,
            )
        else:
            new_patterns = [
                path(
                    r"%s/" % SHOP_BASE_URL,
                    include("ocyan.plugin.payment_invoice.urls", namespace=APP_CODE),
                )
            ]
        new_patterns.extend(list(urlpatterns))
        return new_patterns
