from rest_framework import serializers
from punkweb_boards.conf.settings import SHOUTBOX_DISABLED_TAGS
from punkweb_boards.models import (
    BoardProfile,
    Category,
    Subcategory,
    Thread,
    Post,
    Conversation,
    Message,
    Report,
    Shout,
)


class BoardProfileSerializer(serializers.ModelSerializer):
    post_count = serializers.ReadOnlyField()
    can_shout = serializers.ReadOnlyField()
    rendered_username = serializers.ReadOnlyField()
    rendered_rank = serializers.ReadOnlyField()

    class Meta:
        model = BoardProfile
        fields = "__all__"


class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        exclude = ("auth_req",)


class SubcategorySerializer(serializers.ModelSerializer):
    last_thread = serializers.ReadOnlyField(source="last_thread.id")
    last_thread_title = serializers.ReadOnlyField(source="last_thread.title")
    last_thread_created = serializers.ReadOnlyField(
        source="last_thread.created"
    )
    last_thread_user = serializers.ReadOnlyField(
        source="last_thread.user.profile.rendered_username"
    )
    parent_name = serializers.ReadOnlyField(source="parent.name")
    thread_count = serializers.ReadOnlyField()
    post_count = serializers.ReadOnlyField()
    can_post = serializers.SerializerMethodField()

    def get_can_post(self, obj):
        return obj.can_post(self.context.get("request").user)

    class Meta:
        model = Subcategory
        exclude = ("auth_req",)


class ThreadSerializer(serializers.ModelSerializer):
    last_post = serializers.ReadOnlyField(source="last_post.id")
    last_post_created = serializers.ReadOnlyField(source="last_post.created")
    last_post_username = serializers.ReadOnlyField(
        source="last_post.user.username"
    )
    last_post_rendered_username = serializers.ReadOnlyField(
        source="last_post.user.profile.rendered_username"
    )
    user_username = serializers.ReadOnlyField(source="user.username")
    user_rendered_username = serializers.ReadOnlyField(
        source="user.profile.rendered_username"
    )
    user_image = serializers.ReadOnlyField(source="user.profile.avatar")
    user_post_count = serializers.ReadOnlyField(
        source="user.profile.post_count"
    )
    user_join_date = serializers.ReadOnlyField(source="user.created")
    flagged = serializers.ReadOnlyField(source="reported")
    posts_count = serializers.ReadOnlyField()
    can_edit = serializers.SerializerMethodField()

    def get_can_edit(self, obj):
        return obj.can_edit(self.context.get("request").user)

    class Meta:
        model = Thread
        fields = "__all__"
        read_only_fields = (
            "pinned",
            "closed",
            "user",
            "upvoted_by",
            "downvoted_by",
        )


class PostSerializer(serializers.ModelSerializer):
    flagged = serializers.ReadOnlyField(source="reported")
    can_edit = serializers.SerializerMethodField()

    def get_can_edit(self, obj):
        return obj.can_edit(self.context.get("request").user)

    class Meta:
        model = Post
        fields = "__all__"
        read_only_fields = ("user", "upvoted_by", "downvoted_by")


class ConversationSerializer(serializers.ModelSerializer):
    last_message = serializers.ReadOnlyField(source="last_message.id")
    last_message_title = serializers.ReadOnlyField(source="last_message.title")
    last_message_created = serializers.ReadOnlyField(
        source="last_message.created"
    )
    last_message_user = serializers.ReadOnlyField(
        source="last_message.user.profile.rendered_username"
    )
    message_count = serializers.ReadOnlyField()

    class Meta:
        model = Conversation
        fields = "__all__"
        read_only_fields = ("unread_by",)


class MessageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Message
        fields = "__all__"
        read_only_fields = ("user",)


class ShoutSerializer(serializers.ModelSerializer):
    username = serializers.ReadOnlyField(source="user.username")
    rendered_username = serializers.ReadOnlyField(
        source="user.profile.rendered_username"
    )

    class Meta:
        model = Shout
        fields = (
            "id",
            "user",
            "username",
            "rendered_username",
            "content",
            "_content_rendered",
            "created",
            "modified",
        )
        read_only_fields = ("user",)

    def create(self, validated_data):
        for key in SHOUTBOX_DISABLED_TAGS:
            key_tag = "[{}]".format(key).lower()
            if (
                key_tag[: len(key_tag) - 1]
                in validated_data.get("content").lower()
            ):
                raise serializers.ValidationError(
                    {
                        "notAllowed": "{} is not allowed in the shoutbox".format(
                            key_tag
                        )
                    }
                )

        return Shout.objects.create(**validated_data)

[(17, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (18, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (19, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (20, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (34, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (35, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (36, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (39, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (42, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (43, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (44, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (45, 'rest_framework.serializers.SerializerMethodField', 'serializers.SerializerMethodField', False, 'from rest_framework import serializers\n'), (56, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (57, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (58, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (61, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (64, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (65, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (68, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (69, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (72, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (73, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (74, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (75, 'rest_framework.serializers.SerializerMethodField', 'serializers.SerializerMethodField', False, 'from rest_framework import serializers\n'), (93, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (94, 'rest_framework.serializers.SerializerMethodField', 'serializers.SerializerMethodField', False, 'from rest_framework import serializers\n'), (106, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (107, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (108, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (111, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (114, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (130, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (131, 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', False, 'from rest_framework import serializers\n'), (164, 'punkweb_boards.models.Shout.objects.create', 'Shout.objects.create', False, 'from punkweb_boards.models import BoardProfile, Category, Subcategory, Thread, Post, Conversation, Message, Report, Shout\n')]