# coding=utf-8

from django.conf import settings
from rest_framework import serializers
from apps.vehicle.models import Brand, Series, Model, VehicleImg
from apps.images.models import Images
from utils.booleancharfield import PriceShowCharField
from apps.base import Formater


class BrandSerializer(serializers.ModelSerializer):
    enabled_text = serializers.SerializerMethodField()

    def get_enabled_text(self, obj):
        if obj.enabled:
            return u'是'
        return u'否'

    class Meta:
        model = Brand
        fields = '__all__'

    def create(self, validated_data):
        instance = super(BrandSerializer, self).create(validated_data)
        return instance

    def update(self, instance, validated_data):
        instance = super(BrandSerializer, self).update(instance, validated_data)
        return instance


class SeriesSerializer(serializers.ModelSerializer):
    brand_name = serializers.CharField(source='brand.name', read_only=True)
    enabled_text = serializers.SerializerMethodField()
    img_url = serializers.SerializerMethodField()
    is_hot_text = serializers.SerializerMethodField()

    def get_is_hot_text(self, obj):
        if obj.is_hot:
            return u'是'
        return u'否'

    def get_enabled_text(self, obj):
        if obj.enabled:
            return u'是'
        return u'否'

    def get_img_url(self, obj):
        if obj.thumbnail:
            return '%s%s' % (settings.MEDIA_URL, obj.thumbnail.picture)
        return ''

    class Meta:
        model = Series
        fields = '__all__'

    def create(self, validated_data):
        user = self.context['request'].user
        file = self.context['request'].FILES.get('image', None)

        if file:
            validated_data['thumbnail'] = Images.objects.employee_addnew(user, Images.VEHICLE_SERIES_THUMBNAIL,
                                                                         file)
        instance = super(SeriesSerializer, self).create(validated_data)
        return instance


class ModelSerializer(serializers.ModelSerializer):
    series_name = serializers.CharField(source='series.name', read_only=True)
    brand_name = serializers.CharField(source='series.brand.name', read_only=True)
    enabled_text = serializers.SerializerMethodField()

    thumbnail_url = serializers.SerializerMethodField()
    price = PriceShowCharField()
    sale_price = PriceShowCharField()

    def get_enabled_text(self, obj):
        if obj.enabled:
            return u'是'
        return u'否'

    def get_thumbnail_url(self, obj):
        if obj.thumbnail:
            return '%s%s' % (settings.MEDIA_URL, obj.thumbnail.picture)
        return ''

    class Meta:
        model = Model
        fields = '__all__'

    def create(self, validated_data):
        user = self.context['request'].user
        thumbnail = self.context['request'].FILES.get('image', None)
        if thumbnail:
            validated_data['thumbnail'] = Images.objects.employee_addnew(user, Images.VEHICLE_MODEL_THUMBNAIL,
                                                                         thumbnail)
        instance = super(ModelSerializer, self).create(validated_data)

        return instance

    def validate(self, attrs):
        if 'price' in attrs:
            attrs['price'] = Formater.formatPrice(attrs['price'])
        if 'sale_price' in attrs:
            attrs['sale_price'] = Formater.formatPrice(attrs['sale_price'])
        return attrs


class VehicleImgSerializer(serializers.ModelSerializer):
    img_url = serializers.SerializerMethodField()

    def get_img_url(self, obj):
        if obj.img:
            return '%s%s' % (settings.MEDIA_URL, obj.img.picture)
        return ''

    class Meta:
        model = VehicleImg
        fields = '__all__'

