## $Id: viewlets.py 16256 2020-09-29 11:06:48Z henrik $
##
## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##

import grok
from zope.component import getUtility
from waeup.kofa.interfaces import REQUESTED, IExtFileStore
from waeup.kofa.browser.viewlets import ManageActionButton
from waeup.kofa.students.interfaces import IStudent, IStudentsUtils
from kofacustom.iuokada.students.interfaces import (
    ICustomStudentStudyCourse, ICustomStudentStudyLevel, ICustomStudent,
    ICustomStudentStudyCourse)
from waeup.kofa.students.fileviewlets import (
    StudentFileDisplay, StudentFileUpload, StudentImage)
from waeup.kofa.students.browser import (
    ExportPDFClearanceSlip, StudyCourseDisplayFormPage,
    StudyLevelDisplayFormPage, StudentBaseDisplayFormPage)
from waeup.kofa.students.viewlets import (
    AddPreviousPaymentActionButton, AddBalancePaymentActionButton,
    StudentPersonalEditActionButton)

from kofacustom.nigeria.interfaces import MessageFactory as _

class StudentPersonalEditActionButton(StudentPersonalEditActionButton):
    text = _('Edit registration bio data')

class SwitchLibraryAccessActionButton(ManageActionButton):
    grok.order(7)
    grok.context(ICustomStudent)
    grok.view(StudentBaseDisplayFormPage)
    grok.require('waeup.switchLibraryAccess')
    text = _('Switch library access')
    target = 'switch_library_access'
    icon = 'actionicon_book.png'

class MakePaymentActionButton(ManageActionButton):
    grok.order(8)
    grok.context(ICustomStudent)
    grok.view(StudentBaseDisplayFormPage)
    grok.require('waeup.handleStudent')
    text = _('Make payment now')
    target = 'payments/addop'
    icon = 'actionicon_pay.png'

class EditBioDataActionButton(ManageActionButton):
    grok.order(9)
    grok.context(ICustomStudent)
    grok.view(StudentBaseDisplayFormPage)
    grok.require('waeup.handleStudent')
    text = _('Edit registration bio data now')
    target = 'edit_personal'

class PersonalDataSlipActionButton(ManageActionButton):
    grok.order(10)
    grok.context(ICustomStudent)
    grok.view(StudentBaseDisplayFormPage)
    grok.require('waeup.viewStudent')
    text = _('Download bio data slip')
    target = 'personal_slip.pdf'
    icon = 'actionicon_pdf.png'

class AddBalancePaymentActionButton(AddBalancePaymentActionButton):
    grok.require('waeup.payStudent')

class GetMatricNumberActionButton(ManageActionButton):
    grok.order(10)
    grok.context(ICustomStudent)
    grok.view(StudentBaseDisplayFormPage)
    grok.require('waeup.manageStudent')
    icon = 'actionicon_count.png'
    text = _('Get Matriculation Number')

    @property
    def target_url(self):
        students_utils = getUtility(IStudentsUtils)
        if self.context.matric_number:
            return ''
        error, matric_number = students_utils.constructMatricNumber(
            self.context)
        if error:
            return ''
        return self.view.url(self.view.context, 'get_matric_number')

class StudyCourseDataSlipActionButton(ManageActionButton):
    grok.order(10)
    grok.context(ICustomStudentStudyCourse)
    grok.view(StudyCourseDisplayFormPage)
    grok.require('waeup.viewStudent')
    text = _('Download study course slip')
    target = 'studycourse_slip.pdf'
    icon = 'actionicon_pdf.png'

# Library

class LibraryIdCardActionButton(ManageActionButton):
    grok.order(10)
    grok.context(ICustomStudent)
    grok.view(StudentBaseDisplayFormPage)
    grok.require('waeup.viewStudent')
    icon = 'actionicon_pdf.png'
    text = _('Download Library Id Card')
    target = 'lib_idcard.pdf'

    @property
    def target_url(self):
        if self.context.library:
            return self.view.url(self.view.context, self.target)
        return

# Signature

class SignatureDisplay(StudentFileDisplay):
    """Signature display viewlet.
    """
    grok.order(2)
    label = _(u'Signature')
    title = _(u'Signature Scan')
    download_name = u'signature'

class SignatureSlip(SignatureDisplay):
    grok.view(ExportPDFClearanceSlip)

class SignatureUpload(StudentFileUpload):
    """Signature upload viewlet.
    """
    grok.order(2)
    label = _(u'Signature')
    title = _(u'Signature Scan')
    download_name = u'signature'

class SignatureImage(StudentImage):
    """Renders signature scan.
    """
    grok.name('signature')
    download_name = u'signature'

class SignatureDownloadButton(ManageActionButton):

    grok.order(12)
    grok.context(IStudent)
    grok.view(StudentBaseDisplayFormPage)
    grok.require('waeup.manageStudent')
    target = 'signature'
    text = _('Signature')
    icon = 'actionicon_signature.png'

    @property
    def target_url(self):
        image = getUtility(IExtFileStore).getFileByContext(
            self.context, attr='signature')
        if not image:
            return ''
        return self.view.url(self.view.context, self.target)

# JAMB Result

class JAMBResultDisplay(StudentFileDisplay):
    """JAMB Result display viewlet.
    """
    grok.order(9)
    label = _(u'JAMB Result')
    title = _(u'JAMB Result')
    download_name = u'jamb'

class JAMBResultSlip(JAMBResultDisplay):
    grok.view(ExportPDFClearanceSlip)

class JAMBResultUpload(StudentFileUpload):
    """JAMB Result upload viewlet.
    """
    grok.order(9)
    label = _(u'JAMB Result')
    title = _(u'JAMB Result Scan')
    download_name = u'jamb'

class JAMBResultImage(StudentImage):
    """Renders JAMB Result scan.
    """
    grok.name('jamb')
    download_name = u'jamb'

