"""This module holds the api-related views of the ``translation`` app"""

import json

from django.http import HttpResponse
from django.views.generic.base import View
from django.views.i18n import get_javascript_catalog


class TranslationCatalogForIOSClient(View):

    def get(self, *args, **kwargs):  # pylint: disable=unused-argument
        locale = self.kwargs['language_slug']
        catalog, __ = get_javascript_catalog(locale, 'client', '')

        data = {}
        for key, value in catalog.items():
            key = key.split('\x04', 1)[1]
            data[key] = value

        return self.render_to_response(data)

    def render_to_response(self, context):
        """Returns a JSON response containing 'context' as payload

        For the parameters, see ``django.views.generic.View``"""
        return self.get_json_response(self.convert_context_to_json(context))

    @staticmethod
    def get_json_response(content, **httpresponse_kwargs):
        """Construct an `HttpResponse` object."""
        return HttpResponse(content,
                            content_type='application/json',
                            **httpresponse_kwargs)

    @staticmethod
    def convert_context_to_json(context):
        """Convert the context dictionary into a JSON object"""
        return json.dumps(context)
