# coding=utf-8

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

from apps.examination.examquestion.models import ExamQuestion, ExamQuestionOption

class PractiseLog(models.Model):
    SUBJECT = 1
    CHAPTER = 2
    TYPE_CHOICES = (
        (SUBJECT, u'科目'),
        (CHAPTER, u'章节'),
    )
    TYPE_JSON = [{'id': item[0], 'value': item[1]} for item in TYPE_CHOICES]

    type = models.PositiveSmallIntegerField(choices=TYPE_CHOICES, verbose_name=u"类型")
    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)
    submit_time = models.DateTimeField(verbose_name=u"结束时间", null=True, blank=True)

    begin_answer = models.ForeignKey('PractiseAnswerLog', verbose_name=u"开始习题", null=True, related_name='practise_log_begin_answer', editable=False, on_delete=models.PROTECT)
    end_answer = models.ForeignKey('PractiseAnswerLog', verbose_name=u"结束习题", null=True, related_name='practise_log_end_answer', editable=False, on_delete=models.PROTECT)
    right_count = models.IntegerField(verbose_name=u'正确数量', default=0, editable=False)
    wrong_count = models.IntegerField(verbose_name=u'错误数量', default=0, editable=False)
    total_count = models.IntegerField(verbose_name=u'总数量', default=0, editable=False)

    class Meta:
        db_table = "practise_log"
        ordering = ['-id']
        verbose_name = u"练习记录"
        default_permissions = ()

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

    main = models.ForeignKey(PractiseLog, verbose_name=u"练习记录", on_delete=models.PROTECT)
    question = models.ForeignKey(ExamQuestion, verbose_name=u"试题", on_delete=models.PROTECT)
    status = models.PositiveSmallIntegerField(choices=STATUS_CHOICES, verbose_name=u'回答状态')

    class Meta:
        db_table = "practise_answer_log"
        ordering = ['id']
        verbose_name = u"练习答题记录"
        default_permissions = ()

class PractiseAnswerOptionLog(models.Model):
    main = models.ForeignKey(PractiseLog, verbose_name=u"练习答题记录", on_delete=models.PROTECT)
    option = models.ForeignKey(ExamQuestionOption, verbose_name=u"选项", on_delete=models.PROTECT)

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

class PractiseAnswerFillLog(models.Model):
    main = models.ForeignKey(PractiseLog, verbose_name=u"练习答题记录", on_delete=models.PROTECT)
    content = models.TextField(verbose_name=u"内容")
    order = models.IntegerField(verbose_name=u'排序')

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