## $Id: contracts.py 12747 2015-03-12 08:36:30Z 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 grok
from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
from zope.catalog.interfaces import ICatalog
from zope.component import getUtility, queryUtility
from zope.component.interfaces import IFactory
from zope.interface import implementedBy
from zope.schema import getFields
from waeup.ikoba.interfaces import MessageFactory as _
from waeup.ikoba.interfaces import (
    IObjectHistory, VERIFIED, APPROVED, PROVISIONALLY, IIDSource)
from waeup.ikoba.customers.interfaces import (
    IContractsContainer, ICustomerNavigation, IContract,
    IContractSelectProduct, ICustomersUtils, ISampleContract,
    ISampleContractProcess, ISampleContractEdit, ISampleContractOfficialUse)
from waeup.ikoba.payments.interfaces import (
    IPayer, IPayableFinder, IPayable, IPaymentWaitingForGatewayEvent,
    STATE_PAID, STATE_FAILED, IPaymentFinishedEvent
    )
from waeup.ikoba.payments.payment import (
    PaymentItem, find_payable_from_payable_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 ContractPayer(grok.Adapter):
    """Adapter to turn contracts into IPayers.
    """
    grok.implements(IPayer)
    grok.context(IContract)

    def __init__(self, context):
        self.context = context

    @property
    def _customer(self):
        return self.context.customer

    @property
    def first_name(self):
        return getattr(self._customer, 'firstname', None)

    @property
    def last_name(self):
        return getattr(self._customer, 'lastname', None)

    @property
    def payer_id(self):
        return getattr(self._customer, 'customer_id', None)


class ContractBase(grok.Container):
    """This is a customer contract baseclass.
    """
    grok.implements(IContractSelectProduct)  # Neccessary for the
                                             # selectproduct page (why?)
    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


@grok.subscribe(IPaymentWaitingForGatewayEvent)
def handle_payment_waiting_for_gw(event):
    maybe_contract = find_payable_from_payable_id(
        event.object.payable_id)
    if IContract.providedBy(maybe_contract):
        IWorkflowInfo(maybe_contract).fireTransition('await')


@grok.subscribe(IPaymentFinishedEvent)
def handle_payment_finished(event):
    payment = event.object
    maybe_contract = find_payable_from_payable_id(payment.payable_id)
    if not IContract.providedBy(maybe_contract):
        return
    if payment.state == STATE_PAID:
        IWorkflowInfo(maybe_contract).fireTransition('confirm')
    else:
        IWorkflowInfo(maybe_contract).fireTransition('abort')


class ContractFinder(grok.GlobalUtility):
    grok.name('contracts_finder')
    grok.implements(IPayableFinder)

    def get_payable_by_id(self, contract_id):
        catalog = queryUtility(ICatalog, 'contracts_catalog')
        if catalog is None:
            return None
        result = catalog.searchResults(
            contract_id=(contract_id, contract_id))
        result = [x for x in result]
        if not result:
            return None
        # there should not be more than one result really.
        return result[0]


class PayableContract(grok.Adapter):
    """Adapter to adapt IContracts to IPayable.
    """

    grok.context(IContract)
    grok.implements(IPayable)

    def __init__(self, context):
        self.context = context
        currencies = set([x.currency for x in context.product_options])
        if len(currencies) > 1:
            raise ValueError(
                "Only contracts with same currency for all options allowed.")
        return

    @property
    def payable_id(self):
        return self.context.contract_id

    @property
    def title(self):
        return self.context.title or u''

    @property
    def currency(self):
        if not len(self.context.product_options):
            return None
        return self.context.product_options[0].currency

    @property
    def payment_items(self):
        result = []
        for num, option in enumerate(self.context.product_options):
            item = PaymentItem()
            item.item_id = u'%s' % num
            item.title = option.title
            item.amount = option.fee
            result.append(item)
        return tuple(result)
