from django.urls import include, path

from ocyan.core.fender import config
from ocyan.core.utils import merge_dicts
from ocyan.plugin.wagtail.block_plugin import BlockPluginConfig

from .sassfunctions import (
    ocyanbarenumber,
    ocyancolor,
    ocyanlist,
    ocyannumber,
    ocyanvariable,
)

OXYAN_SETTINGS = {
    "PASSWORD_REQUIRED_TEMPLATE": "oxyan/privacy/password_required.html",
    "WAGTAIL_FRONTEND_LOGIN_TEMPLATE": "oxyan/privacy/login_required.html",
    "OXYAN_THEME": config.get("themes", "theme", "default"),
    "OXYAN_THEME_MIXINS": config.get("themes", "mixins", []),
}


class OxyanThemesPluginConfig(BlockPluginConfig):
    def contribute_to_installed_apps(self, installed_apps):
        installed_apps.front.append("oxyan.themes")
        return installed_apps

    def contribute_to_sass_functions(self, sass_functions):
        return merge_dicts(
            sass_functions,
            {
                "ocyanvariable": ocyanvariable,
                "ocyanlist": ocyanlist,
                "ocyannumber": ocyannumber,
                "ocyanbarenumber": ocyanbarenumber,
                "ocyancolor": ocyancolor,
            },
        )

    def contribute_to_settings(self, settings):
        return merge_dicts(settings, OXYAN_SETTINGS, overwrite=True)

    def contribute_to_urlpatterns(self, urlpatterns):
        new_patterns = [
            path("themes/", include("oxyan.themes.urls")),
        ]
        new_patterns.extend(list(urlpatterns))
        return new_patterns

    def contribute_to_maintenance(self, maintenances):
        return maintenances + [
            ("remove_ocyan", "ocyan.plugin.themes.maintenance.remove_ocyan"),
        ]
