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

from ocyan.core.fender import config
from ocyan.core.utils import merge_dicts

from ocyan.plugin.oscar_checkout.settings import (
    OCYAN_CHECKOUT_OSCAR_SETTINGS,
    OCYAN_REST_FRAMEWORK_SETTINGS,
)


class CheckoutEntryPoint:
    def contribute_to_settings(self, settings):
        settings = merge_dicts(
            settings, OCYAN_REST_FRAMEWORK_SETTINGS, deduplicate_iterables=True
        )
        return merge_dicts(settings, OCYAN_CHECKOUT_OSCAR_SETTINGS, overwrite=True)

    def contribute_to_middleware(self, middleware):
        middlewares = middleware + ["oscarapi.middleware.ApiBasketMiddleWare"]
        try:
            middlewares.remove("oscar.apps.basket.middleware.BasketMiddleware")
        except ValueError:
            pass
        return middlewares

    def get_api_application(self):
        raise NotImplementedError

    def contribute_to_urlpatterns(self, urlpatterns):
        api = self.get_api_application()

        if config.i18n_enabled:
            urls = i18n_patterns(
                path(r"checkout/api/", api.urls), prefix_default_language=False
            )
        else:
            urls = [path(r"checkout/api/", api.urls)]
        urls.extend(list(urlpatterns))
        return urls

    def get_smoketest_modules(self):
        return ["ocyan.plugin.oscar_checkout.tests.test_smoketests"]

    def contribute_to_i18n_fields(self, fields_dict):
        fields_dict.update(
            {
                "checkout.paymentmethod.label": True,
                "checkout.percentagesurcharge.name": True,
                "checkout.fixedsurcharge.name": True,
            }
        )
        return fields_dict
