# coding=utf-8

from rest_framework import serializers
from .models import ExamPaper
from utils.exceptions import CustomError

class ExamPaperSerializer(serializers.ModelSerializer):
    subject_name = serializers.CharField(source='subject.name', read_only=True)
    type_text = serializers.CharField(source='get_type_display', read_only=True)
    create_user_text = serializers.CharField(source='create_user.name', read_only=True)

    class Meta:
        model = ExamPaper
        fields = "__all__"

    def validate(self, attrs):
        if 'subject' in attrs and attrs['subject'].delete:
            raise CustomError(u'科目[%s]已被删除！' % attrs['subject'].name)
        return attrs

    def create(self, validated_data):
        validated_data['create_user'] = self.context['request'].user
        instance = super(ExamPaperSerializer, self).create(validated_data)
        instance.update_count()
        if not instance.question_total_count:
            raise CustomError(u'请选择试题！')
        if instance.passline > instance.question_total_scores:
           raise CustomError(u'及格线不能大于试题总分！')
        instance.save()

        instance.generate_detail()
        return instance

    def update(self, instance, validated_data):
        old_count = []
        old_count.append(instance.single_simple_count)
        old_count.append(instance.single_mid_count)
        old_count.append(instance.single_hard_count)
        old_count.append(instance.multiple_simple_count)
        old_count.append(instance.multiple_mid_count)
        old_count.append(instance.multiple_hard_count)
        old_count.append(instance.fill_simple_count)
        old_count.append(instance.fill_mid_count)
        old_count.append(instance.fill_hard_count)
        old_count.append(instance.judgment_simple_count)
        old_count.append(instance.judgment_mid_count)
        old_count.append(instance.judgment_hard_count)

        if instance.delete:
            raise CustomError(u'试卷[%s]已经被删除,禁止操作' % instance.name)
        instance = super(ExamPaperSerializer, self).update(instance, validated_data)
        instance.update_count()
        if not instance.question_total_count:
            raise CustomError(u'请选择试题！')
        if instance.passline > instance.question_total_scores:
           raise CustomError(u'及格线不能大于试题总分！')
        instance.save()

        new_count = []
        new_count.append(instance.single_simple_count)
        new_count.append(instance.single_mid_count)
        new_count.append(instance.single_hard_count)
        new_count.append(instance.multiple_simple_count)
        new_count.append(instance.multiple_mid_count)
        new_count.append(instance.multiple_hard_count)
        new_count.append(instance.fill_simple_count)
        new_count.append(instance.fill_mid_count)
        new_count.append(instance.fill_hard_count)
        new_count.append(instance.judgment_simple_count)
        new_count.append(instance.judgment_mid_count)
        new_count.append(instance.judgment_hard_count)

        change_detail = False
        for i in range(0, len(old_count)):
            if old_count[i] != new_count[i]:
                change_detail = True
                break

        if change_detail:
            instance.clear_detail()
            instance.generate_detail()

        return instance

class StaffExamPaperSerializer(serializers.ModelSerializer):
    subject_name = serializers.CharField(source='subject.name', read_only=True)
    status_text = serializers.SerializerMethodField()

    def get_status_text(self, obj):
        from apps.examination.exam.models import ExamLog
        exampaper = ExamLog.objects.filter(user=self.context['request'].user,exampaper=obj)
        if exampaper:
            return True
        return False

    class Meta:
        model = ExamPaper
        fields = ('id', 'name','subject_name','status_text','did_count','question_total_count', )
