# coding=utf-8

from rest_framework import serializers
from django.utils import timezone
from .models import *
from utils.exceptions import CustomError
from utils.booleancharfield import PriceShowCharField
from apps.order.models import Order
from apps.base import Formater
from utils.booleancharfield import BooleanCharField

class ConfigSerializer(serializers.ModelSerializer):

    class Meta:
        model = Config
        fields = '__all__'

class SchoolSerializer(serializers.ModelSerializer):
    enable_text = serializers.SerializerMethodField()

    def get_enable_text(self, obj):
        if obj.enable:
            return '是'
        return '否'

    class Meta:
        model = School
        fields = '__all__'

class GradeSerializer(serializers.ModelSerializer):
    school_text = serializers.CharField(source='school.name', read_only=True)

    class Meta:
        model = Grade
        fields = '__all__'

    def create(self, validated_data):
        count = self.initial_data.get('count')
        if int(count) > 30:
            raise CustomError(u'班級数量不能大于30个！')
        instance = super(GradeSerializer, self).create(validated_data)

        if instance.count > 0:
            for count in range(1, instance.count + 1):
                Classes.objects.create(grade=instance, name=u'{}班'.format(count), enable=True)
        return instance

    def update(self, instance, validated_data):
        count = self.initial_data.get('count')
        if int(count) > 30:
            raise CustomError(u'班級数量不能大于30个！')

        instance = super(GradeSerializer, self).update(instance, validated_data)

        Classes.objects.filter(grade=instance).update(enable=False)
        for count in range(1, instance.count + 1):
            classes, create = Classes.objects.get_or_create(grade=instance, name=u'{}班'.format(count))
            if not create:
                classes.enable = True
                classes.save()
        return instance

class StudentSerializer(serializers.ModelSerializer):
    school_text = serializers.CharField(source='school.name', read_only=True)
    grade_text = serializers.CharField(source='classes.grade.name', read_only=True)
    classes_text = serializers.CharField(source='classes.name', read_only=True)
    create_user_text = serializers.CharField(source='create_user.username', read_only=True)
    create_time_f = serializers.DateTimeField(source='create_time', format=settings.SHORT_DATETIME_FORMAT,
                                              read_only=True)
    enable_text = BooleanCharField(source='enable', read_only=True)

    class Meta:
        model = Student
        fields = '__all__'

    def create(self, validated_data):
        validated_data['create_user'] = self.context['request'].user
        instance = super(StudentSerializer, self).create(validated_data)
        return instance

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

class CommodityLevelSerializer(serializers.ModelSerializer):
    home_show_text = BooleanCharField(source='home_show', read_only=True)
    enable_text = BooleanCharField(source='enable', read_only=True)
    category_text = serializers.CharField(source='category.name', read_only=True)

    class Meta:
        model = CommodityLevel
        fields = '__all__'
