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

from .constants import CAPTCHA_ENABLED, RECAPTCHA_PRIVATE_KEY, RECAPTCHA_PUBLIC_KEY
from .runtime import is_webshop


class GoogleEntrypoint(OcyanPluginConfig):
    def contribute_to_fixtures(self, fixtures):
        # these fixtures contain webshop specific data, don't include them
        # in barebone projectss
        if is_webshop():
            fixtures.add("google_catalogue", "google/google_catalogue")
        return fixtures

    def contribute_to_settings(self, settings):
        settings = merge_dicts(
            settings,
            {"SILENCED_SYSTEM_CHECKS": ["django_recaptcha.recaptcha_test_key_error"]},
        )
        if CAPTCHA_ENABLED:
            return merge_dicts(
                settings,
                {
                    "RECAPTCHA_PRIVATE_KEY": RECAPTCHA_PRIVATE_KEY,
                    "RECAPTCHA_PUBLIC_KEY": RECAPTCHA_PUBLIC_KEY,
                    "RECAPTCHA_REQUIRED_SCORE": 0.85,
                },
            )
        return settings

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


class GoogleConfig(GoogleEntrypoint):
    def contribute_to_installed_apps(self, installed_apps):
        installed_apps.ocyan.append("ocyan.plugin.google")
        if CAPTCHA_ENABLED:
            installed_apps.apps.append("django_recaptcha")

        installed_apps.oscar.register_extension(
            "ocyan.plugin.google.google_catalogue_review_extension.reviews",
            "oscar.apps.catalogue.reviews",
        )
        installed_apps.oscar.register_extension(
            "ocyan.plugin.google.google_customer_extension",
            "ocyan.plugin.oscar_communications.customer_extension",
        )
        return installed_apps


class Oscar3GoogleConfig(GoogleEntrypoint):
    def contribute_to_installed_apps(self, installed_apps):
        installed_apps.ocyan.append("ocyan.plugin.google")
        if CAPTCHA_ENABLED:
            installed_apps.apps.append("django_recaptcha")
        installed_apps.oscar.register_extension(
            "ocyan.plugin.google.google_oxyan_catalogue_review_extension",
            "oscar.apps.catalogue",
        )
        installed_apps.oscar.register_extension(
            "ocyan.plugin.google.google_customer_extension",
            "ocyan.plugin.oscar_communications.customer_extension",
        )
        return installed_apps
