## $Id: contracts.py 12289 2014-12-21 22:17:06Z 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, IIDSource)
from waeup.ikoba.customers.interfaces import (
    IContractsContainer, ICustomerNavigation,
    IContract, IContractEdit, ICustomersUtils,
    ISampleContract, ISampleContractEdit)
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(IContract, IContractEdit, ICustomerNavigation)
    grok.provides(IContract)
    grok.baseclass()

    check_docs_interface = None

    contract_category = None

    form_fields_interface = None

    edit_form_fields_interface = None

    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
        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_by_customer(self):
        try:
            # Customer must be approved
            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):
        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


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

    grok.implements(ISampleContract, ISampleContractEdit, ICustomerNavigation)

    contract_category = 'sample'

    form_fields_interface = ISampleContract

    edit_form_fields_interface = ISampleContractEdit

    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
