## $Id: browser.py 11581 2014-04-04 08:38:27Z henrik $
##
## Copyright (C) 2012 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 waeup.kofa.browser.layout import UtilityView
from waeup.kofa.browser.viewlets import ManageActionButton
from waeup.kofa.students.browser import OnlinePaymentDisplayFormPage as OPDPStudent
from waeup.kofa.applicants.browser import OnlinePaymentDisplayFormPage as OPDPApplicant
from kofacustom.nigeria.interswitch.helpers import (
    query_interswitch, write_payments_log)
from kofacustom.nigeria.payments.interfaces import INigeriaOnlinePayment
from kofacustom.nigeria.students.interfaces import INigeriaStudentOnlinePayment
from kofacustom.nigeria.applicants.interfaces import INigeriaApplicantOnlinePayment
from kofacustom.nigeria.interfaces import MessageFactory as _

class InterswitchActionButtonStudent(ManageActionButton):
    grok.order(1)
    grok.context(INigeriaOnlinePayment)
    grok.view(OPDPStudent)
    grok.require('waeup.payStudent')
    icon = 'actionicon_pay.png'
    text = _('CollegePAY')
    target = 'goto_interswitch'

    @property
    def target_url(self):
        if self.context.p_state != 'unpaid':
            return ''
        return self.view.url(self.view.context, self.target)

class InterswitchActionButtonApplicant(InterswitchActionButtonStudent):
    grok.view(OPDPApplicant)
    grok.require('waeup.payApplicant')

class InterswitchRequestWebserviceActionButtonStudent(ManageActionButton):
    grok.order(2)
    grok.context(INigeriaOnlinePayment)
    grok.view(OPDPStudent)
    grok.require('waeup.payStudent')
    icon = 'actionicon_call.png'
    text = _('Requery CollegePAY')
    target = 'request_webservice'

    @property
    def target_url(self):
        if self.context.p_state == 'paid':
            return ''
        return self.view.url(self.view.context, self.target)

class InterswitchRequestWebserviceActionButtonApplicant(
    InterswitchRequestWebserviceActionButtonStudent):
    grok.view(OPDPApplicant)
    grok.require('waeup.payApplicant')

class InterswitchPaymentRequestWebservicePageStudent(UtilityView, grok.View):
    """ Request webservice view for the CollegePAY gateway
    """
    grok.context(INigeriaStudentOnlinePayment)
    grok.name('request_webservice')
    grok.require('waeup.payStudent')

    product_id = None
    gateway_host = None
    gateway_url = None

    def update(self):
        if self.context.p_state == 'paid':
            self.flash(_('This ticket has already been paid.'), type='warning')
            return
        student = self.context.student
        success, msg, log = query_interswitch(
            self.context, self.product_id, self.gateway_host, self.gateway_url)
        student.writeLogMessage(self, log)
        if not success:
            self.flash(msg, type='danger')
            return
        write_payments_log(student.student_id, self.context)
        flashtype, msg, log = self.context.doAfterStudentPayment()
        if log is not None:
            student.writeLogMessage(self, log)
        self.flash(msg, type=flashtype)
        return

    def render(self):
        self.redirect(self.url(self.context, '@@index'))
        return

class InterswitchPaymentRequestWebservicePageApplicant(UtilityView, grok.View):
    """ Request webservice view for the CollegePAY gateway
    """
    grok.context(INigeriaApplicantOnlinePayment)
    grok.name('request_webservice')
    grok.require('waeup.payApplicant')

    product_id = None
    gateway_host = None
    gateway_url = None

    def update(self):
        if self.context.p_state == 'paid':
            self.flash(_('This ticket has already been paid.'), type='warning')
            return
        applicant = self.context.__parent__
        success, msg, log = query_interswitch(
            self.context, self.product_id, self.gateway_host, self.gateway_url)
        applicant.writeLogMessage(self, log)
        if not success:
            self.flash(msg, type='danger')
            return
        write_payments_log(applicant.applicant_id, self.context)
        flashtype, msg, log = self.context.doAfterApplicantPayment()
        if log is not None:
            applicant.writeLogMessage(self, log)
        self.flash(msg, type=flashtype)
        return

    def render(self):
        self.redirect(self.url(self.context, '@@index'))
        return
