## $Id: catalog.py 12155 2014-12-05 18:30:41Z 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
##
"""Cataloging and searching components for customers.
"""
import grok
from grok import index
from hurry.query import Eq, Text
from hurry.query.query import Query
from zope.catalog.interfaces import ICatalog
from zope.component import queryUtility
from waeup.ikoba.catalog import FilteredCatalogQueryBase
from waeup.ikoba.interfaces import (
    ICompany, IQueryResultItem)
from waeup.ikoba.customers.interfaces import ICustomer, IContract


class CustomersCatalog(grok.Indexes):
    """A catalog for customers.
    """
    grok.site(ICompany)
    grok.name('customers_catalog')
    grok.context(ICustomer)

    customer_id = index.Field(attribute='customer_id')
    fullname = index.Text(attribute='fullname')
    email = index.Field(attribute='email')
    reg_number = index.Field(attribute='reg_number')
    state = index.Field(attribute='state')


class CustomerQueryResultItem(object):
    grok.implements(IQueryResultItem)

    title = u'Customer Query Item'
    description = u'Some customer found in a search'

    def __init__(self, context, view):
        self.context = context
        self.url = view.url(context)
        self.customer_id = context.customer_id
        self.display_fullname = context.display_fullname
        self.reg_number = context.reg_number
        self.state = context.state
        self.translated_state = context.translated_state


def search(query=None, searchtype=None, view=None):
    hitlist = []
    if searchtype in ('fullname',):
        results = Query().searchResults(
            Text(('customers_catalog', searchtype), query))
    elif searchtype == 'suspended':
        # 'suspended' is not indexed
        cat = queryUtility(ICatalog, name='customers_catalog')
        all = cat.searchResults(customer_id=(None, None))
        for customer in all:
            if customer.suspended:
                hitlist.append(CustomerQueryResultItem(customer, view=view))
        return hitlist
    else:
        # Temporary solution to display all customers added
        if query == '*':
            cat = queryUtility(ICatalog, name='customers_catalog')
            results = cat.searchResults(customer_id=(None, None))
        else:
            results = Query().searchResults(
                Eq(('customers_catalog', searchtype), query))
    for result in results:
        hitlist.append(CustomerQueryResultItem(result, view=view))
    return hitlist


class SimpleFieldSearch(object):
    """A programmatic (no UI required) search.

    Looks up a given field attribute of the customers catalog for a
    single value. So normally you would call an instance of this
    search like this:

      >>> SimpleFieldSearch()(reg_number='somevalue')

    """
    catalog_name = 'customers_catalog'

    def __call__(self, **kw):
        """Search customers catalog programmatically.
        """
        if len(kw) != 1:
            raise ValueError('must give exactly one index name to search')
        cat = queryUtility(ICatalog, name=self.catalog_name)
        index_name, query_term = kw.items()[0]
        results = cat.searchResults(index_name=(query_term, query_term))
        return results

#: an instance of `SimpleFieldSearch` looking up customers catalog.
simple_search = SimpleFieldSearch()


class CustomersQuery(FilteredCatalogQueryBase):
    """Query customers in a site. See waeup.ikoba.catalog for more info.
    """
    cat_name = 'customers_catalog'
    defaults = dict(customer_id=None)  # make sure we get all studs by default


# Catalog for customer contracts

class ContractsCatalog(grok.Indexes):
    """A catalog for all contracts.
    """
    grok.site(ICompany)
    grok.name('contracts_catalog')
    grok.context(IContract)

    contract_id = grok.index.Field(attribute='contract_id')
    last_product_id = grok.index.Field(attribute='last_product_id')
    contract_category = grok.index.Field(attribute='contract_category')
