from zope.app.component.hooks import setSite
from zope.app import zapi
from zope.component import getUtility
from zope.component import queryUtility

from Products.Five.site.localsite import enableLocalSiteHook

from Acquisition import aq_parent
from OFS.Application import Application

from Products.listen.interfaces import IListLookup
from Products.listen.interfaces import IMemberLookup
from Products.listen.interfaces import IGetMailHost
from Products.listen.utilities.list_lookup import ListLookup
from Products.listen.utilities.token_to_email import MemberToEmail
from Products.listen.utilities.get_mail_host import GetMailHost

def importVarious(context):
    """
    Import various settings.
    
    Provisional handler that does initialization that is not yet taken
    care of by exising generic setup handlers.
    """

    # check to see if this step should run
    if context.readDataFile('listen-various.txt') is None:
        return

    site = context.getSite()
    logger = context.getLogger("listen")

    install_token_email_utility(site)
    logger.info('IMemberLookup Utility registered at %s' % site)

    install_mailhost_utility(site)
    logger.info('IGetMailHost Utility registered at %s' % site)

    # Install the list lookup utility at the zope application root,
    # it seems a bit sketchy to be doing things at the zope application level
    # but this registry is really something that should be shared across
    # instances. We register it as a local, rather than global, utility because
    # it needs to have some persistent state.
    app = site
    # find the zope application root
    while app is not None and not isinstance(app, Application):
        app = aq_parent(app)

    # If we have the zope Application and the utility is not yet registered,
    # then register it.
    if (app is not None and zapi.queryUtility(IListLookup, default=None) is None):
        enableLocalSiteHook(app)
        setSite(app)
        sm = app.getSiteManager()
        if not sm.queryUtility(IListLookup):
            sm.registerUtility(IListLookup, ListLookup('list_lookup'))
            logger.info('IListLookup Utility registered at %s' % app)
        else:
            logger.info('IListLookup Utility already registered at %s' % app)
    else:
        logger.info('Nothing to import.')


def install_token_email_utility(site):
    """
    import the local utility responsible for converting arbitrary
    subscription tokens, such as member ids, to email addresses
    """
    enableLocalSiteHook(site)
    setSite(site)
    if queryUtility(IMemberLookup) is not None: return
    sm = site.getSiteManager()
    member_to_email_utility = MemberToEmail(site)
    sm.registerUtility(IMemberLookup, member_to_email_utility)


def install_mailhost_utility(site):
    """
    import the local utility 
    """
    if queryUtility(IGetMailHost) is not None: return
    sm = site.getSiteManager()
    get_mail_host_utility = GetMailHost(site)
    sm.registerUtility(IGetMailHost, get_mail_host_utility)
