from zope import schema, interface
from z3c.relationfield.schema import RelationList, RelationChoice
from z3c.form.object import registerFactoryAdapter

from plone.app.textfield import RichText
from plone.autoform import directives as form
from plone.formwidget.contenttree import ObjPathSourceBinder
from plone.namedfile.field import NamedBlobImage, NamedBlobFile
from plone.supermodel import model

from collective.z3cform.datagridfield import DictRow

from tutorweb.content import _


class ILaTeXQuestionAnswer(model.Schema):
    text = schema.TextLine(
        title=_(u'Answer text'),
        required=True)
    correct = schema.Bool(
        title=_(u'Correct?'),
        required=False)


class ILectureSettings(model.Schema):
    key = schema.Choice(
        title=_(u"Setting"),
        vocabulary='tutorweb.content.vocabularies.lectureSettings',
        required=True)
    value = schema.TextLine(
        title=_(u'Value'),
        required=True)


class IQuestion(model.Schema):
    """Marker for anything question-ish"""


class ILaTeXQuestion(model.Schema):
    """Static question (i.e. one not generated by R)"""
    title = schema.TextLine(
        title=_(u'Question Title'),
        description=_(u'The title of the question'),
        required=True)
    text = RichText(
        title=u"Question text",
        default_mime_type='text/x-tex',
        output_mime_type='text/html',
        allowed_mime_types=('text/html', 'text/x-tex',),
        required=False)
    image = NamedBlobImage(
        title=_(u'Question Image'),
        description=_(u'An image associated to the question'),
        required=False)
    choices = schema.List(
        title=_(u"Answers"),
        description=_(u'''Specify the answer text, and whether it is correct.
        Answers will be displayed in a randomized order.'''),
        value_type=DictRow(title=u"tablerow", schema=ILaTeXQuestionAnswer),
        required=False)
    form.widget(choices=
                'collective.z3cform.datagridfield.DataGridFieldFactory')
    finalchoices = schema.List(
        title=_(u"Fixed answers at end"),
        description=_(u'''Specify the answer text, and whether it is correct.
        Answers will be displayed in a fixed order at the end of the answer list.'''),
        value_type=DictRow(title=u"tablerow", schema=ILaTeXQuestionAnswer),
        required=False)
    form.widget(finalchoices=
                'collective.z3cform.datagridfield.DataGridFieldFactory')
    explanation = RichText(
        title=u"Explanation text",
        description=_(u'Displayed to the student after the question is asked'),
        default_mime_type='text/x-tex',
        output_mime_type='text/html',
        allowed_mime_types=('text/html', 'text/x-tex', 'text/x-web-intelligent'),
        required=False)
    timesanswered = schema.Int(
        title=_(u'Number of times answered'),
        default=0,
        required=True)
    timescorrect = schema.Int(
        title=_(u'Number of times correct'),
        default=0,
        required=True)


class ILecture(model.Schema):
    """A lecture contains Slides and quiz questions"""
    id = schema.TextLine(
        title=_(u'Lecture code'),
        description=_(u"Code for this lecture, e.g. 'lecture01'"),
        required=True)
    title = schema.TextLine(
        title=_(u'Title'),
        description=_(u'The title of the lecture'),
        required=True)
    settings = schema.List(
        title=_(u"Lecture settings"),
        description=_(u'e.g. historical selection'),
        value_type=DictRow(title=u"tablerow", schema=ILectureSettings),
        required=False)
    form.widget(settings=
                'collective.z3cform.datagridfield.DataGridFieldFactory')
    pdf = NamedBlobFile(
        title=_(u'Generated lecture PDF'),
        required=False)


class ITutorial(model.Schema):
    """A Tutorial contains lectures"""
    id = schema.TextLine(
        title=_(u'Tutorial code'),
        description=_(u"Code for this tutorial, e.g. 'FISH101.1'"),
        required=True)
    title = schema.TextLine(
        title=_(u'Title'),
        description=_(u'The title of the tutorial'),
        required=True)
    language = schema.Choice(
        title=_(u'Language'),
        description=_(u'Language tutorial notes are in'),
        vocabulary=u'plone.app.vocabularies.SupportedContentLanguages',
        default='English',
        required=True)
    author = schema.TextLine(
        title=_(u'Author'),
        description=_(u'Author(s) of the tutorial'),
        required=True)
    credits = schema.Int(
        title=_(u'Credits'),
        description=_(u'Number of Credits given for the tutorial'),
        default=0,
        required=True)
    settings = schema.List(
        title=_(u"Default lecture settings"),
        description=_(u"Lecture settings used if lecture doesn't override values"),
        value_type=DictRow(title=u"tablerow", schema=ILectureSettings),
        required=False)
    form.widget(settings=
                'collective.z3cform.datagridfield.DataGridFieldFactory')
    primarycourse = RelationChoice(
        title=_(u'Primary Course'),
        description=_(u"The main course this tutorial is part of"),
        source=ObjPathSourceBinder(Type='Course', navigation_tree_query=dict(
            Type=['department', 'Course'])),
        required=True)
    pdf = NamedBlobFile(
        title=_(u'Generated tutorial PDF'),
        required=False)


