## $Id: viewlets.py 12757 2015-03-12 22:28:55Z henrik $
##
## Copyright (C) 2014 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.i18n import translate
from zope.interface import Interface
from zope.component import getUtility
from waeup.ikoba.interfaces import IIkobaObject
from waeup.ikoba.interfaces import MessageFactory as _
from waeup.ikoba.browser.viewlets import (
    PrimaryNavTab, ManageActionButton, AddActionButton)
from waeup.ikoba.browser.layout import default_primary_nav_template
from waeup.ikoba.customers.interfaces import (
    ICustomer, ICustomersContainer,
    ICustomerDocumentsContainer, ICustomerDocument,
    IContractsContainer, IContract, ICustomersUtils)
from waeup.ikoba.customers.browser import (
    CustomersContainerPage, CustomersContainerManagePage,
    CustomerBaseDisplayFormPage,
    DocumentsManageFormPage, DocumentDisplayFormPage, DocumentManageFormPage,
    ContractsFormPage, ContractDisplayFormPage,
    ContractManageFormPage,
    ContractOfficialUsePage)

grok.context(IIkobaObject)  # Make IIkobaObject the default context
grok.templatedir('browser_templates')


class CustomersTab(PrimaryNavTab):
    """Customers tab in primary navigation.
    """

    grok.context(IIkobaObject)
    grok.order(4)
    grok.require('waeup.viewCustomersTab')
    grok.name('customerstab')

    pnav = 4
    tab_title = _(u'Customers')

    @property
    def link_target(self):
        return self.view.application_url('customers')


class PrimaryCustomerNavManager(grok.ViewletManager):
    """Viewlet manager for the primary navigation tab.
    """
    grok.name('primary_nav_customer')


class PrimaryCustomerNavTab(grok.Viewlet):
    """Base for primary customer nav tabs.
    """
    grok.baseclass()
    grok.viewletmanager(PrimaryCustomerNavManager)
    template = default_primary_nav_template
    grok.order(1)
    grok.require('waeup.Authenticated')
    pnav = 0
    tab_title = u'Some Text'

    @property
    def link_target(self):
        return self.view.application_url()

    @property
    def active(self):
        view_pnav = getattr(self.view, 'pnav', 0)
        if view_pnav == self.pnav:
            return 'active'
        return ''


class MyCustomerDataTab(PrimaryCustomerNavTab):
    """MyData dropdown tab in primary navigation.
    """
    grok.order(3)
    grok.require('waeup.viewMyCustomerDataTab')
    grok.template('mydatadropdowntabs')
    grok.name('mycustomerdatatab')
    pnav = 4
    tab_title = _(u'My Data')

    @property
    def active(self):
        view_pnav = getattr(self.view, 'pnav', 0)
        if view_pnav == self.pnav:
            return 'active dropdown'
        return 'dropdown'

    @property
    def targets(self):
        customer = grok.getSite()['customers'][self.request.principal.id]
        customer_url = self.view.url(customer)
        targets = []
        targets += [
            {'url':customer_url, 'title':'Base Data'},
            {'url':customer_url + '/contracts', 'title':_('My Contracts')},
            {'url':customer_url + '/documents', 'title':_('My Documents')},
            {'url':customer_url + '/history', 'title':_('History')},
            ]
        return targets


class CustomerManageSidebar(grok.ViewletManager):
    grok.name('left_customermanage')


class CustomerManageLink(grok.Viewlet):
    """A link displayed in the customer box which shows up for CustomerNavigation
    objects.

    """
    grok.baseclass()
    grok.viewletmanager(CustomerManageSidebar)
    grok.view(Interface)
    grok.order(5)
    grok.require('waeup.viewCustomer')

    link = 'index'
    text = _(u'Base Data')

    def render(self):
        url = self.view.url(self.context.customer, self.link)
        # Here we know that the cookie has been set
        lang = self.request.cookies.get('ikoba.language')
        text = translate(self.text, 'waeup.ikoba',
            target_language=lang)
        if not self.link:
            return ''
        return u'<li><a href="%s">%s</a></li>' % (
                url, text)


class CustomerManageBaseLink(CustomerManageLink):
    grok.order(2)
    link = 'index'
    text = _(u'Base Data')


