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

from ocyan.core.fender import config
from ocyan.core.plugins import OcyanPluginConfig
from ocyan.core.utils import find_first, merge_dicts

SHOP_BASE_URL = config.get("oscar", "shop_base_url", "shop")
COMPARE_BASE_URL = config.get("oscar_compare", "compase_base_url", "vergelijk")
OSCAR_COMPARE_TEMPLATE_SETTINGS = {
    "OPTIONS": {
        "context_processors": [
            "ocyan.plugin.oscar_compare.context_processors.in_comparison"
        ]
    }
}


class OscarCompareConfig(OcyanPluginConfig):
    def contribute_to_installed_apps(self, installed_apps):
        installed_apps.ocyan.append(
            "ocyan.plugin.oscar_compare", extends="ocyan.plugin.oscar_detail_filters"
        )
        return installed_apps

    def contribute_to_urlpatterns(self, urlpatterns):
        _new_patterns = [
            path(
                "%s/%s/" % (SHOP_BASE_URL, COMPARE_BASE_URL),
                include("ocyan.plugin.oscar_compare.urls", namespace="compare"),
            )
        ]

        if config.i18n_enabled:
            new_patterns = i18n_patterns(*_new_patterns, prefix_default_language=False)
        else:
            new_patterns = _new_patterns

        new_patterns.extend(list(urlpatterns))
        return new_patterns

    def contribute_to_settings(self, settings):
        # find django template settings dict
        django_templates = find_first(
            lambda x: x["BACKEND"].endswith("DjangoTemplates"), settings["TEMPLATES"]
        )

        # configure templates
        merge_dicts(django_templates, OSCAR_COMPARE_TEMPLATE_SETTINGS, overwrite=True)
        return settings