class IDepartment(model.Schema):
    id = schema.TextLine(
        title=_(u'Department code'),
        description=_(u"The short code for the department, e.g. 'FISH'"),
        required=True)
    title = schema.TextLine(
        title=_(u'Department title'),
        description=_(u"The name of the department, e.g. 'Fishery science department'"),
        required=True)


class ICourse(model.Schema):
    id = schema.TextLine(
        title=_(u'Course code'),
        description=_(u"The short code for the course, e.g. '101'"),
        required=True)
    title = schema.TextLine(
        title=_(u'Course title'),
        description=_(u"The name of the course"),
        required=True)
    tutorials = RelationList(
        title=_(u'Course tutorials'),
        description=_(u"All tutorials this course contains"),
        default=[],
        value_type=RelationChoice(
            source=ObjPathSourceBinder(Type='Tutorial', navigation_tree_query=dict(
                Type=['department', 'Tutorial'])),
        ),
        required=False)


class IClass(model.Schema):
    id = schema.TextLine(
        title=_(u'Class code'),
        description=_(u"The short code for the class, e.g. '101'"),
        required=True)
    title = schema.TextLine(
        title=_(u'Class title'),
        description=_(u"The name of the class"),
        required=True)
    students = schema.List(
        title=_(u'Students in class'),
        description=_(u"All students within the class"),
        default=[],
        value_type=schema.Choice(
            vocabulary='plone.app.vocabularies.Users',
        ),
        required=False)
    lectures = RelationList(
        title=_(u'Lectures for class'),
        description=_(u"Lectures"),
        default=[],
        value_type=RelationChoice(
            source=ObjPathSourceBinder(Type='Lecture'),
        ),
        required=False)


class ISlideSection(model.Schema):
    title = schema.TextLine(
        title=_(u'Section Title'),
        description=_(u"A title for this section, e.g. 'Explanation', 'Examples', 'Alternative', 'Handout'. Leave blank for main items"),
        default=u"",
        required=False)
    text = RichText(
        title=u"Text",
        default_mime_type='text/x-tex',
        output_mime_type='text/html',
        allowed_mime_types=('text/html', 'text/x-tex', 'text/x-rst',),
        #TODO: text/x-R
        default=u"",
        required=False)
    image_code = RichText(
        title=u"Code to generate image",
        default_mime_type='text/x-tex',
        output_mime_type='text/html',
        allowed_mime_types=('text/html', 'text/x-tex', 'text/x-rst',),
        #TODO: text/x-gnuplot, image/x-xfig, text/x-R
        #TODO: url (and allow images in lectures, to link to)
        default=u"",
        required=False)
    image_caption = schema.TextLine(
        title=_(u'Image caption'),
        description=_(u"Caption to go underneath images"),
        default=u"",
        required=False)


@interface.implementer(ISlideSection)
class SlideSection(object):
    title = schema.fieldproperty.FieldProperty(ISlideSection['title'])
    text = schema.fieldproperty.FieldProperty(ISlideSection['text'])
    image_code = schema.fieldproperty.FieldProperty(ISlideSection['image_code'])
    image_caption = schema.fieldproperty.FieldProperty(ISlideSection['image_caption'])
registerFactoryAdapter(ISlideSection, SlideSection)


class ISlide(model.Schema):
    id = schema.TextLine(
        title=_(u'Slide Id'),
        description=_(u"Change ID to become more readable. Slides appear in alphabetical order based on this value."),
        required=True)
    title = schema.TextLine(
        title=_(u'Slide title'),
        description=_(u"The main title of the slide"),
        required=False)
    sections = schema.List(
        title=_(u'Slide sections'),
        value_type=schema.Object(schema=ISlideSection),
        default=[],
        required=False)
