from django import forms
from django.utils.translation import gettext_lazy as _

from oscar.core.loading import get_class
from oscar.templatetags.currency_filters import currency

DataPriceSelectWidget = get_class("basket.widgets", "DataPriceSelectWidget")
InfoString = get_class("basket.utils", "InfoString")


class AddToBasketFormMixin:
    OPTION_FIELD_FACTORIES = {}

    class Media:
        js = ()
        css = {}


class DataPriceMixin(object):
    child_product_widget = DataPriceSelectWidget

    def _create_parent_product_fields(self, product):
        """
        Override to add price info to options attributes
        """
        choices = []
        disabled_values = []
        for child in product.variants_ordered_by_price.all():
            # Check if it is available to buy
            info = self.basket.strategy.fetch_for_product(child)
            price = info.price

            if not info.availability.is_available_to_buy:
                disabled_values.append(child.id)

            choices.append(
                (
                    child.id,
                    InfoString(
                        "%s - %s"
                        % (child.get_title(), currency(price.incl_tax, price.currency)),
                        info,
                    ),
                )
            )

        self.fields["child_id"] = forms.ChoiceField(
            choices=tuple(choices),
            label=_("Variant"),
            widget=self.child_product_widget(disabled_values=disabled_values),
        )
