"""
This file contains the code that is the same for oscar 1.6 and oscar 3
"""

from django.conf.urls.i18n import i18n_patterns
from django.urls import path

from ocyan.core.fender import config
from ocyan.core.plugins import OcyanPluginConfig


class SharedOscarEntrypoint(OcyanPluginConfig):
    def get_smoketest_modules(self):
        return [
            "ocyan.plugin.oscar.tests.test_urls",
            "ocyan.plugin.oscar.tests.test_screenshots",
        ]

    def contribute_to_fixtures(self, fixtures):
        fixtures.add("auth", "oscar_demo_data/auth")
        fixtures.add("address", "oscar_demo_data/address")
        fixtures.add("ranges", "oscar_demo_data/ranges")
        return fixtures

    def contribute_to_middleware(self, middleware):
        return middleware + ["oscar.apps.basket.middleware.BasketMiddleware"]

    def get_app_config(self):
        raise NotImplementedError()

    def contribute_to_urlpatterns(self, urlpatterns):
        application = self.get_app_config()

        if config.i18n_enabled:
            # wrap new urls in i18n_patterns but not the old urls (redirects)
            new_patterns = i18n_patterns(
                path("", (application.get_new_urls(), None, None)),
                prefix_default_language=False,
            ) + [path("", (application.get_old_urls(), None, None))]
        else:
            new_patterns = [path("", application.urls)]

        new_patterns.extend(list(urlpatterns))
        return new_patterns

    def contribute_to_i18n_fields(self, fields_dict):
        fields_dict.update(
            {
                "address.country.printable_name": True,
                "catalogue.product.title": True,
                "catalogue.product.slug": True,
                "catalogue.product.description": True,
                "catalogue.productattribute.name": True,
                "catalogue.productattributevalue.value_text": True,
                "catalogue.productattributevalue.value_richtext": True,
                "catalogue.productattributevalue.value_file": True,
                "catalogue.productattributevalue.value_image": True,
                "catalogue.category.name": True,
                "catalogue.category.description": True,
                "catalogue.category.image": True,
                "catalogue.category.slug": True,
                "catalogue.option.name": True,
                "catalogue.attributeoptiongroup.name": True,
                "catalogue.attributeoption.option": True,
                "catalogue.productimage.original": True,
                "catalogue.productimage.caption": True,
                "offer.conditionaloffer.name": True,
                "offer.conditionaloffer.description": True,
                "offer.range.name": True,
                "offer.range.description": True,
                "payment.sourcetype.name": True,
                "shipping.orderanditemcharges.name": True,
                "shipping.orderanditemcharges.description": True,
                "shipping.weightbased.name": True,
                "shipping.weightbased.description": True,
                "voucher.voucher.name": True,
                "voucher.voucherset.name": True,
                "voucher.voucherset.description": True,
            }
        )
        return fields_dict
