from rest_framework.serializers import ModelSerializer, CharField, Serializer, SerializerMethodField, FloatField
from rest_framework import serializers

from apps.exam.models import Question, Questioncat, Paper, Exam, PaperQuestion, ExamRecord, AnswerDetail


class QuestioncatSerializer(ModelSerializer):
    class Meta:
        model = Questioncat
        fields = '__all__'


class QuestionSerializer(ModelSerializer):
    questioncat_name = serializers.CharField(source='questioncat.name', read_only=True)
    class Meta:
        model = Question
        fields = '__all__'


class PaperSerializer(ModelSerializer):
    class Meta:
        model = Paper
        exclude = ('questions',)


class PaperQuestionSerializer(ModelSerializer):
    class Meta:
        model = PaperQuestion
        fields = ['question', 'total_score']


class PaperCreateUpdateSerializer(ModelSerializer):
    questions_ = PaperQuestionSerializer(many=True)

    class Meta:
        model = Paper
        fields = '__all__'


class PaperQuestionDetailSerializer(ModelSerializer):
    name = serializers.ReadOnlyField(source='question.name')
    options = serializers.ReadOnlyField(source='question.options')
    right = serializers.ReadOnlyField(source='question.right')
    type = serializers.ReadOnlyField(source='question.type')
    img = serializers.ReadOnlyField(source='question.img')
    questioncat_name = serializers.ReadOnlyField(
        source='question.questioncat.name')
    level = serializers.ReadOnlyField(source='question.level')

    class Meta:
        model = PaperQuestion
        fields = ('id', 'name', 'options', 'right', 'type', 'level',
                  'total_score', 'questioncat_name', 'img', 'question')


class PaperQuestionShortSerializer(ModelSerializer):
    class Meta:
        model = PaperQuestion
        fields = '__all__'


class PaperDetailSerializer(ModelSerializer):
    questions_ = SerializerMethodField()

    class Meta:
        model = Paper
        fields = '__all__'

    def get_questions_(self, instance):
        pqs = PaperQuestion.objects.filter(paper=instance)
        return PaperQuestionDetailSerializer(pqs, many=True).data


class ExamCreateUpdateSerializer(ModelSerializer):
    class Meta:
        model = Exam
        fields = ['name', 'place', 'open_time',
                  'close_time', 'proctor_name', 'proctor_phone', 'chance', 'paper']


class ExamListSerializer(ModelSerializer):
    create_by_name = CharField(source='create_by.name', read_only=True)
    paper_ = PaperSerializer(source='paper', read_only=True)

    class Meta:
        model = Exam
        fields = '__all__'


class ExamDetailSerializer(ModelSerializer):
    create_by_name = CharField(source='create_by.name', read_only=True)
    paper_ = PaperSerializer(source='paper', read_only=True)

    class Meta:
        model = Exam
        fields = '__all__'


class ExamAttendSerializer(Serializer):
    code = CharField(label="考试编号")


class ExamRecordListSerializer(serializers.ModelSerializer):
    """
    考试列表序列化
    """
    took_format = serializers.SerializerMethodField()
    create_by_name = serializers.CharField(
        source='create_by.name', read_only=True)

    class Meta:
        model = ExamRecord
        exclude = ('detail',)

    def get_took_format(self, obj):
        m, s = divmod(obj.took, 60)
        h, m = divmod(m, 60)
        return "%02d:%02d:%02d" % (h, m, s)


class ExamRecordDetailSerializer(serializers.ModelSerializer):
    """
    考试详情序列化
    """
    took_format = serializers.SerializerMethodField()
    questions_ = serializers.SerializerMethodField()

    class Meta:
        model = ExamRecord
        exclude = ('detail',)

    def get_took_format(self, obj):
        m, s = divmod(obj.took, 60)
        h, m = divmod(m, 60)
        return "%02d:%02d:%02d" % (h, m, s)

    def get_questions_(self, obj):
        objs = AnswerDetail.objects.select_related('question').filter(
            examrecord=obj).order_by('id')
        return AnswerDetailSerializer(instance=objs, many=True).data


class AnswerDetailUpdateSerializer(serializers.Serializer):
    id = serializers.CharField(label='下发ID')
    user_answer = serializers.JSONField(label='作答')


class ExamRecordSubmitSerializer(serializers.ModelSerializer):
    questions_ = AnswerDetailUpdateSerializer(many=True)

    class Meta:
        model = ExamRecord
        fields = ['questions_']


class AnswerDetailSerializer(ModelSerializer):
    name = serializers.ReadOnlyField(source='question.name')
    options = serializers.ReadOnlyField(source='question.options')
    right = serializers.ReadOnlyField(source='question.right')
    type = serializers.ReadOnlyField(source='question.type')
    img = serializers.ReadOnlyField(source='question.img')
    questioncat_name = serializers.ReadOnlyField(
        source='question.questioncat.name')
    level = serializers.ReadOnlyField(source='question.level')

    class Meta:
        model = AnswerDetail
        fields = ['id', 'question', 'name', 'options', 'right', 'type', 'level',
                  'total_score', 'questioncat_name', 'img', 'user_answer', 'score', 'is_right']


class AnswerDetailOutSerializer(ModelSerializer):
    name = serializers.ReadOnlyField(source='question.name')
    options = serializers.ReadOnlyField(source='question.options')
    type = serializers.ReadOnlyField(source='question.type')
    img = serializers.ReadOnlyField(source='question.img')
    questioncat_name = serializers.ReadOnlyField(
        source='question.questioncat.name')
    level = serializers.ReadOnlyField(source='question.level')

    class Meta:
        model = AnswerDetail
        fields = ['id', 'question', 'name', 'options', 'type', 'level',
                  'total_score', 'questioncat_name', 'img', 'user_answer', 'score', 'is_right']
