from rest_framework import serializers

from common.common import getLanguage
from mama_care_api.settings import languageKey
from .problem_solution_content_models import (
    ProblemSolutionContent,
    ProblemSolutionContentSocialMedia,
    ProblemSolutionContentSocialMediaLanguage,
    ProblemSolutionContentSocialMediaImageText,
    ProblemSolutionContentSocialMediaImageTextLanguage,
    ProblemSolutionContentSocialMediaHashtags,
    ProblemSolutionContentSocialMediaHashtagsLanguage,
    ProblemSolutionContentAppContent,
    ProblemSolutionContentAppContentLanguage,
    ProblemSolutionContentAppContentPossibleCauses,
    ProblemSolutionContentAppContentPossibleCausesLanguage,
    ProblemSolutionContentAppContentRecommendedSolutions,
    ProblemSolutionContentAppContentRecommendedSolutionsLanguage,
)


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

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

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

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


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

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

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

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

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


class ProblemSolutionContentSocialMediaLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProblemSolutionContentSocialMediaLanguage
        fields = [
            "id",
            "postTitle",
            "caption",
            "cta",
            "problemSolutionContentSocialMediaId",
            languageKey,
        ]


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

    class Meta:
        model = ProblemSolutionContentSocialMediaImageText
        fields = [
            "id",
            "keyName",
            "type",
            "problemSolutionContentSocialMediaId",
            "text",
        ]

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


class ProblemSolutionContentSocialMediaImageTextLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProblemSolutionContentSocialMediaImageTextLanguage
        fields = [
            "id",
            "text",
            "problemSolutionContentSocialMediaImageTextId",
            languageKey,
        ]


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

    class Meta:
        model = ProblemSolutionContentSocialMediaHashtags
        fields = [
            "id",
            "keyName",
            "problemSolutionContentSocialMediaId",
            "text",
        ]

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


class ProblemSolutionContentSocialMediaHashtagsLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProblemSolutionContentSocialMediaHashtagsLanguage
        fields = [
            "id",
            "text",
            "problemSolutionContentSocialMediaHashtagsId",
            languageKey,
        ]


class ProblemSolutionContentAppContentSerializer(serializers.ModelSerializer):
    title = serializers.SerializerMethodField()
    problemDescription = serializers.SerializerMethodField()
    whenToSeekMedicalHelp = serializers.SerializerMethodField()
    importantNote = serializers.SerializerMethodField()
    possibleCauseList = serializers.SerializerMethodField()
    recommendedSolutionList = serializers.SerializerMethodField()

    class Meta:
        model = ProblemSolutionContentAppContent
        fields = [
            "id",
            "problemSolutionContentId",
            "title",
            "problemDescription",
            "whenToSeekMedicalHelp",
            "importantNote",
            "possibleCauseList",
            "recommendedSolutionList",
        ]

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

    def get_problemDescription(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = ProblemSolutionContentAppContentLanguage.objects.filter(
                problemSolutionContentAppContentId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.problemDescription

    def get_whenToSeekMedicalHelp(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = ProblemSolutionContentAppContentLanguage.objects.filter(
                problemSolutionContentAppContentId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.whenToSeekMedicalHelp

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

    def get_possibleCauseList(self, instance):
        languageId = getLanguage(self)
        dataList = ProblemSolutionContentAppContentPossibleCauses.objects.filter(
            problemSolutionContentAppContentId=instance.id
        )
        return ProblemSolutionContentAppContentPossibleCausesSerializer(
            dataList, context={languageKey: languageId}, many=True
        ).data

    def get_recommendedSolutionList(self, instance):
        languageId = getLanguage(self)
        dataList = ProblemSolutionContentAppContentRecommendedSolutions.objects.filter(
            problemSolutionContentAppContentId=instance.id
        )
        return ProblemSolutionContentAppContentRecommendedSolutionsSerializer(
            dataList, context={languageKey: languageId}, many=True
        ).data


class ProblemSolutionContentAppContentLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProblemSolutionContentAppContentLanguage
        fields = [
            "id",
            "title",
            "problemDescription",
            "whenToSeekMedicalHelp",
            "importantNote",
            "problemSolutionContentAppContentId",
            languageKey,
        ]


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

    class Meta:
        model = ProblemSolutionContentAppContentPossibleCauses
        fields = [
            "id",
            "keyName",
            "problemSolutionContentAppContentId",
            "title",
            "description",
        ]

    def get_title(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = ProblemSolutionContentAppContentPossibleCausesLanguage.objects.filter(
                problemSolutionContentAppContentPossibleCausesId=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 = ProblemSolutionContentAppContentPossibleCausesLanguage.objects.filter(
                problemSolutionContentAppContentPossibleCausesId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.description


class ProblemSolutionContentAppContentPossibleCausesLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProblemSolutionContentAppContentPossibleCausesLanguage
        fields = [
            "id",
            "title",
            "description",
            "problemSolutionContentAppContentPossibleCausesId",
            languageKey,
        ]


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

    class Meta:
        model = ProblemSolutionContentAppContentRecommendedSolutions
        fields = [
            "id",
            "keyName",
            "problemSolutionContentAppContentId",
            "title",
            "description",
        ]

    def get_title(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = ProblemSolutionContentAppContentRecommendedSolutionsLanguage.objects.filter(
                problemSolutionContentAppContentRecommendedSolutionsId=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 = ProblemSolutionContentAppContentRecommendedSolutionsLanguage.objects.filter(
                problemSolutionContentAppContentRecommendedSolutionsId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.description


class ProblemSolutionContentAppContentRecommendedSolutionsLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProblemSolutionContentAppContentRecommendedSolutionsLanguage
        fields = [
            "id",
            "title",
            "description",
            "problemSolutionContentAppContentRecommendedSolutionsId",
            languageKey,
        ]
