## $Id: contracts.py 12663 2015-03-05 07:28:31Z 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
##
"""
Customer contract components.
"""
import os
import grok
from zope.component import queryUtility, getUtility
from zope.component.interfaces import IFactory
from zope.interface import implementedBy
from zope.schema import getFields
from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
from waeup.ikoba.interfaces import MessageFactory as _
from waeup.ikoba.interfaces import (
    IIkobaUtils, IObjectHistory,
    VERIFIED, APPROVED, PROVISIONALLY,
    IIDSource)
from waeup.ikoba.customers.interfaces import (
    IContractsContainer, ICustomerNavigation,
    IContract, IContractSelectProduct, ICustomersUtils,
    ISampleContract, ISampleContractProcess, ISampleContractEdit,
    ISampleContractOfficialUse)
from waeup.ikoba.customers.utils import generate_contract_id
from waeup.ikoba.utils.helpers import attrs_to_fields

class ContractsContainer(grok.Container):
    """This is a container for customer contracts.
    """
    grok.implements(IContractsContainer, ICustomerNavigation)
    grok.provides(IContractsContainer)

    def addContract(self, contract):
        if not IContract.providedBy(contract):
            raise TypeError(
                'ContractsContainers contain only IContract instances')
        self[contract.contract_id] = contract
        return

    @property
    def customer(self):
        return self.__parent__

    def writeLogMessage(self, view, message):
        return self.__parent__.writeLogMessage(view, message)

ContractsContainer = attrs_to_fields(ContractsContainer)

class ContractBase(grok.Container):
    """This is a customer contract baseclass.
    """
    grok.implements(IContractSelectProduct)  # Necesary for the selectproduct page

    grok.baseclass()

    def __init__(self):
        super(ContractBase, self).__init__()
        # The site doesn't exist in unit tests
        source = getUtility(IIDSource)
        self.contract_id = unicode(source.get_hex_uuid())
        self.last_product_id = None
        self.tc_dict = {}
        self.title = None
        self.valid_to = None
        self.valid_from = None
        return

    @property
    def history(self):
        history = IObjectHistory(self)
        return history

    @property
    def state(self):
        return IWorkflowState(self).getState()

    @property
    def translated_state(self):
        try:
            TRANSLATED_STATES = getUtility(
                ICustomersUtils).TRANSLATED_CONTRACT_STATES
            return TRANSLATED_STATES[self.state]
        except KeyError:
            return

    @property
    def class_name(self):
        return self.__class__.__name__

    @property
    def formatted_transition_date(self):
        try:
            return self.history.messages[-1].split(' - ')[0]
        except IndexError:
            return

    @property
    def customer(self):
        try:
            return self.__parent__.__parent__
        except AttributeError:
            return None

    @property
    def user_id(self):
        if self.customer is not None:
            return self.customer.customer_id
        return

    def writeLogMessage(self, view, message):
        return self.__parent__.__parent__.writeLogMessage(view, message)

    @property
    def is_editable(self):
        try:
            # Customer must have requested
            cond1 = self.customer.state in getUtility(
                ICustomersUtils).CONMANAGE_CUSTOMER_STATES
            # Contract must be in state created
            cond2 = self.state in getUtility(
                ICustomersUtils).CONMANAGE_CONTRACT_STATES
            if not (cond1 and cond2):
                return False
        except AttributeError:
            pass
        return True

    @property
    def is_approvable(self):
        if self.customer and not self.customer.state in (
            APPROVED, PROVISIONALLY):
            return False, _("Customer has not yet been approved.")
        for key, field in getFields(self.check_docs_interface).items():
            if key.endswith('_object'):
                obj = getattr(self, key, None)
                state = getattr(obj, 'state', None)
                if state and state != VERIFIED:
                    return False, _("Attached documents must be verified first.")
        return True, None

    @property
    def translated_class_name(self):
        try:
            CONTYPES_DICT = getUtility(ICustomersUtils).CONTYPES_DICT
            return CONTYPES_DICT[self.class_name]
        except KeyError:
            return

    @property
    def fee_based(self):
        if self.product_options:
            amount = 0
            for option in self.product_options:
                amount += option.fee
            if amount:
                return True
        return False


class SampleContract(ContractBase):
    """This is a sample contract.
    """

    grok.implements(
        ISampleContractProcess, # must come before ISampleContract
        ISampleContract,
        ISampleContractEdit,
        ICustomerNavigation)

    contract_category = 'sample'

    form_fields_interface = ISampleContract

    edit_form_fields_interface = ISampleContractEdit

    ou_form_fields_interface = ISampleContractOfficialUse

    check_docs_interface = ISampleContract

SampleContract = attrs_to_fields(SampleContract)


# Contracts must be importable. So we might need a factory.
class SampleContractFactory(grok.GlobalUtility):
    """A factory for contracts.
    """
    grok.implements(IFactory)
    grok.name(u'waeup.SampleContract')
    title = u"Create a new contract.",
    description = u"This factory instantiates new sample contract instances."

    def __call__(self, *args, **kw):
        return SampleContract(*args, **kw)

    def getInterfaces(self):
        return implementedBy(SampleContract)

@grok.subscribe(IContract, grok.IObjectAddedEvent)
def handle_contract_added(contract, event):
    """If an contract is added the transition create is fired.
    The latter produces a logging message.
    """
    if IWorkflowState(contract).getState() is None:
        IWorkflowInfo(contract).fireTransition('create')
    return
