# coding=utf-8
from django.conf import settings
from rest_framework import serializers
from .models import UsedVehicle, UsedVehicleImg


class UsedVehicleSerializer(serializers.ModelSerializer):
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    brand_name = serializers.CharField(source='brand.name', read_only=True)
    plate_date = serializers.DateField(format='%Y-%m-%d')
    factory_date = serializers.DateField(format='%Y-%m-%d')
    thumbnail_url = serializers.SerializerMethodField()

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

    class Meta:
        model = UsedVehicle
        fields = '__all__'

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

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

class UsedVehicleImgSerializer(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 = UsedVehicleImg
        fields = '__all__'
