# coding=utf-8

import json
from django.conf import settings
from rest_framework import serializers
from apps.vehicle.models import Brand, Series, Model, VehicleImg, VehicleModelImg
from apps.images.models import Images

from utils.exceptions import CustomError
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()
    hot_text = serializers.SerializerMethodField()
    thumbnail_url = serializers.SerializerMethodField()
    price_show = serializers.SerializerMethodField()
    shop_price_show = serializers.SerializerMethodField()
    advance_pay_show = serializers.SerializerMethodField()
    equal_pay_show = serializers.SerializerMethodField()
    price = serializers.CharField()
    advance_pay = serializers.CharField()
    equal_pay = serializers.CharField()
    commission_ratio = serializers.CharField(required=False)
    playbill_url = serializers.SerializerMethodField()
    full_name = serializers.SerializerMethodField()
    commission_amount = serializers.SerializerMethodField()

    def get_commission_amount(self, obj):
        if obj.commission_ratio != None:
            return Formater.formatAmountShow(obj.commission_ratio)
        return ''

    def get_shop_price_show(self, obj):
        return Formater.formatAmountShow(obj.shop_price)

    def get_price_show(self, obj):
        return Formater.formatAmountShow(obj.price)

    def get_advance_pay_show(self, obj):
        return Formater.formatAmountShow(obj.advance_pay)

    def get_equal_pay_show(self, obj):
        return Formater.formatAmountShow(obj.equal_pay)

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

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

    def get_full_name(self, obj):
        return obj.series.brand.name + '-' + obj.series.name + '-' + obj.name

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

    def get_playbill_url(self, obj):
        if obj.playbill:
            return '%s%s' % (settings.MEDIA_URL, obj.playbill.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)
        tags = self.context['request'].data.get('tags')

        if thumbnail:
            validated_data['thumbnail'] = Images.objects.employee_addnew(user.employee, 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.formatAmount(attrs['price'])
        if 'equal_pay' in attrs:
            attrs['equal_pay'] = Formater.formatAmount(attrs['equal_pay'])
        if 'advance_pay' in attrs:
            attrs['advance_pay'] = Formater.formatAmount(attrs['advance_pay'])
        if 'shop_price' in attrs:
            attrs['shop_price'] = Formater.formatAmount(attrs['shop_price'])
        if 'commission_ratio' in attrs and attrs['commission_ratio']:
            attrs['commission_ratio'] = Formater.formatAmount(attrs['commission_ratio'])
        else:
            attrs['commission_ratio'] = None
        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__'


class VehicleModelImgSerializer(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 = VehicleModelImg
        fields = '__all__'
