# -*- coding: utf-8 -*-
from django.utils.text import slugify

from ocyan.core.fender import config
from ocyan.core.utils import merge_dicts
from ocyan.plugin.oscar.plugins import OscarPluginConfig

from .utils import get_facet_config

DEFAULT_ITEMS_PER_PAGE = config.get("oscar", "dashboard_items_per_page", 21)
PROJECT_NAME = config.get("django", "name")


class OscarElasticsearchConfig(OscarPluginConfig):
    def contribute_to_core_apps(self, core_apps):
        return core_apps + ["ocyan.plugin.oscar_elasticsearch.search"]

    def contribute_to_installed_apps(self, installed_apps):
        installed_apps.oscar.append(
            "oscar_elasticsearch.search", replaces="oscar.apps.search"
        )
        installed_apps.oscar.append(
            "ocyan.plugin.oscar_elasticsearch.search",
            replaces="oscar_elasticsearch.search",
        )
        installed_apps.oscar.append(
            "ocyan.plugin.oscar_elasticsearch.catalogue_es_extension",
            extends="ocyan.plugin.oscar_catalogue.catalogue",
        )
        installed_apps.oscar.register_extension(
            "ocyan.plugin.oscar_elasticsearch.catalogue_dashboard_es_extension.dashboard.catalogue",
            "ocyan.plugin.oscar_catalogue_dashboard.dashboard.catalogue",
        )
        installed_apps.wagtail.append("wagtail.contrib.search_promotions")
        return installed_apps

    def contribute_to_settings(self, settings):
        OSCAR_ELASTICSEARCH_OVERWRITE_SETTINGS = {
            "OSCAR_ELASTICSEARCH_QUERY_PAGE_SIZE": config.get(
                "oscar_elasticsearch", "query_page_size", 100
            ),
            "OSCAR_ELASTICSEARCH_FACETS": get_facet_config(),
            "OSCAR_ELASTICSEARCH_MIN_NUM_BUCKETS": config.get(
                "oscar_elasticsearch", "min_num_buckets", 2
            ),
            "OSCAR_ELASTICSEARCH_FILTER_AVAILABLE": config.get(
                "oscar_elasticsearch", "filter_available", False
            ),
            "OSCAR_ELASTICSEARCH_PRIORITIZE_AVAILABLE_PRODUCTS": config.get(
                "oscar_elasticsearch", "prioritize_available_products", True
            ),
            "OSCAR_ELASTICSEARCH_DEFAULT_ITEMS_PER_PAGE": DEFAULT_ITEMS_PER_PAGE,
            "OSCAR_ELASTICSEARCH_ITEMS_PER_PAGE_CHOICES": config.get(
                "oscar_elasticsearch",
                "items_per_page_choices",
                [DEFAULT_ITEMS_PER_PAGE],
            ),
            "OSCAR_ELASTICSEARCH_AUTOCOMPLETE_CONTEXTS": [
                {
                    "name": "status",
                    "type": "category",
                    "path": "status",
                }
            ],
            "OSCAR_ELASTICSEARCH_INDEX_UPDATE_SPOOLER_CHUNK_SIZE": 1000,
            "OSCAR_ELASTICSEARCH_INDEX_PREFIX": slugify(PROJECT_NAME),
            "OSCAR_ELASTICSEARCH_FACET_BUCKET_SIZE": config.get(
                "oscar_elasticsearch", "facet_bucket_size", 10
            ),
            "OSCAR_ELASTICSEARCH_HIDE_IMAGELESS_PRODUCTS": config.get(
                "oscar_elasticsearch", "hide_imageless_products", False
            ),
            "OSCAR_ELASTICSEARCH_PRODUCTS_WITH_IMAGES_FIRST": config.get(
                "oscar_elasticsearch", "products_with_images_first", False
            ),
        }

        return merge_dicts(
            settings, OSCAR_ELASTICSEARCH_OVERWRITE_SETTINGS, overwrite=True
        )

    def get_spooler_modules(self, modules):
        modules.append("ocyan.plugin.oscar_elasticsearch.search.spooler")
        return modules

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


class Oscar3ElasticsearchConfig(OscarElasticsearchConfig):
    def contribute_to_installed_apps(self, installed_apps):
        installed_apps.oscar.append(
            "ocyan.plugin.oscar_elasticsearch.search.apps.OscarElasticSearchConfig",
            replaces="oscar.apps.search.apps.SearchConfig",
        )
        installed_apps.oscar.register_extension(
            "oscar_elasticsearch.search", "oscar.apps.search"
        )
        installed_apps.oscar.register_extension(
            "ocyan.plugin.oscar_elasticsearch.search", "oscar_elasticsearch.search"
        )
        installed_apps.oscar.append(
            "ocyan.plugin.oscar_elasticsearch.catalogue_es_extension",
            extends="ocyan.plugin.oscar_catalogue.catalogue",
        )
        installed_apps.oscar.register_extension(
            "ocyan.plugin.oscar_elasticsearch.catalogue_dashboard_es_extension.dashboard.catalogue",
            "ocyan.plugin.oscar_catalogue_dashboard.dashboard.catalogue",
        )
        installed_apps.wagtail.append("wagtail.contrib.search_promotions")
        return installed_apps
