from django.utils.translation import gettext_lazy as _

from ocyan.core.fender import config

IDEAL = "ideal"
CREDITCARD = "creditcard"
PAYPAL = "paypal"
MISTERCASH = "mistercash"
BANKTRANSFER = "banktransfer"
DIRECTDEBIT = "directdebit"
SOFORT = "sofort"
EPS = "eps"
KBC = "kbc"
INGHOMEPAY = "inghomepay"
GIFTCARDS = "giftcard"
BELFIUS = "belfius"
BITCOIN = "bitcoin"
BANCONTACT = "bancontact"
KLARNA = "klarna"
KLARNA_NOW = "klarnapaynow"
KLARNA_LATER = "klarnapaylater"
KLARNA_SLICE = "klarnasliceit"
PRZELEWY = "przelewy24"

pretty_names = {
    IDEAL: _("iDeal"),
    CREDITCARD: _("Creditcard"),
    PAYPAL: _("PayPal"),
    MISTERCASH: _("Bancontact"),
    BANCONTACT: _("Bancontact"),
    BANKTRANSFER: _("Overboeking"),
    DIRECTDEBIT: _("SEPA-incasso"),
    SOFORT: _("SOFORT Banking"),
    EPS: _("EPS"),
    KBC: _("KBC/CBC Payment"),
    INGHOMEPAY: _("ING HomePay"),
    GIFTCARDS: _("Cadeaukaart"),
    BELFIUS: _("Belfius Direct Net"),
    BITCOIN: _("Bitcoin"),
    KLARNA: _("klarna"),
    KLARNA_NOW: _("Klarna Pay Now"),
    KLARNA_LATER: _("Klarna Pay Later"),
    KLARNA_SLICE: _("Klarna Pay In Parts"),
    PRZELEWY: _("Przelewy24"),
}

all_mollie_methods = [
    IDEAL,
    CREDITCARD,
    PAYPAL,
    MISTERCASH,
    BANKTRANSFER,
    DIRECTDEBIT,
    SOFORT,
    EPS,
    KBC,
    INGHOMEPAY,
    GIFTCARDS,
    BELFIUS,
    BITCOIN,
    BANCONTACT,
    KLARNA,
    KLARNA_NOW,
    KLARNA_LATER,
    KLARNA_SLICE,
    PRZELEWY,
]

# Place payment methods that require the mollie order api in ORDER_API_METHODS
ORDER_API_METHODS = set([KLARNA, KLARNA_NOW, KLARNA_LATER, KLARNA_SLICE])

# ``mistercash`` is a historic configuration and URL alias. Mollie's current
# API expects the canonical ``bancontact`` method value.
METHOD_ALIASES = {
    MISTERCASH: BANCONTACT,
    KLARNA_NOW: KLARNA,
    KLARNA_LATER: KLARNA,
    KLARNA_SLICE: KLARNA,
}

# Keep the historic configuration list above for compatibility, but only these
# method identifiers are sent to the current Payments API.
SUPPORTED_API_METHODS = frozenset(
    {
        IDEAL,
        CREDITCARD,
        PAYPAL,
        BANCONTACT,
        BANKTRANSFER,
        DIRECTDEBIT,
        EPS,
        KBC,
        GIFTCARDS,
        BELFIUS,
        KLARNA,
        PRZELEWY,
    }
)


def normalize_payment_method(method):
    return METHOD_ALIASES.get(method, method)


def is_supported_payment_method(method):
    return normalize_payment_method(method) in SUPPORTED_API_METHODS


MOLLIE_LOCALES = {
    "nl": "nl_NL",
    "en": "en_US",
    "fr": "fr_FR",
    "de": "de_DE",
    "es": "es_ES",
    "it": "it_IT",
}


def get_mollie_payments(app_code, mapping, all_methods=False):
    payment_methods = []
    if all_methods:
        for method in all_mollie_methods:
            if config.get(app_code, method):
                method = normalize_payment_method(method)
                if method in SUPPORTED_API_METHODS:
                    payment_methods.append(
                        ("%s:%s" % (app_code, method), pretty_names.get(method))
                    )
    else:
        for method in mapping:
            if method in all_mollie_methods:
                method = all_mollie_methods[all_mollie_methods.index(method)]
                if config.get(app_code, method):
                    method = normalize_payment_method(method)
                    if method in SUPPORTED_API_METHODS:
                        payment_methods.append(
                            ("%s:%s" % (app_code, method), pretty_names.get(method))
                        )
    return list(dict.fromkeys(payment_methods))
