"""Vocabularies and sources for the application section.
"""
from datetime import datetime
from zope.component import getUtility
from zope.catalog.interfaces import ICatalog
from zc.sourcefactory.basic import BasicSourceFactory
from zc.sourcefactory.contextual import BasicContextualSourceFactory
from waeup.sirp.interfaces import SimpleWAeUPVocabulary
from waeup.sirp.applicants.lgas import LGAS

#: Types of applications we support.
APPLICATION_TYPES = (
    ('General Studies', 'app','APP'),
    ('Pre-NCE Programme', 'prence','PRE'),
    ('Post UME Screening Test', 'pume','PUME'),
    ('Post UDE Screening', 'pude','PUDE'),
    ('Part Time Degree in Education', 'sandwich','SAND'),
    ('Part-Time Degree Programmes', 'pt','PTP'),
    ('Diploma Programmes', 'dp','DPP'),
    ('PCE Screening', 'pce','PCE'),
    ('Certificate Programmes', 'ct','CTP'),
    ('Common Entry Screening Test', 'cest','CEST'),
    )

#: A :class:`waeup.sirp.interfaces.SimpleWAeUPVocabulary` of supported
#: application or screening types.
application_types_vocab = SimpleWAeUPVocabulary(
    *[(x[0],x[1]) for x in APPLICATION_TYPES])
application_pins_vocab = SimpleWAeUPVocabulary(
    *[(u"%s (%s)" % (x[2],x[0]),x[2]) for x in APPLICATION_TYPES])
lgas_vocab = SimpleWAeUPVocabulary(
    *sorted([(x[1],x[0]) for x in LGAS]))

def year_range():
    curr_year = datetime.now().year
    return range(curr_year - 2, curr_year + 5)

def entry_sessions():
    curr_year = datetime.now().year
    year_range = range(curr_year - 5, curr_year + 2)
    return [('%s/%s' % (year,year+1), '%s' % year) for year in year_range]

entry_session_vocab = SimpleWAeUPVocabulary(*entry_sessions())

class CertificateSource(BasicContextualSourceFactory):
    """A certificate source delivers all certificates provided
    in the portal.
    """
    def getValues(self, context):
        catalog = getUtility(ICatalog, name='certificates_catalog')
        return sorted(list(
                catalog.searchResults(
                    code=('', 'z*'))),
                    key=lambda value: value.code)

    def getToken(self, context, value):
        return value.code

    def getTitle(self, context, value):
        return "%s - %s" % (value.code, value.title[:64])

class AppCatCertificateSource(CertificateSource):
    """An application certificate source delivers all courses which belong to
    a certain application_category.
    """
    def getValues(self, context):
        appcat = context.__parent__.application_category
        catalog = getUtility(ICatalog, name='certificates_catalog')
        return sorted(list(
                catalog.searchResults(
                    code=('', 'z*'),
                    application_category=(appcat,appcat))),
                    key=lambda value: value.code)

class GenderSource(BasicSourceFactory):
    """A gender source delivers basically a mapping
       ``{'m': 'Male', 'f': 'Female'}``

       Using a source, we make sure that the tokens (which are
       stored/expected for instance from CSV files) are something one
       can expect and not cryptic IntIDs.
    """
    def getValues(self):
        return ['m', 'f']

    def getToken(self, value):
        return value[0].lower()

    def getTitle(self, value):
        if value == 'm':
            return 'Male'
        if value == 'f':
            return 'Female'
