from django.utils.translation import gettext_lazy as _

from wagtail.admin.ui.tables import BooleanColumn, DateColumn, TitleColumn
from wagtail.admin.views.generic.chooser import (
    ChooseResultsViewMixin,
    ChooseViewMixin,
)

from oscar.core.loading import get_class

from .chooser.views import BaseSearchChooseView
from .tables import ImageTitleColumn

ProductElasticsearchIndex = get_class("search.api.product", "ProductElasticsearchIndex")
CategoryElasticsearchIndex = get_class(
    "search.api.category", "CategoryElasticsearchIndex"
)


# pylint: disable=abstract-method
class BaseCategoryChooseView(BaseSearchChooseView):
    elasticsearch_api_class = CategoryElasticsearchIndex

    @property
    def columns(self):
        return [
            ImageTitleColumn(
                name="image",
                label=_("Image"),
                url_name=self.chosen_url_name,
                link_attrs={"data-chooser-modal-choice": True},
            ),
            TitleColumn(
                name="full_name",
                label=_("Category"),
                url_name=self.chosen_url_name,
                link_attrs={"data-chooser-modal-choice": True},
            ),
            TitleColumn(
                name="get_num_children",
                label=_("Children"),
                url_name=self.chosen_url_name,
                link_attrs={"data-chooser-modal-choice": True},
            ),
            BooleanColumn(name="is_public", label=_("Public")),
        ]


class CategoryChooseView(ChooseViewMixin, BaseCategoryChooseView):
    pass


class CategoryChooseResultsView(ChooseResultsViewMixin, BaseCategoryChooseView):
    pass


def image_accessor(product):
    image = product.primary_image()
    try:
        return image.original
    except AttributeError:
        return image.get("original")


class BaseProductChooseView(BaseSearchChooseView):
    elasticsearch_api_class = ProductElasticsearchIndex

    @property
    def columns(self):
        return [
            ImageTitleColumn(
                name="primary_image",
                label=_("Image"),
                url_name=self.chosen_url_name,
                accessor=image_accessor,
                link_attrs={"data-chooser-modal-choice": True},
            ),
            TitleColumn(
                name="upc",
                label=_("UPC"),
                url_name=self.chosen_url_name,
                link_attrs={"data-chooser-modal-choice": True},
            ),
            TitleColumn(
                name="structure",
                label=_("Structure"),
                url_name=self.chosen_url_name,
                link_attrs={"data-chooser-modal-choice": True},
            ),
            TitleColumn(
                name="title",
                label=_("Title"),
                url_name=self.chosen_url_name,
                link_attrs={"data-chooser-modal-choice": True},
            ),
            DateColumn(name="date_updated", label=_("Modified")),
            BooleanColumn(name="is_public", label=_("Public")),
        ]


class ProductChooseView(ChooseViewMixin, BaseProductChooseView):
    pass


class ProductChooseResultsView(ChooseResultsViewMixin, BaseProductChooseView):
    pass
