# coding=utf-8

from django.db import models
from django.utils import timezone
from django.conf import settings

from apps.foundation.models import Subject
from apps.examination.exampaper.models import ExamPaper, ExamPaperDetail
from apps.examination.examquestion.models import ExamQuestion, ExamQuestionOption

class Exam(models.Model):
    FIXED = 1
    RANDOM = 2
    TYPE_CHOICES = (
        (FIXED, u'固定试卷'),
        (RANDOM, u'随机试卷'),
    )
    TYPE_JSON = [{'id': item[0], 'value': item[1]} for item in TYPE_CHOICES]

    name = models.CharField(max_length=200, verbose_name=u"名称")
    subject = models.ForeignKey(Subject, verbose_name=u"科目", on_delete=models.PROTECT)
    type = models.PositiveSmallIntegerField(choices=TYPE_CHOICES, verbose_name=u"试卷类型")
    duration = models.IntegerField(verbose_name=u'时长')
    exam_time = models.DateTimeField(verbose_name=u"考试时间")
    exampaper = models.ForeignKey(ExamPaper, verbose_name=u"试卷", on_delete=models.PROTECT)
    desc = models.TextField(verbose_name=u"备注", null=True, blank=True)

    examinee_count = models.IntegerField(verbose_name=u'考试人数', default=0, editable=False)

    create_user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u'添加人', editable=False, on_delete=models.PROTECT)
    create_time = models.DateTimeField(verbose_name=u"添加时间", default=timezone.now, editable=False)
    delete = models.BooleanField(verbose_name=u'删除', default=False, editable=False)

    class Meta:
        db_table = "exam"
        ordering = ['-id']
        verbose_name = u"考试管理"
        default_permissions = ()

class ExamLog(models.Model):
    MOCK = 1
    FORMAL = 2
    TYPE_CHOICES = (
        (MOCK, u'模拟考试'),
        (FORMAL, u'正式考试'),
    )
    TYPE_JSON = [{'id': item[0], 'value': item[1]} for item in TYPE_CHOICES]

    type = models.PositiveSmallIntegerField(choices=TYPE_CHOICES, verbose_name=u"类型")
    exam = models.ForeignKey(Exam, verbose_name=u"考试", null=True, blank=True, on_delete=models.PROTECT)
    exampaper = models.ForeignKey(ExamPaper, verbose_name=u"试卷", on_delete=models.PROTECT)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u'参加人员', on_delete=models.PROTECT)

    scores = models.IntegerField(verbose_name=u'分数', null=True, blank=True)
    rank = models.IntegerField(verbose_name=u'排名', null=True, blank=True)
    use_time = models.IntegerField(verbose_name=u'用时', null=True, blank=True)
    exam_time = models.DateTimeField(verbose_name=u"考试时间", null=True, blank=True)
    submit_time = models.DateTimeField(verbose_name=u"交卷时间", null=True, blank=True)

    single_answer_scores = models.IntegerField(verbose_name=u'单选题得分', default=0, editable=False)
    multiple_answer_scores = models.IntegerField(verbose_name=u'多选题得分', default=0, editable=False)
    fill_answer_scores = models.IntegerField(verbose_name=u'填空题得分', default=0, editable=False)
    judgment_answer_scores = models.IntegerField(verbose_name=u'判断题得分', default=0, editable=False)
    single_answer_count = models.IntegerField(verbose_name=u'单选题答对数', default=0, editable=False)
    multiple_answer_count = models.IntegerField(verbose_name=u'多选题答对数', default=0, editable=False)
    fill_answer_count = models.IntegerField(verbose_name=u'填空题答对数', default=0, editable=False)
    judgment_answer_count = models.IntegerField(verbose_name=u'判断题答对数', default=0, editable=False)

    class Meta:
        db_table = "exam_log"
        ordering = ['-id']
        verbose_name = u"考试记录"
        default_permissions = ()

class ExamAnswerLog(models.Model):
    RIGHT = 1
    WRONG = 2
    NOTDONE = 3
    STATUS_CHOICES = (
        (RIGHT, u'正确'),
        (WRONG, u'错误'),
        (NOTDONE, u'未做'),
    )
    STATUS_JSON = [{'id': item[0], 'value': item[1]} for item in STATUS_CHOICES]

    main = models.ForeignKey(ExamLog, verbose_name=u"考试记录", on_delete=models.PROTECT)
    detail = models.ForeignKey(ExamPaperDetail, verbose_name=u"试题", on_delete=models.PROTECT)
    status = models.PositiveSmallIntegerField(choices=STATUS_CHOICES, verbose_name=u'回答状态', default=NOTDONE)

    class Meta:
        db_table = "exam_answer_log"
        ordering = ['id']
        verbose_name = u"考试答题记录"
        default_permissions = ()

class ExamAnswerOptionLog(models.Model):
    main = models.ForeignKey(ExamAnswerLog, verbose_name=u"考试答题记录", on_delete=models.PROTECT)
    option = models.ForeignKey(ExamQuestionOption, verbose_name=u"选项", on_delete=models.PROTECT)

    class Meta:
        db_table = "exam_answer_option_log"
        ordering = ['id']
        verbose_name = u"选择题回答"
        default_permissions = ()

class ExamAnswerFillLog(models.Model):
    main = models.ForeignKey(ExamAnswerLog, verbose_name=u"考试答题记录", on_delete=models.PROTECT)
    content = models.TextField(verbose_name=u"内容")
    order = models.IntegerField(verbose_name=u'排序')

    class Meta:
        db_table = "exam_answer_fill_log"
        ordering = ['order', 'id']
        verbose_name = u"填空题回答"
        default_permissions = ()