# coding=utf-8

from django.db.models import Q
from rest_framework import serializers
from apps.customer.models import *
from apps.vehicle_order.models import *
from apps.product_order.models import *
from apps.base import Formater
from utils.exceptions import CustomError
from apps.maint_order.models import MaintOrderReserve
from .xgj import XGJ


class CustomerVehicleSerializer(serializers.ModelSerializer):
    has_orders = serializers.SerializerMethodField()

    def get_has_orders(self, obj):
        abutment_xgj = Config.getConfigValue(Config.KEY_ABUTMENT_XGJ) or ''
        if abutment_xgj and abutment_xgj == 'true':
            res = XGJ.get_order(obj.id)
            if len(res['data']) > 0:
                return True
            return False
        return False

    class Meta:
        model = CustomerVehicle
        fields = '__all__'

    def create(self, validated_data):
        customer = self.context['request'].customer
        validated_data['customer'] = customer
        instance = super(CustomerVehicleSerializer, self).create(validated_data)
        return instance

class CustomerAddressSerializer(serializers.ModelSerializer):
    full_addr = serializers.SerializerMethodField()

    def get_full_addr(self, obj):
        full_addr = obj.area + obj.addr
        return full_addr

    class Meta:
        model = CustomerAddress
        fields = '__all__'

    def create(self, validated_data):
        customer = self.context['request'].customer
        validated_data['customer'] = customer
        if validated_data['default']:
            CustomerAddress.objects.filter(customer=customer).update(default=False)
        instance = super(CustomerAddressSerializer, self).create(validated_data)
        return instance


class ProductOrderSerializer(serializers.ModelSerializer):
    product_name = serializers.CharField(source='product.name', read_only=True)
    name = serializers.CharField(source='customer.name', read_only=True)
    customer_name = serializers.CharField(source='customer_address.name', read_only=True)
    customer_tel = serializers.CharField(source='customer_address.tel', read_only=True)
    area = serializers.CharField(source='customer_address.area', read_only=True)
    addr = serializers.CharField(source='customer_address.addr', read_only=True)
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M', read_only=True)
    price = serializers.SerializerMethodField()
    amount = serializers.SerializerMethodField()

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

    def get_amount(self, obj):
        return Formater.formatAmountShow(obj.amount)

    class Meta:
        model = ProductOrder
        fields = '__all__'

    def create(self, validated_data):
        validated_data['customer'] = self.context['request'].customer
        validated_data['price'] = Formater.formatPrice(self.initial_data.get('price'))
        validated_data['amount'] = validated_data['price'] * validated_data['count']
        instance = super(ProductOrderSerializer, self).create(validated_data)
        no = instance.generateNo()
        instance.no = no
        instance.save()
        return instance


class VehicleDriveReserveSerializer(serializers.ModelSerializer):
    shop_name = serializers.CharField(source='shop.name', read_only=True)
    brand_name = serializers.CharField(source='model.series.brand.name', read_only=True)
    series_name = serializers.CharField(source='model.series.name', read_only=True)
    model_name = serializers.CharField(source='model.name', read_only=True)
    customer_name = serializers.CharField(source='customer.name', read_only=True)
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M', read_only=True)
    date = serializers.DateField(format='%Y-%m-%d')

    class Meta:
        model = VehicleDriveReserve
        fields = '__all__'

    def create(self, validated_data):
        customer = self.context['request'].customer
        validated_data['customer'] = customer
        instance = super(VehicleDriveReserveSerializer, self).create(validated_data)
        return instance


class VehicleInquirySerializer(serializers.ModelSerializer):
    shop_name = serializers.CharField(source='shop.name', read_only=True)
    brand_name = serializers.CharField(source='model.series.brand.name', read_only=True)
    series_name = serializers.CharField(source='model.series.name', read_only=True)
    model_name = serializers.CharField(source='model.name', read_only=True)
    customer_name = serializers.CharField(source='customer.name', read_only=True)
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M', read_only=True)
    displace_text = serializers.SerializerMethodField()

    def get_displace_text(self, obj):
        return obj.displace and u'是' or u'否'

    class Meta:
        model = VehicleInquiry
        fields = '__all__'

    def create(self, validated_data):
        customer = self.context['request'].customer
        validated_data['customer'] = customer
        instance = super(VehicleInquirySerializer, self).create(validated_data)
        return instance


class UsedVehicleInquirySerializer(serializers.ModelSerializer):
    brand_name = serializers.CharField(source='brand.name', read_only=True)
    customer_name = serializers.CharField(source='customer.name', read_only=True)
    customer_tel = serializers.CharField(source='customer.tel', read_only=True)
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    gender_text = serializers.CharField(source='get_gender_display', read_only=True)
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M', read_only=True)

    class Meta:
        model = UsedVehicleInquiry
        fields = '__all__'

    def create(self, validated_data):
        customer = self.context['request'].customer
        validated_data['customer'] = customer
        instance = super(UsedVehicleInquirySerializer, self).create(validated_data)
        return instance


class UsedVehicleEstimateSerializer(serializers.ModelSerializer):
    brand_name = serializers.CharField(source='brand.name', read_only=True)
    customer_name = serializers.CharField(source='customer.name', read_only=True)
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M', read_only=True)
    plate_date = serializers.DateField(format='%Y-%m-%d')

    class Meta:
        model = UsedVehicleEstimate
        fields = '__all__'

    def create(self, validated_data):
        customer = self.context['request'].customer
        validated_data['customer'] = customer
        instance = super(UsedVehicleEstimateSerializer, self).create(validated_data)
        return instance


class InsurConsultationSerializer(serializers.ModelSerializer):
    brand_name = serializers.CharField(source='brand.name', read_only=True)
    customer_name = serializers.CharField(source='customer.name', read_only=True)
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M', read_only=True)

    class Meta:
        model = InsurConsultation
        fields = '__all__'

    def create(self, validated_data):
        customer = self.context['request'].customer
        validated_data['customer'] = customer
        instance = super(InsurConsultationSerializer, self).create(validated_data)
        return instance


class MaintOrderReserveSerializer(serializers.ModelSerializer):
    shop_name = serializers.CharField(source='shop.name', read_only=True)
    model_name = serializers.CharField(source='vehicle.model', read_only=True)
    customer_name = serializers.CharField(source='customer.name', read_only=True)
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M', read_only=True)
    date = serializers.DateField(format='%Y-%m-%d')

    class Meta:
        model = MaintOrderReserve
        fields = '__all__'


    def create(self, validated_data):
        customer = self.context['request'].customer
        validated_data['customer'] = customer
        instance = super(MaintOrderReserveSerializer, self).create(validated_data)
        return instance

