"""This module contains the serializers for the ``reveal`` app"""

import copy

from django.core.urlresolvers import reverse

from easy_thumbnails.files import get_thumbnailer
from rest_framework.serializers import Serializer

from .constants import REVEAL_CONFIGURATION_OPTIONS


class RevealBaseConfigSerializer(Serializer):
    """Serializer for the configuration options of the "reveal" module"""

    def to_representation(self, instance):
        """Render the configuration options of the reveal

        The configuration options are mostly constants, except for thoses related to the
        provided ``session`` parameter.

        Parameters
        ----------
        For the parameters, see ``rest_framework.serializers.Serializer.to_representation``

        """

        config_options = copy.deepcopy(REVEAL_CONFIGURATION_OPTIONS)

        # Reveal logo (the instance default theme reveal image)
        logo = instance.training.get_theme().reveal_logo
        logo_thumbnail = get_thumbnailer(logo)['reveal'].url
        config_options['datas']['assets']['images']['final_logo_converge'] = logo_thumbnail
        config_options['datas']['assets']['images']['final_logo'] = logo.url

        # Reveal images (users's avatars)
        config_options['datas']['images']['url'] = reverse(
            'apiv2:reveal-photos',
            kwargs={'session_uuid': instance.pk}
        )

        return config_options


class RevealPhotosSerializer(Serializer):
    """Serializer for the "photos" of the reveal"""

    def to_representation(self, instance):
        """Render the configuration for user image

        Parameters
        ----------
        For the parameters, see ``rest_framework.serializers.Serializer.to_representation``

        """

        return {
            'url': get_thumbnailer(instance.photo.img)['reveal_large'].url,
            'url_128x128': get_thumbnailer(instance.photo.img)['reveal_small'].url,
            'url_text': '',
        }
