from django.urls import include, path

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


class DjangoConfig(OcyanPluginConfig):
    def contribute_to_settings(self, settings):
        settings["STORAGES"]["staticfiles"] = {
            "BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage"
        }

        DJANGO_FIXED_SETTINGS = {
            "TIME_ZONE": config.get("django", "time_zone", "Europe/Amsterdam"),
            "EMAIL_HOST": config.get("django", "email_host"),
            "EMAIL_PORT": config.get("django", "email_port"),
            "EMAIL_HOST_USER": config.get("django", "email_host_user"),
            "EMAIL_HOST_PASSWORD": config.get("django", "email_host_password"),
            "EMAIL_USE_TLS": config.get("django", "email_use_tls"),
            "EMAIL_TIMEOUT": 30,  # if we cannot send an email do continue to process orders
            "DATA_UPLOAD_MAX_NUMBER_FIELDS": settings.get(
                "DATA_UPLOAD_MAX_NUMBER_FIELDS", 9999
            ),
            "AUTH_PASSWORD_VALIDATORS": [
                {
                    "NAME": "ocyan.plugin.django.password_validation.SpecialCharactersValidation"
                },
            ],
        }
        email_to = config.get("django", "email_to")
        DJANGO_OVERWRITABLE_SETTINGS = {
            "SERVER_EMAIL": config.get("django", "email_from"),
            "DEFAULT_FROM_EMAIL": config.get("django", "email_from"),
            "DEFAULT_TO_EMAIL": email_to if isinstance(email_to, list) else [email_to],
            "FILE_CHARSET": "utf-8",
            "DEFAULT_AUTO_FIELD": "django.db.models.AutoField",
        }

        fixed_settings = merge_dicts(settings, DJANGO_FIXED_SETTINGS, overwrite=True)
        return merge_dicts(
            fixed_settings, DJANGO_OVERWRITABLE_SETTINGS, overwrite=False
        )

    def contribute_to_middleware(self, middleware):
        return [
            "django.middleware.security.SecurityMiddleware",
            "whitenoise.middleware.WhiteNoiseMiddleware",
        ] + middleware

    def contribute_to_installed_apps(self, installed_apps):
        installed_apps.ocyan.append(
            "ocyan.plugin.django.apps.OcyanPluginDjangoAppConfig"
        )
        installed_apps.django.append("ocyan.plugin.django.compat")
        installed_apps.django.append("ocyan.plugin.django.admin_extension")
        return installed_apps

    def contribute_to_urlpatterns(self, urlpatterns):
        new_patterns = [
            path("i18n/", include("django.conf.urls.i18n")),
            path("errors/", include("ocyan.plugin.django.urls")),
        ]
        new_patterns.extend(list(urlpatterns))
        return new_patterns

    def contribute_to_fixtures(self, fixtures):
        fixtures.add("sites", "django/sites")
        fixtures.add("auth", "django/auth")
        return fixtures

    def get_smoketest_modules(self):
        return [
            "ocyan.plugin.django.tests.test_smoketests",
            "ocyan.plugin.django.tests.test_screenshots",
        ]

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