## $Id: viewlets.py 11997 2014-11-19 17:02:48Z 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 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)
from waeup.ikoba.customers.browser import (
    CustomersContainerPage, CustomersContainerManagePage,
    CustomerBaseDisplayFormPage)

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 + '/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 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'Trigger 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')
    text = _('Edit')
    target = 'edit_base'

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_portrait.png'
    text = _('Change portrait')
    target = 'change_portrait'
