from rest_framework import serializers

from common.common import getLanguage
from mama_care_api.settings import languageKey
from .tip_quote_models import (
    TipQuote,
    TipQuoteLanguage,
)


class TipQuoteSerializer(serializers.ModelSerializer):
    tipTitle = serializers.SerializerMethodField()
    tip = serializers.SerializerMethodField()
    tipExplanation = serializers.SerializerMethodField()
    quote = serializers.SerializerMethodField()
    cta = serializers.SerializerMethodField()

    class Meta:
        model = TipQuote
        fields = [
            "id",
            "contentInformationId",
            "tipTitle",
            "tip",
            "tipExplanation",
            "quote",
            "cta",
        ]

    def get_tipTitle(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = TipQuoteLanguage.objects.filter(
                tipQuoteId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.tipTitle

    def get_tip(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = TipQuoteLanguage.objects.filter(
                tipQuoteId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.tip

    def get_tipExplanation(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = TipQuoteLanguage.objects.filter(
                tipQuoteId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.tipExplanation

    def get_quote(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            translation = TipQuoteLanguage.objects.filter(
                tipQuoteId=instance.id,
                languageId=languageId,
            ).first()
            if translation is not None:
                return translation.quote

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


class TipQuoteLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = TipQuoteLanguage
        fields = [
            "id",
            "tipTitle",
            "tip",
            "tipExplanation",
            "quote",
            "cta",
            "tipQuoteId",
            languageKey,
        ]
