# Path: src/oscarapicheckout/settings.py
# ORDER_STATUS_AUTHORIZED = overridable("ORDER_STATUS_AUTHORIZED", "Authorized")
# 
# ORDER_STATUS_PAYMENT_DECLINED = overridable(
#     "ORDER_STATUS_PAYMENT_DECLINED", "Payment Declined"
# )
# 
# Path: src/oscarapicheckout/signals.py

# 
# Path: src/oscarapicheckout/states.py
# COMPLETE = "Complete"
# 
# DECLINED = "Declined"
# 
# CONSUMED = "Consumed"
# 
# class Complete(SourceBoundPaymentStatus):
#     status = COMPLETE
# 
# class Declined(SourceBoundPaymentStatus):
#     status = DECLINED
# 
# class Consumed(SourceBoundPaymentStatus):
#     status = CONSUMED


from decimal import Decimal
from django.db.models import F
from django.utils.translation import gettext_lazy as _
from oscar.core.loading import get_class, get_model
from oscarapi.basket import operations
from .settings import ORDER_STATUS_AUTHORIZED, ORDER_STATUS_PAYMENT_DECLINED
from .signals import order_payment_declined, order_payment_authorized
from .states import COMPLETE, DECLINED, CONSUMED, Complete, Declined, Consumed
import pickle
import base64
Order = get_model("order", "Order")
OrderCreator = get_class("order.utils", "OrderCreator")

CHECKOUT_ORDER_ID = "api_checkout_pending_order_id"
CHECKOUT_PAYMENT_STEPS = "api_checkout_payment_steps"


def _session_pickle(obj):
    pickled = pickle.dumps(obj)
    base64ed = base64.standard_b64encode(pickled)
    utfed = base64ed.decode("utf8")
    return utfed


def _session_unpickle(utfed):
    base64ed = utfed.encode("utf8")
    pickled = base64.standard_b64decode(base64ed)
    obj = pickle.loads(pickled)
    return obj


def _update_payment_method_state(request, method_key, state):
    states = request.session.get(CHECKOUT_PAYMENT_STEPS, {})
    states[method_key] = _session_pickle(state)
    request.session[CHECKOUT_PAYMENT_STEPS] = states
    request.session.modified = True


def _set_order_authorized(order, request):
    # Set the order status

# Path: src/oscarapicheckout/settings.py
# ORDER_STATUS_AUTHORIZED = overridable("ORDER_STATUS_AUTHORIZED", "Authorized")
# 
# ORDER_STATUS_PAYMENT_DECLINED = overridable(
#     "ORDER_STATUS_PAYMENT_DECLINED", "Payment Declined"
# )
# 
# Path: src/oscarapicheckout/signals.py

# 
# Path: src/oscarapicheckout/states.py
# COMPLETE = "Complete"
# 
# DECLINED = "Declined"
# 
# CONSUMED = "Consumed"
# 
# class Complete(SourceBoundPaymentStatus):
#     status = COMPLETE
# 
# class Declined(SourceBoundPaymentStatus):
#     status = DECLINED
# 
# class Consumed(SourceBoundPaymentStatus):
#     status = CONSUMED


# Path: src/oscarapicheckout/utils.py
from decimal import Decimal
from django.db.models import F
from django.utils.translation import gettext_lazy as _
from oscar.core.loading import get_class, get_model
from oscarapi.basket import operations
from .settings import ORDER_STATUS_AUTHORIZED, ORDER_STATUS_PAYMENT_DECLINED
from .signals import order_payment_declined, order_payment_authorized
from .states import COMPLETE, DECLINED, CONSUMED, Complete, Declined, Consumed
import pickle
import base64

Order = get_model("order", "Order")
OrderCreator = get_class("order.utils", "OrderCreator")

CHECKOUT_ORDER_ID = "api_checkout_pending_order_id"
CHECKOUT_PAYMENT_STEPS = "api_checkout_payment_steps"


def _session_pickle(obj):
    pickled = pickle.dumps(obj)
    base64ed = base64.standard_b64encode(pickled)
    utfed = base64ed.decode("utf8")
    return utfed


def _session_unpickle(utfed):
    base64ed = utfed.encode("utf8")
    pickled = base64.standard_b64decode(base64ed)
    obj = pickle.loads(pickled)
    return obj


def _update_payment_method_state(request, method_key, state):
    states = request.session.get(CHECKOUT_PAYMENT_STEPS, {})
    states[method_key] = _session_pickle(state)
    request.session[CHECKOUT_PAYMENT_STEPS] = states
    request.session.modified = True


def _set_order_authorized(order, request):
    # Set the order status

    order.set_status(ORDER_STATUS_AUTHORIZED)