## $Id: browser.py 13068 2015-06-16 14:50:12Z henrik $
##
## Copyright (C) 2015 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.ikoba.interfaces import IIkobaUtils, IExtFileStore
from waeup.ikoba.browser.layout import action
from waeup.ikoba.customers.browser import (
    PDFContractSlip, CustomerBaseEditFormPage, ContractSelectProductPage, msave)
from ikobacustom.pcn.interfaces import MessageFactory as _
from ikobacustom.pcn.customers.interfaces import (
    IRPCContract, IAPPITContract, IAPPTContract)

class PDFContractSlip(PDFContractSlip):
    """Deliver pdf file including metadata.
    """

    @property
    def label(self):
        portal_language = getUtility(IIkobaUtils).PORTAL_LANGUAGE
        return self.context.title

    def _signatures(self):
        return ()

    def _sigsInFooter(self):
        return ()

class PDFRPCContractSlip(PDFContractSlip):
    """Deliver pdf file including metadata.
    """

    grok.context(IRPCContract)

    def _sigsInFooter(self):
        return (_('Date, Signature of Witness'),
                _('Date, Signature of Registrant'),
                )

class PDFAPPITContractSlip(PDFContractSlip):
    """Deliver pdf file including metadata.
    """

    grok.context(IAPPITContract)

    def _sigsInFooter(self):
        return (_('Date, Signature of Internee'),
                _('Date, Signature of Supervising Pharmacist'),
                )

class PDFAPPTContractSlip(PDFContractSlip):
    """Deliver pdf file including metadata.
    """

    grok.context(IAPPTContract)

    def _sigsInFooter(self):
        return (_('Date, Signature of Applicant'),
                _('Date, Signature of Supervisor'),
                )

class CustomerBaseEditFormPage(CustomerBaseEditFormPage):
    """ View to edit customer base data
    """

    def dataNotComplete(self):
        store = getUtility(IExtFileStore)
        error = ''
        if not store.getFileByContext(self.context, attr=u'birth_certificate.pdf'):
            error += _('Birth certificate is missing. ')
        if not store.getFileByContext(self.context, attr=u'passport.jpg'):
            error += _('Passport picture is missing.')
        if error:
            return error
        return


class ContractSelectProductPage(ContractSelectProductPage):
    """ Page to select a contract product

    This page is for both customers and officers.
    """

    @action(_('Save and proceed'), style='primary')
    def save(self, **data):
        msave(self, **data)
        self.context.contract_year = self.context.product_object.contract_year
        self.context.title = self.context.product_object.contract_autotitle
        self.context.tc_dict = self.context.product_object.tc_dict
        self.context.valid_from = self.context.product_object.valid_from
        self.context.valid_to = self.context.product_object.valid_to
        isCustomer = getattr(
            self.request.principal, 'user_type', None) == 'customer'
        if isCustomer:
            self.redirect(self.url(self.context, 'edit'))
        else:
            self.redirect(self.url(self.context, 'manage'))
        return
