from rest_framework import serializers

from common.common import getLanguage
from mama_care_api.settings import languageKey
from .education_content_models import (
    EducationContent,
    EducationContentSocialMedia,
    EducationContentSocialMediaLanguage,
    EducationContentSocialMediaImageText,
    EducationContentSocialMediaImageTextLanguage,
    EducationContentSocialMediaHashtags,
    EducationContentSocialMediaHashtagsLanguage,
    EducationContentAppContent,
    EducationContentAppContentLanguage,
    EducationContentAppContentWhatYouShouldKnow,
    EducationContentAppContentWhatYouShouldKnowLanguage,
)


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

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

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

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


class EducationContentSocialMediaSerializer(serializers.ModelSerializer):
    postTitle = serializers.SerializerMethodField()
    caption = serializers.SerializerMethodField()
    cta = serializers.SerializerMethodField()
    imageTextList = serializers.SerializerMethodField()
    hashtagList = serializers.SerializerMethodField()

    class Meta:
        model = EducationContentSocialMedia
        fields = [
            "id",
            "educationContentId",
            "postTitle",
            "caption",
            "cta",
            "imageTextList",
            "hashtagList",
        ]

    def get_postTitle(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = EducationContentSocialMediaLanguage.objects.filter(
                educationContentSocialMediaId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.postTitle

    def get_caption(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = EducationContentSocialMediaLanguage.objects.filter(
                educationContentSocialMediaId=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 = EducationContentSocialMediaLanguage.objects.filter(
                educationContentSocialMediaId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.cta

    def get_imageTextList(self, instance):
        languageId = getLanguage(self)
        dataList = EducationContentSocialMediaImageText.objects.filter(
            educationContentSocialMediaId=instance.id
        )
        return EducationContentSocialMediaImageTextSerializer(
            dataList, context={languageKey: languageId}, many=True
        ).data

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


class EducationContentSocialMediaLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = EducationContentSocialMediaLanguage
        fields = [
            "id",
            "postTitle",
            "caption",
            "cta",
            "educationContentSocialMediaId",
            languageKey,
        ]


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

    class Meta:
        model = EducationContentSocialMediaImageText
        fields = [
            "id",
            "keyName",
            "educationContentSocialMediaId",
            "text",
        ]

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


class EducationContentSocialMediaImageTextLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = EducationContentSocialMediaImageTextLanguage
        fields = [
            "id",
            "text",
            "educationContentSocialMediaImageTextId",
            languageKey,
        ]


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

    class Meta:
        model = EducationContentSocialMediaHashtags
        fields = [
            "id",
            "keyName",
            "educationContentSocialMediaId",
            "text",
        ]

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


class EducationContentSocialMediaHashtagsLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = EducationContentSocialMediaHashtagsLanguage
        fields = [
            "id",
            "text",
            "educationContentSocialMediaImageTextId",
            languageKey,
        ]


class EducationContentAppContentSerializer(serializers.ModelSerializer):
    title = serializers.SerializerMethodField()
    introduction = serializers.SerializerMethodField()
    importantNote = serializers.SerializerMethodField()
    whatYouShouldKnowList = serializers.SerializerMethodField()

    class Meta:
        model = EducationContentAppContent
        fields = [
            "id",
            "educationContentId",
            "title",
            "introduction",
            "importantNote",
            "whatYouShouldKnowList",
        ]

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

    def get_introduction(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = EducationContentAppContentLanguage.objects.filter(
                educationContentAppContentId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.introduction

    def get_importantNote(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = EducationContentAppContentLanguage.objects.filter(
                educationContentAppContentId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.importantNote

    def get_whatYouShouldKnowList(self, instance):
        languageId = getLanguage(self)
        dataList = EducationContentAppContentWhatYouShouldKnow.objects.filter(
            educationContentAppContentId=instance.id
        )
        return EducationContentAppContentWhatYouShouldKnowSerializer(
            dataList, context={languageKey: languageId}, many=True
        ).data


class EducationContentAppContentLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = EducationContentAppContentLanguage
        fields = [
            "id",
            "title",
            "introduction",
            "importantNote",
            "educationContentAppContentId",
            languageKey,
        ]


class EducationContentAppContentWhatYouShouldKnowSerializer(serializers.ModelSerializer):
    title = serializers.SerializerMethodField()
    description = serializers.SerializerMethodField()

    class Meta:
        model = EducationContentAppContentWhatYouShouldKnow
        fields = [
            "id",
            "keyName",
            "educationContentAppContentId",
            "title",
            "description",
        ]

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

    def get_description(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = EducationContentAppContentWhatYouShouldKnowLanguage.objects.filter(
                educationContentAppContentWhatYouShouldKnowId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.description


class EducationContentAppContentWhatYouShouldKnowLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = EducationContentAppContentWhatYouShouldKnowLanguage
        fields = [
            "id",
            "title",
            "description",
            "educationContentAppContentWhatYouShouldKnowId",
            languageKey,
        ]