class CustomerManageContractsLink(CustomerManageLink):
    grok.order(3)
    link = 'contracts'
    text = _(u'Contracts')


class CustomerManageDocumentsLink(CustomerManageLink):
    grok.order(4)
    link = 'documents'
    text = _(u'Documents')

class CustomerManagePaymentsLink(CustomerManageLink):
    grok.order(5)
    link = 'payments'
    text = _(u'Payments')


class CustomerManageHistoryLink(CustomerManageLink):
    grok.order(8)
    link = 'history'
    text = _(u'History')


class CustomersContainerManageActionButton(ManageActionButton):
    grok.order(1)
    grok.context(ICustomersContainer)
    grok.view(CustomersContainerPage)
    grok.require('waeup.manageCustomer')
    text = _('Manage customer section')


class CustomersContainerAddActionButton(AddActionButton):
    grok.order(1)
    grok.context(ICustomersContainer)
    grok.view(CustomersContainerManagePage)
    grok.require('waeup.manageCustomer')
    text = _('Add customer')
    target = 'addcustomer'


class ContactActionButton(ManageActionButton):
    grok.order(5)
    grok.context(ICustomer)
    grok.view(CustomerBaseDisplayFormPage)
    grok.require('waeup.manageCustomer')
    icon = 'actionicon_mail.png'
    text = _('Send email')
    target = 'contactcustomer'


class CustomerBaseManageActionButton(ManageActionButton):
    grok.order(1)
    grok.context(ICustomer)
    grok.view(CustomerBaseDisplayFormPage)
    grok.require('waeup.manageCustomer')
    text = _('Manage')
    target = 'manage_base'


class CustomerTrigTransActionButton(ManageActionButton):
    grok.order(2)
    grok.context(ICustomer)
    grok.view(CustomerBaseDisplayFormPage)
    grok.require('waeup.triggerTransition')
    icon = 'actionicon_trigtrans.png'
    text = _(u'Transition')
    target = 'trigtrans'


class CustomerLoginAsActionButton(ManageActionButton):
    grok.order(3)
    grok.context(ICustomer)
    grok.view(CustomerBaseDisplayFormPage)
    grok.require('waeup.loginAsCustomer')
    icon = 'actionicon_mask.png'
    text = _(u'Login as customer')
    target = 'loginasstep1'


class CustomerDeactivateActionButton(ManageActionButton):
    grok.order(7)
    grok.context(ICustomer)
    grok.view(CustomerBaseDisplayFormPage)
    grok.require('waeup.manageCustomer')
    text = _('Deactivate account')
    target = 'deactivate'
    icon = 'actionicon_traffic_lights_red.png'

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

    @property
    def onclick(self):
        return "return window.confirm(%s);" % _(
            "'A history message will be added. Are you sure?'")


class CustomerActivateActionButton(ManageActionButton):
    grok.order(7)
    grok.context(ICustomer)
    grok.view(CustomerBaseDisplayFormPage)
    grok.require('waeup.manageCustomer')
    text = _('Activate account')
    target = 'activate'
    icon = 'actionicon_traffic_lights_green.png'

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

    @property
    def onclick(self):
        return "return window.confirm(%s);" % _(
            "'A history message will be added. Are you sure?'")


class CustomerBaseActionButton(ManageActionButton):
    grok.order(1)
    grok.context(ICustomer)
    grok.view(CustomerBaseDisplayFormPage)
    grok.require('waeup.handleCustomer')
    target = 'edit_base'

    @property
    def text(self):
        if self.view.is_requestable:
            return _('Edit and request registration')
        return _('Edit')


class CustomerPasswordActionButton(ManageActionButton):
    grok.order(2)
    grok.context(ICustomer)
    grok.view(CustomerBaseDisplayFormPage)
    grok.require('waeup.handleCustomer')
    icon = 'actionicon_key.png'
    text = _('Change password')
    target = 'changepassword'


class CustomerPassportActionButton(ManageActionButton):
    grok.order(3)
    grok.context(ICustomer)
    grok.view(CustomerBaseDisplayFormPage)
    grok.require('waeup.handleCustomer')
    icon = 'actionicon_up.png'
    text = _('Upload files')
    target = 'upload_files'

    @property
    def target_url(self):
        CUSTMANAGE_STATES = getUtility(
            ICustomersUtils).CUSTMANAGE_CUSTOMER_STATES
        if self.context.state not in CUSTMANAGE_STATES:
            return ''
        return self.view.url(self.view.context, self.target)


