# -*- 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.utils import merge_dicts
from ocyan.plugin.google.constants import CAPTCHA_ENABLED
from ocyan.plugin.wagtail.block_plugin import BlockPluginConfig

from .constants import APP_CODE

SHOP_BASE_URL = config.get("oscar", "shop_base_url", "shop")


class ContactFormConfig(BlockPluginConfig):
    def contribute_to_installed_apps(self, installed_apps):
        installed_apps.apps.append("phonenumber_field")
        installed_apps.ocyan.append("ocyan.plugin.contact_form")
        installed_apps.wagtail.register_extension(
            "ocyan.plugin.contact_form.contact_form_coyote_extension",
            "ocyan.plugin.coyote",
        )

        if CAPTCHA_ENABLED:
            installed_apps.apps.append("django_recaptcha")

        return installed_apps

    def contribute_to_urlpatterns(self, urlpatterns):
        if config.i18n_enabled:
            return (
                i18n_patterns(
                    path(
                        r"%s/" % SHOP_BASE_URL,
                        include("ocyan.plugin.contact_form.urls", namespace=APP_CODE),
                    ),
                    prefix_default_language=False,
                )
                + urlpatterns
            )

        return [
            path(
                r"%s/" % SHOP_BASE_URL,
                include("ocyan.plugin.contact_form.urls", namespace=APP_CODE),
            )
        ] + urlpatterns

    def contribute_to_blocks(self, blocks):
        from .blocks import ContactFormBlock

        return blocks + [("Contact", ContactFormBlock())]

    def contribute_to_settings(self, settings):
        CONTACT_FORM_SETTINGS = {
            "CONTACT_REDIRECT_URL": "/",
        }

        return merge_dicts(settings, CONTACT_FORM_SETTINGS)

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