from django.urls import reverse_lazy

from oscar import defaults

from ocyan.core.fender import config
from ocyan.core.utils import module_to_dict

from .pipeline import PIPELINE_STATUS_NEW, format_pipeline


def configure_oscar_navigation(settings):
    OSCAR_DASHBOARD_NAVIGATION = settings["OSCAR_DASHBOARD_NAVIGATION"]

    # remove pages menu
    del OSCAR_DASHBOARD_NAVIGATION[5]["children"][0]

    if not config.get("shipping", "enable_charged_shipping", False):
        # enable shipping only when charged shipping is not enabled.
        # the shipping dashboard is for weight based shipping only.
        OSCAR_DASHBOARD_NAVIGATION[2]["children"].append(
            {"label": "Shipping charges", "url_name": "dashboard:shipping-method-list"}
        )
    return settings


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

OXYAN_TEMPLATE_SETTINGS = {
    "OPTIONS": {
        "context_processors": [
            "ocyan.plugin.oscar.context_processors.search_form",
            "oscar.apps.checkout.context_processors.checkout",
            "oscar.apps.communication.notifications.context_processors.notifications",
            "oscar.core.context_processors.metadata",
        ]
    }
}

OSCAR_DJANGO_CONFIGURATION = {
    "AUTHENTICATION_BACKENDS": "oscar.apps.customer.auth_backends.EmailBackend",
    "HAYSTACK_CONNECTIONS": {
        "default": {"ENGINE": "haystack.backends.simple_backend.SimpleEngine"}
    },
    "LOGIN_URL": reverse_lazy("customer:login"),
}

OSCAR_HIDDEN_FEATURES = []
if not config.get("oscar", "enable_reviews", False):
    OSCAR_HIDDEN_FEATURES.append("reviews")
if not config.get("oscar", "enable_wishlist", False):
    OSCAR_HIDDEN_FEATURES.append("wishlists")
# pylint: disable=no-member
if "ocyan.plugin.oscar_extradiscounts" not in config.ocyan_plugins:
    OSCAR_HIDDEN_FEATURES.append("purchaseunion")

OSCAR_STATUS_PIPELINE, OSCAR_LINE_STATUS_PIPELINE = format_pipeline(
    config.get("oscar", "order_pipeline", [])
)
OSCAR_ORDER_STATUS_CASCADE = {status: status for status in OSCAR_STATUS_PIPELINE.keys()}

OSCAR_OCYAN_CONFIGURATION = dict(
    configure_oscar_navigation(module_to_dict(defaults)),
    OSCAR_PROMOTIONS_ENABLED=True,
    OSCAR_EAGER_ALERTS=False,
    OSCAR_DELETE_IMAGE_FILES=False,
    OSCAR_HIDDEN_FEATURES=OSCAR_HIDDEN_FEATURES,
    OSCAR_DYNAMIC_CLASS_LOADER="ocyan.main.loading.ocyan_class_loader",
    OSCAR_FROM_EMAIL=config.get("django", "email_from", "oscar@example.com"),
    OSCAR_SHOP_NAME=config.get("django", "description", "Oscar shop"),
    OSCAR_SHOP_TAGLINE=config.get("django", "description", ""),
    OSCAR_PRODUCTS_PER_PAGE=config.get("oscar", "dashboard_items_per_page", 21),
    OSCAR_DASHBOARD_ITEMS_PER_PAGE=config.get("oscar", "dashboard_items_per_page", 21),
    OSCAR_INITIAL_ORDER_STATUS=PIPELINE_STATUS_NEW,
    OSCAR_INITIAL_LINE_STATUS=PIPELINE_STATUS_NEW,
    OSCAR_ORDER_STATUS_PIPELINE=OSCAR_STATUS_PIPELINE,
    OSCAR_LINE_STATUS_PIPELINE=OSCAR_LINE_STATUS_PIPELINE,
    OSCAR_ORDER_STATUS_CASCADE=OSCAR_ORDER_STATUS_CASCADE,
    OSCAR_MODERATE_REVIEWS=config.get("oscar", "moderate_reviews", False),
    OSCAR_ALLOW_ANON_CHECKOUT=config.get("oscar", "allow_anon_checkout", False),
    OSCAR_DEFAULT_CURRENCY=config.get("oscar", "default_currency", "EUR"),
    OSCAR_OFFERS_INCL_TAX=True,
    OSCAR_VAT_COUNTRY_OF_ORIGIN_CODE=config.get("oscar", "vat_country_of_origin", "NL"),
    OSCAR_HOMEPAGE="/",
)
