# coding=utf-8

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

from apps.foundation.models import Chapter

class ExamQuestion(models.Model):
    SINGLE = 1
    MULTIPLE = 2
    FILL = 3
    JUDGMENT = 4
    TYPE_CHOICES = (
        (SINGLE, u'单选题'),
        (MULTIPLE, u'多选题'),
        (FILL, u'填空题'),
        (JUDGMENT, u'判断题'),
    )
    TYPE_JSON = [{'id': item[0], 'value': item[1]} for item in TYPE_CHOICES]

    SIMPLE = 1
    MID = 2
    HARD = 3
    DIFFICULTY_CHOICES = (
        (SIMPLE, u'简单'),
        (MID, u'中等'),
        (HARD, u'困难'),
    )
    DIFFICULTY_JSON = [{'id': item[0], 'value': item[1]} for item in DIFFICULTY_CHOICES]

    chapter = models.ForeignKey(Chapter, verbose_name=u"章节", on_delete=models.PROTECT)
    type = models.PositiveSmallIntegerField(choices=TYPE_CHOICES, verbose_name=u"题型")
    difficulty = models.PositiveSmallIntegerField(choices=DIFFICULTY_CHOICES, verbose_name=u"难度")
    scores = models.IntegerField(verbose_name=u'分数')
    title = models.TextField(verbose_name=u"题目")
    judgment = models.BooleanField(verbose_name=u'判断题答案', default=False)
    analysis = models.TextField(verbose_name=u"解析", null=True, blank=True)
    create_user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u'录入人',related_name='exam_question_user', 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_question"
        ordering = ['-id']
        verbose_name = u"试题管理"
        default_permissions = ()

class ExamQuestionOption(models.Model):
    main = models.ForeignKey(ExamQuestion, verbose_name=u"试题", on_delete=models.PROTECT)
    content = models.TextField(verbose_name=u"内容")
    right = models.BooleanField(verbose_name=u'正确答案', default=False)
    delete = models.BooleanField(verbose_name=u'删除', default=False, editable=False)

    class Meta:
        db_table = "exam_question_option"
        ordering = ['id']
        verbose_name = u"选择题选项" # 单选、多选
        default_permissions = ()

class ExamQuestionFill(models.Model):
    main = models.ForeignKey(ExamQuestion, verbose_name=u"试题", on_delete=models.PROTECT)
    content = models.TextField(verbose_name=u"内容")
    order = models.IntegerField(verbose_name=u'排序')
    delete = models.BooleanField(verbose_name=u'删除', default=False, editable=False)

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

class ExamQuestionFeedback(models.Model):
    TYPE = 1
    ANSWER = 2
    ANALYSIS = 3
    OTHER = 4
    TYPE_CHOICES = (
        (TYPE, u'题目类型'),
        (ANSWER, u'题目答案'),
        (ANALYSIS, u'题目解析'),
        (OTHER, u'其它'),
    )
    TYPE_JSON = [{'id': item[0], 'value': item[1]} for item in TYPE_CHOICES]

    UNTREATED = 1
    PROCESSED = 2
    STATUS_CHOICES = (
        (UNTREATED, u'未处理'),
        (PROCESSED, u'已处理'),
    )
    STATUS_JSON = [{'id': item[0], 'value': item[1]} for item in STATUS_CHOICES]

    main = models.ForeignKey(ExamQuestion, verbose_name=u"试题", on_delete=models.PROTECT)
    type = models.PositiveSmallIntegerField(choices=TYPE_CHOICES, verbose_name=u"错误类型")
    desc = models.TextField(verbose_name=u"错误描述")
    create_user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u'反馈人',related_name='exam_question_feedback_user', editable=False, on_delete=models.PROTECT)
    create_time = models.DateTimeField(verbose_name=u"反馈时间", default=timezone.now, editable=False)
    status = models.PositiveSmallIntegerField(choices=STATUS_CHOICES, verbose_name=u'处理状态', default=UNTREATED)
    process_user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u"处理人", on_delete=models.PROTECT, null=True, related_name="exam_question_feedback_ref_process_user")
    process_time = models.DateTimeField(verbose_name=u'处理时间', null=True)

    class Meta:
        db_table = "exam_question_feedback"
        ordering = ['-id']
        verbose_name = u"错误反馈"
        default_permissions = ()
