"""This module holds model tests for ``themes`` app."""

from django.conf import settings

from mtp.apps.common.tests.test_cases import MTPTestCase

from .. import constants
from ..factories import ThemeFactory
from ..models import Theme


class ThemeModelTestCase(MTPTestCase):
    """Test case that tests the ``Theme`` model."""

    def setUp(self):
        super(ThemeModelTestCase, self).setUp()

        self.theme = ThemeFactory()

    def test_requires_images_creation(self):
        """Test that requires images and their hash are created."""

        # Test image creation
        self.assertEqual(self.theme.background_image.name, settings.THEME_BACKGROUND_IMAGE)
        self.assertEqual(self.theme.background_image_diffusion.name,
                         settings.THEME_BACKGROUND_IMAGE_DIFFUSION)
        self.assertEqual(self.theme.header_image.name, settings.THEME_HEADER_IMAGE)
        self.assertEqual(self.theme.home_diffusion_image.name, settings.THEME_HOME_DIFFUSION_IMAGE)
        self.assertEqual(self.theme.reveal_logo.name, constants.DEFAULT_REVEAL_LOGO_PATH)

        # Test hash creation, if url image is not defined, hash image will be not computed so
        # don't need to test it.
        if settings.THEME_BACKGROUND_IMAGE:
            self.assertNotEqual(self.theme.background_image_hash, "")
        if settings.THEME_BACKGROUND_IMAGE_DIFFUSION:
            self.assertNotEqual(self.theme.background_image_diffusion_hash, "")
        if settings.THEME_HEADER_IMAGE:
            self.assertNotEqual(self.theme.header_image_hash, "")
        if settings.THEME_HOME_DIFFUSION_IMAGE:
            self.assertNotEqual(self.theme.home_diffusion_image_hash, "")
        if constants.DEFAULT_REVEAL_LOGO_PATH:
            self.assertNotEqual(self.theme.reveal_logo_hash, "")

    def test_get_default(self):
        """Test that the ``get_default`` method always return a test if one exists"""

        # no theme => no theme (pas d'palais, pas d'palais)
        Theme.objects.all().delete()
        self.assertIsNone(Theme.objects.get_default())

        # create a default one => should be the default
        theme1 = ThemeFactory(default=True)
        self.assertEqual(Theme.objects.get_default(), theme1)

        # unset default => still the only one, should be the default
        theme1.default = False
        theme1.save()
        self.assertFalse(theme1.default)
        self.assertEqual(Theme.objects.get_default(), theme1)

        # add another one, default => should be the new default
        theme2 = ThemeFactory(default=True)
        self.assertEqual(Theme.objects.get_default(), theme2)

        # add yet another default => should be the new default
        theme3 = ThemeFactory(default=True)
        theme2.refresh_from_db()
        self.assertFalse(theme2.default)  # save of theme3 changed this
        self.assertEqual(Theme.objects.get_default(), theme3)

        # force to have two default => first default one
        # (using ``update`` because ``save`` avoid this)
        Theme.objects.filter(pk=theme2.pk).update(default=True)
        self.assertEqual(Theme.objects.get_default(), theme2)

        # many themes but no default => first created one
        Theme.objects.all().update(default=False)
        self.assertEqual(Theme.objects.get_default(), theme1)

        # soft-delete them => not filtered => first created one
        for theme in (theme1, theme2, theme3):
            theme.refresh_from_db()
            theme.remove()
        self.assertEqual(Theme.objects.get_default(), theme1)
