# -*- coding: utf-8 -*-
from io import StringIO

from django.apps import apps
from django.core.management import call_command
from django.test import TestCase

from ocyan.plugin.google.runtime import get_config


def get_failed_reason(config_name, config_option):
    return (
        "The '%s' config option has to be set in the 'google' config option and not '%s'. Move it and re-run tests."
        % (config_option, config_name)
    )


class GooglePluginTestCase(TestCase):
    def test_app_is_installed(self):
        self.assertTrue(apps.is_installed("ocyan.plugin.google"))

    def test_django_checks_pass(self):
        call_command("check", stdout=StringIO())

    def test_settings_in_old_plugins(self):
        """
        The following configuration options first existed both in the google_shopping and google_rich_snippets:
            - gtin_attribute
            - mpn_attribute
            - brand_attribute

        The following configurtion options first existed in contact_form:
            - recaptcha_public_key
            - recaptcha_private_key
            - recaptcha_version

        Those have been moved to this plugin, but we have to check whether a project doesn't have the settings set
        under the wrong config option as it will not work and we can notify developer by letting tests fail.
        """

        # google_shopping
        gtin_attribute = get_config("google_shopping", "gtin_attribute", False)
        self.assertFalse(
            gtin_attribute, get_failed_reason("google_shopping", "gtin_attribute")
        )
        mpn_attribute = get_config("google_shopping", "mpn_attribute", False)
        self.assertFalse(
            mpn_attribute, get_failed_reason("google_shopping", "mpn_attribute")
        )
        brand_attribute = get_config("google_shopping", "brand_attribute", False)
        self.assertFalse(
            brand_attribute, get_failed_reason("google_shopping", "brand_attribute")
        )
        brand_attribute = get_config("google_shopping", "product_type_attribute", False)
        self.assertFalse(
            brand_attribute,
            get_failed_reason("google_shopping", "product_type_attribute"),
        )

        # google_rich_snippets
        gtin_attribute = get_config("google_rich_snippets", "gtin_attribute", False)
        self.assertFalse(
            gtin_attribute, get_failed_reason("google_rich_snippets", "gtin_attribute")
        )
        mpn_attribute = get_config("google_rich_snippets", "mpn_attribute", False)
        self.assertFalse(
            mpn_attribute, get_failed_reason("google_rich_snippets", "mpn_attribute")
        )
        brand_attribute = get_config("google_rich_snippets", "brand_attribute", False)
        self.assertFalse(
            brand_attribute,
            get_failed_reason("google_rich_snippets", "brand_attribute"),
        )

        # contact_form captcha
        recaptcha_public_key = get_config("contact_form", "recaptcha_public_key", False)
        self.assertFalse(
            recaptcha_public_key,
            get_failed_reason("contact_form", "recaptcha_public_key"),
        )
        recaptcha_private_key = get_config(
            "contact_form", "recaptcha_private_key", False
        )
        self.assertFalse(
            recaptcha_private_key,
            get_failed_reason("contact_form", "recaptcha_private_key"),
        )
        recaptcha_version = get_config("contact_form", "recaptcha_version", False)
        self.assertFalse(
            recaptcha_version, get_failed_reason("contact_form", "recaptcha_version")
        )
