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

OSCAR_ADDRESS_HASH_FIELDS = [
    "salutation",
    "line1",
    "house_number",
    "line2",
    "line3",
    "line4",
    "postcode",
    "state",
    "country",
    "email",
]

OSCAR_ADDRESS_BASE_FIELD_ORDER = []

OSCAR_ADDRESS_FORM_FIELD_ORDER = []

OSCAR_ADDRESS_FORM_FIELDS = [
    "email",
    "title",
    "first_name",
    "last_name",
    "line1",
    "house_number",
    "line2",
    "line3",
    "line4",
    "postcode",
    "state",
    "country",
    "phone_number",
]


class OscarAddressConfig(OcyanPluginConfig):
    def contribute_to_installed_apps(self, installed_apps):
        installed_apps.ocyan.append("ocyan.plugin.oscar_address")
        installed_apps.oscar.append(
            "ocyan.plugin.oscar_address.address", replaces="oscar.apps.address"
        )
        installed_apps.oscar.append(
            "ocyan.plugin.oscar_address.dashboard_orders",
            extends="oscar.apps.dashboard.orders",
        )
        return installed_apps

    def contribute_to_settings(self, settings):
        return merge_dicts(
            settings,
            {
                # try to use OSCAR_ADDRESS_FORM_FIELD_ORDER before you use
                # OSCAR_ADDRESS_FORM_FIELDS!
                "OSCAR_ADDRESS_HASH_FIELDS": OSCAR_ADDRESS_HASH_FIELDS,
                "OSCAR_ADDRESS_FORM_FIELD_ORDER": OSCAR_ADDRESS_FORM_FIELD_ORDER,
                "OSCAR_ADDRESS_BASE_FIELD_ORDER": OSCAR_ADDRESS_BASE_FIELD_ORDER,
                "OSCAR_ADDRESS_FORM_FIELDS": OSCAR_ADDRESS_FORM_FIELDS,
            },
        )


class Oscar3AddressConfig(OscarAddressConfig):
    def contribute_to_installed_apps(self, installed_apps):
        installed_apps.ocyan.append("ocyan.plugin.oscar_address")
        installed_apps.oscar.append(
            "ocyan.plugin.oscar_address.address.apps.AddressConfig",
            replaces="oscar.apps.address.apps.AddressConfig",
        )
        installed_apps.oscar.append(
            "ocyan.plugin.oscar_address.dashboard_orders",
            extends="oscar.apps.dashboard.orders",
        )
        return installed_apps
