from rest_framework import serializers

from common.common import getLanguage
from mama_care_api.settings import languageKey
from .interactive_content_models import (
    InteractiveContent,
    InteractiveContentSocialMedia,
    InteractiveContentSocialMediaLanguage,
    InteractiveContentSocialMediaOptions,
    InteractiveContentSocialMediaOptionsLanguage,
    InteractiveContentSocialMediaHashtags,
    InteractiveContentSocialMediaHashtagsLanguage,
    InteractiveContentAppContent,
    InteractiveContentAppContentLanguage,
    InteractiveContentAppContentOptions,
    InteractiveContentAppContentOptionsLanguage,
)


class InteractiveContentSerializer(serializers.ModelSerializer):
    socialMediaList = serializers.SerializerMethodField()
    appContentList = serializers.SerializerMethodField()

    class Meta:
        model = InteractiveContent
        fields = [
            "id",
            "contentInformationId",
            "socialMediaList",
            "appContentList",
        ]

    def get_socialMediaList(self, instance):
        languageId = getLanguage(self)
        dataList = InteractiveContentSocialMedia.objects.filter(
            interactiveContentId=instance.id
        )
        return InteractiveContentSocialMediaSerializer(
            dataList, context={languageKey: languageId}, many=True
        ).data

    def get_appContentList(self, instance):
        languageId = getLanguage(self)
        dataList = InteractiveContentAppContent.objects.filter(
            interactiveContentId=instance.id
        )
        return InteractiveContentAppContentSerializer(
            dataList, context={languageKey: languageId}, many=True
        ).data


class InteractiveContentSocialMediaSerializer(serializers.ModelSerializer):
    title = serializers.SerializerMethodField()
    question = serializers.SerializerMethodField()
    caption = serializers.SerializerMethodField()
    cta = serializers.SerializerMethodField()
    optionList = serializers.SerializerMethodField()
    hashtagList = serializers.SerializerMethodField()

    class Meta:
        model = InteractiveContentSocialMedia
        fields = [
            "id",
            "interactionType",
            "interactiveContentId",
            "title",
            "question",
            "caption",
            "cta",
            "optionList",
            "hashtagList",
        ]

    def get_title(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = InteractiveContentSocialMediaLanguage.objects.filter(
                interactiveContentSocialMediaId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.title

    def get_question(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = InteractiveContentSocialMediaLanguage.objects.filter(
                interactiveContentSocialMediaId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.question

    def get_caption(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = InteractiveContentSocialMediaLanguage.objects.filter(
                interactiveContentSocialMediaId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.caption

    def get_cta(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = InteractiveContentSocialMediaLanguage.objects.filter(
                interactiveContentSocialMediaId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.cta

    def get_optionList(self, instance):
        languageId = getLanguage(self)
        dataList = InteractiveContentSocialMediaOptions.objects.filter(
            interactiveContentSocialMediaId=instance.id
        )
        return InteractiveContentSocialMediaOptionsSerializer(
            dataList, context={languageKey: languageId}, many=True
        ).data

    def get_hashtagList(self, instance):
        languageId = getLanguage(self)
        dataList = InteractiveContentSocialMediaHashtags.objects.filter(
            interactiveContentSocialMediaId=instance.id
        )
        return InteractiveContentSocialMediaHashtagsSerializer(
            dataList, context={languageKey: languageId}, many=True
        ).data


class InteractiveContentSocialMediaLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = InteractiveContentSocialMediaLanguage
        fields = [
            "id",
            "title",
            "question",
            "caption",
            "cta",
            "interactiveContentSocialMediaId",
            languageKey,
        ]


class InteractiveContentSocialMediaOptionsSerializer(serializers.ModelSerializer):
    text = serializers.SerializerMethodField()

    class Meta:
        model = InteractiveContentSocialMediaOptions
        fields = [
            "id",
            "keyName",
            "interactiveContentSocialMediaId",
            "text",
        ]

    def get_text(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = InteractiveContentSocialMediaOptionsLanguage.objects.filter(
                interactiveContentSocialMediaOptionsId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.text


class InteractiveContentSocialMediaOptionsLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = InteractiveContentSocialMediaOptionsLanguage
        fields = [
            "id",
            "text",
            "interactiveContentSocialMediaOptionsId",
            languageKey,
        ]


class InteractiveContentSocialMediaHashtagsSerializer(serializers.ModelSerializer):
    text = serializers.SerializerMethodField()

    class Meta:
        model = InteractiveContentSocialMediaHashtags
        fields = [
            "id",
            "keyName",
            "interactiveContentSocialMediaId",
            "text",
        ]

    def get_text(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = InteractiveContentSocialMediaHashtagsLanguage.objects.filter(
                interactiveContentSocialMediaHashtagsId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.text


class InteractiveContentSocialMediaHashtagsLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = InteractiveContentSocialMediaHashtagsLanguage
        fields = [
            "id",
            "text",
            "interactiveContentSocialMediaHashtagsId",
            languageKey,
        ]


class InteractiveContentAppContentSerializer(serializers.ModelSerializer):
    title = serializers.SerializerMethodField()
    question = serializers.SerializerMethodField()
    explanation = serializers.SerializerMethodField()
    optionList = serializers.SerializerMethodField()

    class Meta:
        model = InteractiveContentAppContent
        fields = [
            "id",
            "correctAnswer",
            "interactiveContentId",
            "title",
            "question",
            "explanation",
            "optionList",
        ]

    def get_title(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = InteractiveContentAppContentLanguage.objects.filter(
                interactiveContentAppContentId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.title

    def get_question(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = InteractiveContentAppContentLanguage.objects.filter(
                interactiveContentAppContentId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.question

    def get_explanation(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = InteractiveContentAppContentLanguage.objects.filter(
                interactiveContentAppContentId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.explanation

    def get_optionList(self, instance):
        languageId = getLanguage(self)
        dataList = InteractiveContentAppContentOptions.objects.filter(
            interactiveContentAppContentId=instance.id
        )
        return InteractiveContentAppContentOptionsSerializer(
            dataList, context={languageKey: languageId}, many=True
        ).data


class InteractiveContentAppContentLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = InteractiveContentAppContentLanguage
        fields = [
            "id",
            "title",
            "question",
            "explanation",
            "interactiveContentAppContentId",
            languageKey,
        ]


class InteractiveContentAppContentOptionsSerializer(serializers.ModelSerializer):
    text = serializers.SerializerMethodField()

    class Meta:
        model = InteractiveContentAppContentOptions
        fields = [
            "id",
            "keyName",
            "interactiveContentAppContentId",
            "text",
        ]

    def get_text(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = InteractiveContentAppContentOptionsLanguage.objects.filter(
                interactiveContentAppContentOptionsId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.text


class InteractiveContentAppContentOptionsLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = InteractiveContentAppContentOptionsLanguage
        fields = [
            "id",
            "text",
            "interactiveContentAppContentOptionsId",
            languageKey,
        ]
