from plone import api
from Products.CMFPlone.utils import get_installer
from zExceptions import BadRequest
from zope.globalrequest import getRequest

import logging

log = logging.getLogger(__name__)

# These add-ons will be uninstalled. They are either dependencies
# of ploneintranet.suite that are not from the ploneintranet-namespace
# or are dependencies of dependencies of ploneintranet.suite that make little
# or no sense out side its context
ADDITIONAL_DEPENDENCIES = [
    "collective.mustread",
    "collective.workspace",
    "slc.mailrouter",
]


def uninstall(context):
    """Run when installing the uninstall-profile."""
    portal = api.portal.get()
    uninstall_dependencies(portal)


def uninstall_dependencies(portal):
    # Uninstall dependencies of ploneintranet from the
    # ploneinstranet-namespace plus some additional add-ons.
    # We do not check if the product is installed since it may be
    # INonInstallable
    installer = get_installer(portal, getRequest())
    install_profile = installer.get_install_profile("ploneintranet.suite")
    dependencies = install_profile.get("dependencies", ())
    dependencies = list(dependencies)
    dependencies.extend(ADDITIONAL_DEPENDENCIES)
    for dependency in dependencies:
        dependency = str(dependency.split("profile-")[-1].split(":")[0])
        if (
            dependency.startswith("ploneintranet.")
            or dependency.startswith("quaive.")
            or dependency in ADDITIONAL_DEPENDENCIES
        ):
            try:
                installer.uninstall_product(dependency)
                log.info('Uninstalled dependency "%s"' % dependency)
            except (AttributeError, BadRequest):
                pass
