"""This module holds the serializers of the ``themes`` app"""

from rest_framework import serializers

from ..models import Theme


class ThemeSerializer(serializers.ModelSerializer):
    """Serializer for ``themes.Theme`` objects.

    This is a pretty straightforward usage of DRF, nothing fancy here.
    The only subtlety is the fact that ImageFields must be serialized as dicts
    containing ``url``, ``hash`` and ``name`` keys. This is done in the
    ``_get_serialized_image`` method, which gets called when serializing said
    fields by way of ``SerializerMethodField``s.

    """

    background_image = serializers.SerializerMethodField()
    background_image_diffusion = serializers.SerializerMethodField()
    header_image = serializers.SerializerMethodField()
    home_diffusion_image = serializers.SerializerMethodField()

    class Meta:
        model = Theme
        fields = (
            'id', 'name', 'default',
            'color_first', 'color_second', 'color_item', 'color_selected_item',
            'color_text_item', 'color_text_selected_item', 'color_text_quizz',
            'background_color', 'background_color_diffusion',
            'main', 'main_lighter', 'main_darker',
            'complementary', 'clear', 'background',
            'gray', 'gray_lighter', 'gray_darker',
            'text_on_main', 'text_on_main_lighter', 'text_on_main_darker',
            'text_on_complementary', 'text_on_clear',
            'accent_blue', 'accent_green', 'accent_yellow',
            'accent_pink', 'accent_red', 'accent_white',
            'font_name', 'font_bold_name', 'font_thin_name',

            'background_image',
            'background_image_diffusion',
            'header_image',
            'home_diffusion_image',
        )

    def _get_serialized_image(self, obj, field_name):
        """Return info about the image identified by its name

        Parameters
        ----------
        obj: Theme
            The theme from which to get the image
        field_name: str
            The name of the field representing the wanted image

        Returns
        -------
        dict
            A dict with three values representing the image:
            - url: str
                The url to get the image from the outside
            - hash: str
                The actual md5 hash of the image
            - name: str
                The name of the image, composed with the hash and
                the original extension of the image name

        """

        image = getattr(obj, field_name)

        try:
            image_url = image.url
        except ValueError:
            return None

        image_hash = getattr(obj, '%s_hash' % field_name)
        image_extension = image.name.rsplit('.', 1)[1]
        image_name = '%s.%s' % (image_hash, image_extension)

        return {
            'url': image_url,
            'hash': image_hash,
            'name': image_name,
        }

    def get_background_image(self, obj):
        return self._get_serialized_image(obj, 'background_image')

    def get_background_image_diffusion(self, obj):
        return self._get_serialized_image(obj, 'background_image_diffusion')

    def get_header_image(self, obj):
        return self._get_serialized_image(obj, 'header_image')

    def get_home_diffusion_image(self, obj):
        return self._get_serialized_image(obj, 'home_diffusion_image')
