# pylint: disable=W0631
import doctest
from unittest.mock import patch

from django.test import TestCase

import ocyan.core.fender
import ocyan.plugin.cookie_jar.context


def load_tests(loader, tests, ignore):  # pylint: disable=W0613
    tests.addTests(doctest.DocTestSuite(ocyan.plugin.cookie_jar.context))
    return tests


TEMPLATES_NAMES = [
    "template1",
    "template2",
    "template3",
    "template4",
    "template5",
    "template6",
    "template7",
    "template8",
]

COOKIE_JAR_HIDDEN = lambda template_name: {
    "theme": {"name": template_name, "category_navigation_depth": 1, "menu_depth": 2},
    "cookie_jar": {
        "analytical": True,
        "functional": True,
        "google_analytics": "UA-99045405-3",
        "marketing": False,
        "social": False,
    },
}

COOKIE_JAR_SOCIAL = lambda template_name: {
    "theme": {"name": template_name, "category_navigation_depth": 1, "menu_depth": 2},
    "cookie_jar": {
        "analytical": True,
        "functional": True,
        "google_analytics": "UA-99045405-3",
        "marketing": False,
        "social": True,
    },
}

COOKIE_JAR_MARKETING = lambda template_name: {
    "theme": {"name": template_name, "category_navigation_depth": 1, "menu_depth": 2},
    "cookie_jar": {
        "analytical": True,
        "functional": True,
        "google_analytics": "UA-99045405-3",
        "marketing": True,
        "social": True,
    },
}


class CookieContextTest(TestCase):
    def test_no_annoying_cookie_bar(self):
        for index, template_name in enumerate(TEMPLATES_NAMES):
            with patch.object(
                ocyan.core.fender.config, "settings", COOKIE_JAR_HIDDEN(template_name)
            ):
                response = self.client.get("/")
                self.assertContains(response, "UA-99045405-3")
                self.assertNotContains(
                    response, 'id="cookie_popup_acceptButton" data-cookie-key'
                )
                self.assertNotContains(
                    response,
                    '<p class="h2 modal-title modal_cookie_header" id="CookiesOptions">',
                )

        self.assertEqual(index, 7)

    def test_annoying_social_cookie_bar(self):
        for index, template_name in enumerate(TEMPLATES_NAMES):
            with patch.object(
                ocyan.core.fender.config, "settings", COOKIE_JAR_SOCIAL(template_name)
            ):
                response = self.client.get("/")
                self.assertContains(response, "UA-99045405-3")
                self.assertContains(
                    response, 'id="cookie_popup_acceptButton" data-cookie-key'
                )
                self.assertContains(
                    response,
                    '<p class="h2 modal-title modal_cookie_header" id="CookiesOptions">',
                )
                self.assertNotContains(
                    response, '<p class="h5 text-primary">Marketing cookies</p>'
                )
                self.assertContains(
                    response, '<p class="h5 text-primary">Social media cookies</p>'
                )

        self.assertEqual(index, 7)

    @patch.object(ocyan.core.fender.config, "settings", COOKIE_JAR_MARKETING)
    def test_annoying_marketing_cookie_bar(self):
        for index, template_name in enumerate(TEMPLATES_NAMES):
            with patch.object(
                ocyan.core.fender.config,
                "settings",
                COOKIE_JAR_MARKETING(template_name),
            ):
                response = self.client.get("/")
                self.assertContains(response, "UA-99045405-3")
                self.assertContains(
                    response, 'id="cookie_popup_acceptButton" data-cookie-key'
                )
                self.assertContains(
                    response,
                    '<p class="h2 modal-title modal_cookie_header" id="CookiesOptions">',
                )
                self.assertContains(
                    response, '<p class="h5 text-primary">Social media cookies</p>'
                )
                self.assertContains(
                    response, '<p class="h5 text-primary">Marketing cookies</p>'
                )

        self.assertEqual(index, 7)
