from ..testing import LINEAGE_CONTROLPANELS_INTEGRATION_TESTING
from collective.lineage.utils import enable_childsite
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from plone.registry.interfaces import IRegistry
from Products.CMFPlone.interfaces import ISiteSchema
from zope.component import getUtility

import plone.api.content
import unittest

# Red pixel with filename pixel.png
LOGO_1_BASE64 = b"filenameb64:cGl4ZWwucG5n;datab64:iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVQI12P4z8AAAAMBAQAY3Y2wAAAAAElFTkSuQmCC"  # noqa

# Green pixel with filename pixelgreen.png
LOGO_2_BASE64 = b"filenameb64:cGl4ZWxncmVlbi5wbmc=;datab64:iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVQI12Ng+M8AAAICAQCqKp4nAAAAAElFTkSuQmCC"  # noqa


class SiteControlPanelFunctionalTest(unittest.TestCase):
    """Test that changes in the site control panel are actually
    stored in the registry.
    """

    layer = LINEAGE_CONTROLPANELS_INTEGRATION_TESTING

    def setUp(self):
        self.app = self.layer["app"]
        self.portal = self.layer["portal"]
        self.request = self.layer["request"]
        self.portal_url = self.portal.absolute_url()

        setRoles(self.portal, TEST_USER_ID, ["Manager"])

        plone.api.content.create(
            container=self.portal, type="Folder", id="subsite", title="subsite"
        )
        self.subsite = self.portal["subsite"]
        plone.api.content.transition(obj=self.subsite, transition="publish")
        enable_childsite(self.subsite)

    def _registry_edit(self, context, schema, data):
        """Edit data against the registry schema on a given context."""
        registry = getUtility(IRegistry, context=context)
        settings = registry.forInterface(schema, prefix="plone")
        for item in data:
            setattr(settings, item["name"], item["value"])

    def _registry_assert(self, context, schema, data):
        """Assert data against the registry schema on a given context."""
        registry = getUtility(IRegistry, context=context)
        settings = registry.forInterface(schema, prefix="plone")

        for item in data:
            self.assertEqual(getattr(settings, item["name"]), item["value"])

    def test_lineage_controlpanel_site_roundtrip(self):
        """Test editing the main site controlpanel and the lineage controlpanel
        in a subsite and check, if settings don't overwrite each other.
        """
        data_portal = [
            {"name": "site_title", "value": "Main portal"},
            {"name": "site_logo", "value": LOGO_1_BASE64},
            {"name": "exposeDCMetaTags", "value": True},
            {"name": "enable_sitemap", "value": True},
            {"name": "webstats_js", "value": "// dummy js"},
            {"name": "display_publication_date_in_byline", "value": True},
            {"name": "icon_visibility", "value": "enabled"},
            {"name": "thumb_visibility", "value": "enabled"},
            {"name": "toolbar_position", "value": "side"},
            {"name": "toolbar_logo", "value": "some_logo, nonexistent"},
            {"name": "robots_txt", "value": "please, no robots"},
            {"name": "default_page", "value": ["start.html"]},
            {"name": "roles_allowed_to_add_keywords", "value": ["Manager"]},
        ]

        data_subsite = [
            {"name": "site_title", "value": "Sub portal"},
            {"name": "site_logo", "value": LOGO_2_BASE64},
            {"name": "exposeDCMetaTags", "value": True},
            {"name": "enable_sitemap", "value": True},
            {"name": "webstats_js", "value": None},
            {"name": "display_publication_date_in_byline", "value": True},
            {"name": "icon_visibility", "value": "false"},
            {"name": "thumb_visibility", "value": "false"},
            {"name": "toolbar_position", "value": "side"},
            {"name": "toolbar_logo", "value": "another nonexistent logo"},
            {"name": "robots_txt", "value": "more robots!"},
            {"name": "default_page", "value": ["start.html"]},
            {"name": "roles_allowed_to_add_keywords", "value": ["Manager"]},
        ]

        # Populate main site
        self._registry_edit(self.portal, ISiteSchema, data_portal)

        # Populate child site
        self._registry_edit(self.subsite, ISiteSchema, data_subsite)

        # Assert main portal
        self._registry_assert(self.portal, ISiteSchema, data_portal)

        # Assert sub portal
        self._registry_assert(self.subsite, ISiteSchema, data_subsite)