# Viewlets for customer documents

class PDFDocumentOverviewActionButton(ManageActionButton):
    grok.order(2)
    grok.context(ICustomerDocumentsContainer)
    grok.view(DocumentsManageFormPage)
    grok.require('waeup.viewCustomer')
    icon = 'actionicon_pdf.png'
    text = _('Download documents overview')
    target = 'documents_overview_slip.pdf'


class DocumentManageActionButton(ManageActionButton):
    grok.order(1)
    grok.context(ICustomerDocument)
    grok.view(DocumentDisplayFormPage)
    grok.require('waeup.manageCustomer')
    text = _('Manage')
    target = 'manage'

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


class DocumentEditActionButton(ManageActionButton):
    grok.order(1)
    grok.context(ICustomerDocument)
    grok.view(DocumentDisplayFormPage)
    grok.require('waeup.handleCustomer')
    text = _('Edit')
    target = 'edit'

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


class DocumentTrigTransActionButton(ManageActionButton):
    grok.order(2)
    grok.context(ICustomerDocument)
    grok.view(DocumentDisplayFormPage)
    grok.require('waeup.triggerTransition')
    icon = 'actionicon_trigtrans.png'
    text = _(u'Transition')
    target = 'trigtrans'


class DocumentViewActionButton(ManageActionButton):
    grok.order(1)
    grok.context(ICustomerDocument)
    grok.view(DocumentManageFormPage)
    grok.require('waeup.handleCustomer')
    text = _('View')
    target = 'index'
    icon = 'actionicon_view.png'


class PDFDocumentSlipActionButton(ManageActionButton):
    grok.order(3)
    grok.context(ICustomerDocument)
    grok.view(DocumentDisplayFormPage)
    grok.require('waeup.viewCustomer')
    icon = 'actionicon_pdf.png'
    text = _('Download document slip')
    target = 'document_slip.pdf'


# Viewlets for customer contracts

class PDFContractOverviewActionButton(ManageActionButton):
    grok.order(2)
    grok.context(IContractsContainer)
    grok.view(ContractsFormPage)
    grok.require('waeup.viewCustomer')
    icon = 'actionicon_pdf.png'
    text = _('Download contracts overview')
    target = 'contracts_overview_slip.pdf'


class ContractManageActionButton(ManageActionButton):
    grok.order(1)
    grok.context(IContract)
    grok.view(ContractDisplayFormPage)
    grok.require('waeup.manageCustomer')
    text = _('Manage')
    target = 'manage'


class ContractOUActionButton(ManageActionButton):
    grok.order(2)
    grok.context(IContract)
    grok.view(ContractDisplayFormPage)
    grok.require('waeup.manageCustomer')
    text = _('Manage official data')
    target = 'official_use'


class ContractEditActionButton(ManageActionButton):
    grok.order(1)
    grok.context(IContract)
    grok.view(ContractDisplayFormPage)
    grok.require('waeup.handleCustomer')
    text = _('Edit')
    target = 'edit'

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


class ContractTrigTransActionButton(ManageActionButton):
    grok.order(3)
    grok.context(IContract)
    grok.view(ContractDisplayFormPage)
    grok.require('waeup.triggerTransition')
    icon = 'actionicon_trigtrans.png'
    text = _(u'Transition')
    target = 'trigtrans'


class ContractViewActionButton(ManageActionButton):
    grok.order(1)
    grok.context(IContract)
    grok.view(ContractManageFormPage)
    grok.require('waeup.handleCustomer')
    text = _('View')
    target = 'index'
    icon = 'actionicon_view.png'


class ContractViewActionButton2(ContractViewActionButton):
    grok.view(ContractOfficialUsePage)


class PDFContractSlipActionButton(ManageActionButton):
    grok.order(4)
    grok.context(IContract)
    grok.view(ContractDisplayFormPage)
    grok.require('waeup.viewCustomer')
    icon = 'actionicon_pdf.png'
    text = _('Download contract slip')
    target = 'contract_slip.pdf'

