# -*- coding: utf-8 -*-
from ocyan.core.fender import config
from ocyan.core.plugins import OcyanPluginConfig
from ocyan.core.utils import find_first, merge_dicts

SHOW_BASKET_POPUP_TEMPLATE_SETTINGS = {
    "OPTIONS": {
        "context_processors": [
            "ocyan.plugin.oscar_basket.contextprocessors.show_basket_popup_setting"
        ]
    }
}


class Oscar3BasketConfig(OcyanPluginConfig):
    def contribute_to_settings(self, settings):
        django_templates = find_first(
            lambda x: x["BACKEND"].endswith("DjangoTemplates"), settings["TEMPLATES"]
        )
        merge_dicts(
            django_templates, SHOW_BASKET_POPUP_TEMPLATE_SETTINGS, overwrite=True
        )

        return merge_dicts(
            settings,
            {
                "MIGRATION_MODULES": {
                    "basket": "ocyan.plugin.oscar_basket.oxyan_basket_extension.migrations"
                },
                "SHOW_BASKET_POPUP_MODAL": config.get(
                    "oscar_basket", "show_basket_popup", False
                ),
            },
        )

    def contribute_to_installed_apps(self, installed_apps):
        installed_apps.oscar.append("ocyan.plugin.oscar_basket")
        installed_apps.oscar.append(
            "ocyan.plugin.oscar_basket.basket.apps.BasketConfig",
            replaces="oscar.apps.basket.apps.BasketConfig",
        )
        installed_apps.oscar.register_extension(
            "ocyan.plugin.oscar_basket.oxyan_basket_extension",
            "ocyan.plugin.oscar_basket.basket",
        )
        return installed_apps
