# coding=utf-8

from django.conf import settings
from rest_framework import serializers
from apps.commodity.models import Commodity, CommodityImage
from apps.collection.models import Collection
from apps.base import Formater
from apps.customer.models import Customer
from apps.option.models import Option


class CommoditySerializer(serializers.ModelSerializer):
    show_image_url = serializers.SerializerMethodField()
    price = serializers.SerializerMethodField()
    vip_price = serializers.SerializerMethodField()
    sale_count = serializers.SerializerMethodField()

    def get_sale_count(self, obj):
        count = (obj.total_sales or 0) + (obj.initial_sale_count or 0)
        return count

    def get_price(self, obj):
        return Formater.formatValueShow(obj.price)

    def get_vip_price(self, obj):
        return Formater.formatValueShow(obj.vip_price)

    def get_show_image_url(self, obj):
        if obj.show_image:
            return obj.show_image.get_path()
        return ''

    class Meta:
        model = Commodity
        fields = ('id', 'show_image_url', 'price', 'vip_price', 'sale_count', 'point_price', 'type', 'name', )


class CommodityDetailSerializer(serializers.ModelSerializer):
    sale_count = serializers.SerializerMethodField()
    is_collection = serializers.SerializerMethodField()
    carousel_urls = serializers.SerializerMethodField()
    details_urls = serializers.SerializerMethodField()
    price = serializers.SerializerMethodField()
    vip_price = serializers.SerializerMethodField()
    show_image = serializers.SerializerMethodField()

    def get_sale_count(self, obj):
        count = (obj.total_sales or 0) + (obj.initial_sale_count or 0)
        return count

    def get_is_collection(self, obj):
        if not self.context['request'].user or not self.context['request'].user.is_authenticated or not self.context['request'].user.is_customer():
            return False
        customer = Customer.objects.filter(user=self.context['request'].user).first()
        if not customer:
            return False
        return Collection.is_collection(customer, obj)

    def get_carousel_urls(self, obj):
        result = []
        rows = CommodityImage.objects.filter(commodity=obj, type=CommodityImage.CAROUSEL).values('image__picture')
        for row in rows:
            url = '%s%s' % (settings.SERVER_DOMAIN, row['image__picture'])
            result.append(url)
        return result

    def get_details_urls(self, obj):
        result = []
        rows = CommodityImage.objects.filter(commodity=obj, type=CommodityImage.DETAILS).values('image__picture')
        for row in rows:
            url = '%s%s' % (settings.SERVER_DOMAIN, row['image__picture'])
            result.append(url)
        return result

    def get_show_image(self, obj):
        if obj.show_image:
            return obj.show_image.get_path()
        return ''

    def get_price(self, obj):
        return Formater.formatValueShow(obj.price)

    def get_vip_price(self, obj):
        return Formater.formatValueShow(obj.vip_price)

    class Meta:
        model = Commodity
        fields = (
            'id', 'name', 'price', 'vip_price', 'point_price', 'sale_count', 'is_collection', 'carousel_urls', 'details_urls', 'show_image', 'type',
        )


class CommodityCategorySerializer(serializers.ModelSerializer):
    label = serializers.CharField(source='name', read_only=True)

    class Meta:
        model = Option
        fields = ('id', 'label', )


class HomeOptionSerializer(serializers.ModelSerializer):
    commodity_list = serializers.SerializerMethodField()

    class Meta:
        model = Option
        fields = ('id', 'name', 'commodity_list', )

    def get_commodity_list(self, obj):
        rows = Commodity.objects.filter(category=obj, delete=False, status=settings.ONLINE)
        serializer = CommoditySerializer(rows, many=True)
        data = serializer.data
        return data
