# coding=utf-8

from rest_framework import serializers
from django.conf import settings
from django.utils import timezone
from apps.upload.models import Upload
from .models import Commodity, CommodityImages
from apps.base import Formater
from utils.booleancharfield import PriceShowCharField

class CommoditySerializer(serializers.ModelSerializer):
    create_time_f = serializers.DateTimeField(source='create_time', format=settings.SHORT_DATETIME_FORMAT,
                                              read_only=True)
    create_user_text = serializers.CharField(source='create_user.name', read_only=True)
    category_text = serializers.SerializerMethodField()
    main_image_url = serializers.CharField(source='main_image.picture', read_only=True)
    list_image_url = serializers.CharField(source='list_image.picture', read_only=True)
    detail_image = serializers.SerializerMethodField()
    price = PriceShowCharField()
    status_text = serializers.CharField(source='get_status_display', read_only=True)

    def get_category_text(self, obj):
        name = ''
        if obj.category:
            name = obj.category.name
            if obj.category.category:
                name = '{} — {}'.format(obj.category.category.name, name)
                if obj.category.category.category:
                    name = '{} — {}'.format(obj.category.category.category.name, name)
        return name

    def get_detail_image(self, obj):
        count = CommodityImages.objects.filter(product=obj, type=CommodityImages.DETAILS).count()
        return count

    class Meta:
        model = Commodity
        fields = '__all__'

    def create(self, validated_data):
        validated_data['create_user'] = self.context['request'].user
        validated_data['price'] = Formater.formatPrice(self.initial_data['price'])
        validated_data['refresh_time'] = timezone.now()

        instance = super(CommoditySerializer, self).create(validated_data)
        return instance

    def update(self, instance, validated_data):
        validated_data['price'] = Formater.formatPrice(self.initial_data['price'])
        validated_data['refresh_time'] = timezone.now()
        instance = super(CommoditySerializer, self).update(instance, validated_data)
        return instance

class WxCommoditySerizlizer(serializers.ModelSerializer):
    price = PriceShowCharField()
    list_image = serializers.SerializerMethodField()

    def get_list_image(self, obj):
        if obj.list_image:
            return obj.list_image.get_path()
        else:
            return ''

    class Meta:
        model = Commodity
        fields = ('id', 'name', 'introduce', 'price', 'list_image', )


class WxCommodityDetailsSerializer(serializers.ModelSerializer):
    price = PriceShowCharField()
    main_image = serializers.SerializerMethodField()
    detail_images = serializers.SerializerMethodField()

    def get_main_image(self, obj):
        if obj.main_image:
            return obj.main_image.get_path()
        else:
            return ''

    def get_detail_images(self, obj):
        detail_images = CommodityImages.objects.filter(product=obj, type=CommodityImages.DETAILS)
        detail_image = []
        for i in detail_images:
            detail_image.append(i.image.get_path())
        return detail_image

    class Meta:
        model = Commodity
        fields = (
            'id', 'name', 'price', 'introduce', 'main_image', 'detail_images', 'discount', 'stock'
        )
