from django.contrib import admin

from oscar.apps.catalogue import admin as oscar
from oscar.apps.catalogue.admin import *  # noqa pylint: disable=W0611,W0401,W0614

from .filters import CategoryFilter, ProductClassFilter


# pylint: disable=function-redefined
class AttributeInline(oscar.AttributeInline):
    autocomplete_fields = ["attribute"]


# pylint: disable=function-redefined
class CategoryInline(oscar.CategoryInline):
    autocomplete_fields = ("category",)


# pylint: disable=function-redefined
class ProductAdmin(oscar.ProductAdmin):
    inlines = [AttributeInline, CategoryInline, oscar.ProductRecommendationInline]
    list_filter = oscar.ProductAdmin.list_filter + [
        "is_public",
        CategoryFilter,
        ProductClassFilter,
    ]
    readonly_fields = oscar.ProductAdmin.readonly_fields + ("product_class",)


class OptionAdmin(admin.ModelAdmin):
    list_display = ["name", "code", "type"]
    list_filter = ["type", "productclass"]
    search_fields = [
        "name",
        "code",
        "productclass__name",
        "productclass__slug",
        "product__upc",
        "product__slug",
        "product__title",
    ]


class ProductAttributeAdmin(oscar.ProductAttributeAdmin):
    search_fields = (
        "name",
        "code",
        "type",
        "product_class__name",
        "option_group__name",
    )


class ProductAttributeValueAdmin(oscar.ProductAttributeValueAdmin):
    autocomplete_fields = ("product", "attribute")


class ProductImageAdmin(admin.ModelAdmin):
    autocomplete_fields = ("product",)
    search_fields = ("caption", "product__upc", "product__title", "product__slug")


class CategoryAdmin(oscar.CategoryAdmin):
    search_fields = ("name", "slug", "pk")


class ProductCategoryAdmin(admin.ModelAdmin):
    list_display = ("pk", "product", "category")
    search_fields = (
        "product__upc",
        "product__title",
        "product__slug",
        "category__name",
        "category__slug",
    )
    autocomplete_fields = ("product", "category")


admin.site.unregister(Product)
admin.site.register(Product, ProductAdmin)
admin.site.unregister(ProductAttribute)
admin.site.register(ProductAttribute, ProductAttributeAdmin)
admin.site.unregister(ProductAttributeValue)
admin.site.register(ProductAttributeValue, ProductAttributeValueAdmin)
admin.site.unregister(ProductImage)
admin.site.register(ProductImage, ProductImageAdmin)
admin.site.unregister(Category)
admin.site.register(Category, CategoryAdmin)
admin.site.unregister(ProductCategory)
admin.site.register(ProductCategory, ProductCategoryAdmin)
admin.site.unregister(Option)
admin.site.register(Option, OptionAdmin)
