from ploneintranet.news.behaviors import IBannerBehavior
from ploneintranet.news.behaviors import INewsMeta
from ploneintranet.news.behaviors import ISplashPageBehavior
from ploneintranet.news.interfaces import IBannerWithHero
from ploneintranet.news.interfaces import IBannerWithIcon
from ploneintranet.news.interfaces import IMagazineHome
from ploneintranet.news.interfaces import ISplashPage
from ploneintranet.news.testing import IntegrationTestCase
from ploneintranet.workspace.basecontent.utils import dexterity_update
from zope.interface import alsoProvides
from zope.interface import noLongerProvides

PROJECTNAME = "ploneintranet.news"


class NewsBehaviorsTestCase(IntegrationTestCase):
    def test_banner(self):
        news = self.portal.news["press-mentions"]["fusce-fames"]
        banner = IBannerBehavior(news)

        # There is an equivalence between setting the banner attribute
        # and providing a marker interface in the context
        identifier = IBannerWithIcon.__identifier__
        banner.banner = identifier
        self.assertEqual(banner.banner, identifier)
        self.assertTrue(IBannerWithIcon.providedBy(news))
        banner.banner = ""
        self.assertEqual(banner.banner, "")
        self.assertFalse(IBannerWithIcon.providedBy(news))
        # testing the other way around
        alsoProvides(news, IBannerWithHero)
        self.assertEqual(banner.banner, IBannerWithHero.__identifier__)
        self.assertTrue(IBannerWithHero.providedBy(news))
        noLongerProvides(news, IBannerWithHero)
        self.assertEqual(banner.banner, "")
        self.assertFalse(IBannerWithHero.providedBy(news))

        # Setting a stupic value is equivalent to setting an empty string
        alsoProvides(news, IBannerWithIcon)
        banner.banner = "Fuzzy"
        self.assertEqual(banner.banner, "")
        self.assertFalse(IBannerWithIcon.providedBy(news))

    def test_is_splash_page(self):
        news = self.portal.news["press-mentions"]["fusce-fames"]
        splashpage = ISplashPageBehavior(news)

        # There is an equivalence between setting the is_splash_page attribute
        # and providing a marker interface in the context
        splashpage.is_splash_page = True
        self.assertEqual(splashpage.is_splash_page, True)
        self.assertTrue(ISplashPage.providedBy(news))
        splashpage.is_splash_page = False
        self.assertEqual(splashpage.is_splash_page, False)
        self.assertFalse(ISplashPage.providedBy(news))
        # testing the other way around
        alsoProvides(news, ISplashPage)
        self.assertEqual(splashpage.is_splash_page, True)
        self.assertTrue(ISplashPage.providedBy(news))
        noLongerProvides(news, ISplashPage)
        self.assertEqual(splashpage.is_splash_page, False)
        self.assertFalse(ISplashPage.providedBy(news))

        # Values will be always casted to booleans
        splashpage.is_splash_page = "Fuzzy"
        self.assertEqual(splashpage.is_splash_page, True)
        self.assertTrue(ISplashPage.providedBy(news))
        splashpage.is_splash_page = {}
        self.assertEqual(splashpage.is_splash_page, False)
        self.assertFalse(ISplashPage.providedBy(news))

    def test_dexterity_update(self):
        """Test z3c.form magic:
        In the forms there is no need to fiddle with the adapter
        """
        news = self.portal.news["press-mentions"]["fusce-fames"]
        banner = IBannerBehavior(news)
        splashpage = ISplashPageBehavior(news)
        self.assertEqual(banner.banner, "")
        self.assertFalse(IBannerWithIcon.providedBy(news))
        self.assertEqual(splashpage.is_splash_page, False)
        self.assertFalse(ISplashPage.providedBy(news))
        request = self.layer["request"].clone()
        request.form = {
            "banner": IBannerWithIcon.__identifier__,
            "is_splash_page": "selected",
            "is_splash_page-empty-marker": "1",
        }
        dexterity_update(news, request)
        self.assertEqual(banner.banner, IBannerWithIcon.__identifier__)
        self.assertTrue(IBannerWithIcon.providedBy(news))
        self.assertEqual(splashpage.is_splash_page, True)
        self.assertTrue(ISplashPage.providedBy(news))
        request = self.layer["request"].clone()
        request.form = {"banner": "", "is_splash_page-empty-marker": "1"}
        dexterity_update(news, request)
        self.assertEqual(banner.banner, "")
        self.assertFalse(IBannerWithIcon.providedBy(news))
        self.assertEqual(splashpage.is_splash_page, False)
        self.assertFalse(ISplashPage.providedBy(news))

    def test_news_app_layout(self):
        news_app = self.portal.news
        news_app.layout = "tc"
        self.assertEqual(news_app.getLayout(), "tc")

    def test_news_app_columns(self):
        news_app = self.portal.news
        news_app.column1 = ["1.1", "1.2"]
        news_app.column2 = ["2.1", "2.2"]
        news_app.column3 = ["3.1", "3.2"]

    def test_enable_disable_magazine_home(self):
        news = self.portal.news["press-mentions"]["fusce-fames"]
        behavior = INewsMeta(news)

        behavior.magazine_home = True

        self.assertEqual(behavior.magazine_home, True)
        self.assertEqual(IMagazineHome.providedBy(news), True)

        behavior.magazine_home = False

        self.assertEqual(behavior.magazine_home, False)
        self.assertEqual(IMagazineHome.providedBy(news), False)
