from collective.ftw.upgrade import UpgradeStep
from logging import getLogger
from plone import api

logger = getLogger(__name__)


class NewsTileDefaultCollapsed(UpgradeStep):
    """News tile default collapsed."""

    def __call__(self):
        mapping = api.portal.get_registry_record(
            "ploneintranet.layout.dashboard_tile_class_mapping"
        )
        try:
            news_tile_classes = mapping["news.tile"]
        except KeyError:
            logger.warning("No class mapping for news.tile, bailing out")
            return
        if "state" in news_tile_classes:
            logger.info("news.tile already is configured as: %s", news_tile_classes)
            return
        mapping["news.tile"] += " state-collapsed"
        api.portal.set_registry_record(
            "ploneintranet.layout.dashboard_tile_class_mapping", mapping
        )
        logger.info("Configured news.tile to default to state-collapsed.")
