"""Interfaces of academics specific objects.
"""
from zc.sourcefactory.basic import BasicSourceFactory
from zope import schema
try:
    from zope.catalog.interfaces import ICatalog
except ImportError:
    # BBB
    from zope.app.catalog.interfaces import ICatalog
from zope.component import getUtility
from zope.interface import Attribute
from waeup.sirp.interfaces import (IWAeUPObject, IWAeUPContainer,
                                   SimpleWAeUPVocabulary)

class CourseSource(BasicSourceFactory):
    """A course source delivers all courses inside the portal by looking
       up a catalog.
    """
    def getValues(self):
        catalog = getUtility(ICatalog, name='courses_catalog')
        return list(catalog.searchResults(code=('', 'z*')))

    def getToken(self, value):
        return value.code
        
    def getTitle(self, value):
        return "%s %s" % (value.code, value.title[:32])

class IFaculty(IWAeUPContainer):
    """Representation of a university faculty.
    """
    title = schema.TextLine(
        title = u'Name of Faculty',
        default = u'Unnamed',
        required = True,
        )

    title_prefix = schema.TextLine(
        title = u'Title prefix',
        default = u'faculty',
        required = True,
        )
    
    code = schema.TextLine(
        title = u'Code',
        description = u'Abbreviated code of the faculty',
        default = u'NA',
        required = True,
        )

class IFacultyContainer(IWAeUPContainer):
    """A container for faculties.
    """
    def addFaculty(faculty):
        """Add an IFactulty object.

        Returns the key, under which the object was stored.
        """
class IDepartment(IWAeUPObject):
    """Representation of a department.
    """
    title = schema.TextLine(
        title = u'Name of Department',
        default = u'Unnamed',
        required = True,
        )

    title_prefix = schema.TextLine(
        title = u'Title prefix',
        default = u'department',
        required = True,
        )
    
    code = schema.TextLine(
        title = u'Code',
        default = u'NA',
        description = u'Abbreviated code of the department',
        required = True,
        )

    courses = Attribute("A container for courses.")
    certificates = Attribute("A container for certificates.")


class ICourseContainer(IWAeUPContainer):
    """A container for faculties.
    """
    def addCourse(faculty):
        """Add an ICourse object.

        Returns the key, under which the object was stored.
        """

class ICourse(IWAeUPObject):
    """Representation of a course.
    """
    code = schema.TextLine(
        title = u'Code',
        default = u'NA',
        description = u'Abbreviated code of the course',
        required = True,
        readonly = True,
        )

    title = schema.TextLine(
        title = u'Title of course',
        default = u'Unnamed',
        required = True,
        )

    level = schema.TextLine(
        title = u'Level',
        default = u'100',
        required = False,
        )

    credits = schema.Int(
        title = u'Credits',
        default = 0,
        required = False,
        )
    
    passmark = schema.Int(
        title = u'Passmark',
        default = 40,
        required = False,
        )

    semester = schema.Choice(
        title = u'Semester/Term',
        default = 0,
        vocabulary = SimpleWAeUPVocabulary(
            ('N/A', 0), ('First Semester', 1),
            ('Second Semester', 2), ('Combined', 3)),
        required = True,
        )


class ICertificate(IWAeUPObject):
    """Representation of a certificate.
    """
    code = schema.TextLine(
        title = u'Code',
        default = u'NA',
        description = u'Abbreviated code of the certificate.',
        required = True,
        )

    review_state = schema.Choice(
        title = u'review state',
        default = 'unchecked',
        values = ['unchecked', 'checked']
        )

    title = schema.TextLine(
        title = u'title',
        required = True,
        )

    study_mode = schema.TextLine(
        title = u'study mode',
        required = True,
        )

    start_level = schema.TextLine(
        title = u'start level',
        required = True,
        )
    
    end_level = schema.TextLine(
        title = u'end level',
        required = True,
        )
    
    application_category = schema.TextLine(
        title = u'application category',
        required = False,
        )
    
    max_pass = schema.TextLine(
        title = u'maximum pass',
        required = False,
        )
    

    
class ICertificateContainer(IWAeUPContainer):
    """A container for certificates.
    """
    def addCertificate(faculty):
        """Add an ICertificate object.

        Returns the key, under which the object was stored.
        """

class ICertificateCourse(IWAeUPObject):
    """A certificatecourse is a course referenced by a certificate, which
       provides some own attributes.
    """
    course = schema.Choice(
        title = u'Course',
        source = CourseSource(),
        )
    
    level = schema.Int(
        title = u'Level of this course',
        required = True,
        default = 100
        )

    core_or_elective = schema.Bool(
        title = u'Is mandatory course (not elective)',
        required = True,
        default = True
        )

    def getCourseCode():
        """Return the code of the referenced course.

        This is needed for cataloging.
        """
