from django.conf import settings
from django.utils.translation import ugettext as _
from rest_framework import serializers
from rest_framework.utils import model_meta

from .models import Comment


class CommentSerializer(serializers.ModelSerializer):
    user_name = serializers.SerializerMethodField()
    is_deleted = serializers.SerializerMethodField()
    ratings = serializers.SerializerMethodField()
    is_moderator = serializers.SerializerMethodField()

    class Meta:
        model = Comment
        read_only_fields = ('modified', 'created', 'id',
                            'user_name', 'ratings', 'content_type',
                            'object_pk', 'last_discussed',
                            'is_moderator_marked')
        exclude = ('creator', 'is_censored', 'is_removed')

    def to_representation(self, instance):
        """
        Gets the categories and adds them along with their values
        to a dictionary
        """
        ret = super().to_representation(instance)
        categories = {}
        if ret['comment_categories']:
            category_choices = getattr(settings,
                                       'A4_COMMENT_CATEGORIES', '')
            if category_choices:
                category_choices = dict((x, str(y)) for x, y
                                        in category_choices)
            category_list = ret['comment_categories'].strip('[]').split(',')
            for category in category_list:
                if category in category_choices:
                    categories[category] = category_choices[category]
                else:
                    categories[category] = category
        ret['comment_categories'] = categories
        return ret

    def to_internal_value(self, data):
        data = super().to_internal_value(data)
        if 'comment_categories' in data:
            value = data.get('comment_categories')
            if value == '' or value == '[]':
                raise serializers.ValidationError({
                    'comment_categories': _('Please choose a category')
                })
        return data

    def get_user_name(self, obj):
        """
        Don't show username if comment is marked removed or censored
        """
        if(obj.is_censored or obj.is_removed):
            return _('unknown user')
        return str(obj.creator.username)

    def get_is_moderator(self, obj):
        return obj.project.has_moderator(obj.creator)

    def get_is_deleted(self, obj):
        """
        Returns true is one of the flags is set
        """
        return (obj.is_censored or obj.is_removed)

    def get_ratings(self, comment):
        """
        Gets positve and negative rating count as well as
        info on the request users rating
        """
        user = self.context['request'].user
        positive_ratings = comment.ratings.filter(value=1).count()
        negative_ratings = comment.ratings.filter(value=-1).count()

        if user.is_authenticated:
            user_rating = comment.ratings.filter(creator=user).first()
        else:
            user_rating = None

        if user_rating:
            user_rating_value = user_rating.value
            user_rating_id = user_rating.pk
        else:
            user_rating_value = None
            user_rating_id = None

        result = {
            'positive_ratings': positive_ratings,
            'negative_ratings': negative_ratings,
            'current_user_rating_value': user_rating_value,
            'current_user_rating_id': user_rating_id
        }

        return result


class ThreadSerializer(CommentSerializer):
    """
    Serializes a comment including child comment (replies).
    """
    child_comments = CommentSerializer(many=True, read_only=True)


class CommentModerateSerializer(serializers.ModelSerializer):

    def update(self, instance, validated_data):
        serializers.raise_errors_on_nested_writes('update', self,
                                                  validated_data)
        info = model_meta.get_field_info(instance)

        # Simply set each attribute on the instance, and then save it.
        # Note that unlike `.create()` we don't need to treat many-to-many
        # relationships as being a special case. During updates we already
        # have an instance pk for the relationships to be associated with.
        for attr, value in validated_data.items():
            if attr in info.relations and info.relations[attr].to_many:
                field = getattr(instance, attr)
                field.set(value)
            else:
                setattr(instance, attr, value)
        instance.save(ignore_modified=True)

        return instance

    class Meta:
        model = Comment
        fields = ('is_moderator_marked', 'modified', 'created', 'id',
                  'content_type', 'object_pk', 'last_discussed', 'comment',
                  'comment_categories')
        read_only_fields = ('modified', 'created', 'id', 'content_type',
                            'object_pk', 'last_discussed', 'comment',
                            'comment_categories')

@@ -3814,16 +3814,1371 @@
 izer):%0A%0A
+    def to_representation(self, instance):%0A        %22%22%22%0A        Create a dictionary form categories.%0A%0A        Gets the categories and adds them along with their values%0A        to a dictionary.%0A        %22%22%22%0A        ret = super().to_representation(instance)%0A        categories = %7B%7D%0A        if ret%5B'comment_categories'%5D:%0A            category_choices = getattr(settings,%0A                                       'A4_COMMENT_CATEGORIES', '')%0A            if category_choices:%0A                category_choices = dict((x, str(y)) for x, y%0A                                        in category_choices)%0A            category_list = ret%5B'comment_categories'%5D.strip('%5B%5D').split(',')%0A            for category in category_list:%0A                if category in category_choices:%0A                    categories%5Bcategory%5D = category_choices%5Bcategory%5D%0A                else:%0A                    categories%5Bcategory%5D = category%0A        ret%5B'comment_categories'%5D = categories%0A        return ret%0A%0A    def to_internal_value(self, data):%0A        data = super().to_internal_value(data)%0A        if 'comment_categories' in data:%0A            value = data.get('comment_categories')%0A            if value == '' or value == '%5B%5D':%0A                raise serializers.ValidationError(%7B%0A                    'comment_categories': _('Please choose a category')%0A                %7D)%0A        return data%0A%0A
     def 

Return whole categories in serializwe