"""Main app serializers."""
from django.contrib.contenttypes.models import ContentType
from rest_framework import serializers

from location.serializers import CountrySerializer
from main import models
from utils.serializers import ProjectModelSerializer, TranslatedField, RecursiveFieldSerializer
from account.serializers.back import BackUserSerializer
from account.models import User


class FeatureSerializer(serializers.ModelSerializer):
    """Feature serializer."""

    class Meta:
        """Meta class."""

        model = models.Feature
        fields = (
            'id',
            'slug',
            'priority',
            'route',
            'site_settings',
        )


class CurrencySerializer(ProjectModelSerializer):
    """Currency serializer."""

    name_translated = TranslatedField()

    class Meta:
        model = models.Currency
        fields = [
            'id',
            'name_translated',
            'sign'
        ]


class _FooterLinkSerializer(serializers.ModelSerializer):
    """FooterLink serializer"""

    class Meta:
        model = models.FooterLink
        fields = [
            'title',
            'link',
        ]


class FooterSerializer(serializers.ModelSerializer):
    """Footer serializer."""

    class Meta:
        model = models.Footer
        fields = [
            'id',
            'about_us',
            'copyright',
            'created',
            'modified',
            'links',
        ]


class _FooterSerializer(FooterSerializer):
    """Footer serializer."""
    links = _FooterLinkSerializer(many=True, read_only=True)


class FooterBackSerializer(FooterSerializer):
    site_id = serializers.PrimaryKeyRelatedField(
        queryset=models.SiteSettings.objects.all(),
        source='site'
    )

    class Meta:
        model = models.Footer
        fields = FooterSerializer.Meta.fields + [
            'site_id'
        ]


class SiteFeatureSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField(source='feature.id')
    slug = serializers.CharField(source='feature.slug')
    priority = serializers.IntegerField(source='feature.priority')
    route = serializers.CharField(source='feature.route.name')
    source = serializers.IntegerField(source='feature.source')
    nested = RecursiveFieldSerializer(many=True, allow_null=True)

    class Meta:
        """Meta class."""
        model = models.SiteFeature
        fields = ('main',
                  'id',
                  'slug',
                  'priority',
                  'route',
                  'source',
                  'nested',
                  )


class SiteSettingsSerializer(serializers.ModelSerializer):
    """Site settings serializer."""

    published_features = SiteFeatureSerializer(source='published_sitefeatures',
                                               many=True, allow_null=True)
    currency = CurrencySerializer()
    # todo: remove this
    country_code = serializers.CharField(source='subdomain', read_only=True)

    country_name = serializers.CharField(source='country.name_translated', read_only=True)
    time_format = serializers.CharField(source='country.time_format', read_only=True)
    footers = _FooterSerializer(many=True, read_only=True)

    class Meta:
        """Meta class."""

        model = models.SiteSettings
        fields = [
            'country_code',
            'time_format',
            'subdomain',
            'pinterest_page_url',
            'twitter_page_url',
            'facebook_page_url',
            'instagram_page_url',
            'contact_email',
            'config',
            'ad_config',
            'published_features',
            'currency',
            'country_name',
            'footers',
        ]


class SiteSettingsBackOfficeSerializer(SiteSettingsSerializer):
    """Site settings serializer for back office."""

    class Meta(SiteSettingsSerializer.Meta):
        """Meta class."""
        fields = SiteSettingsSerializer.Meta.fields + [
            'id',
        ]


class SiteSerializer(SiteSettingsSerializer):
    country = CountrySerializer()

    class Meta:
        """Meta class."""
        model = models.SiteSettings
        fields = SiteSettingsSerializer.Meta.fields + [
            'id',
            'country'
        ]


class SiteShortSerializer(serializers.ModelSerializer):
    """Short serializer for model SiteSettings."""

    class Meta(SiteSerializer.Meta):
        """Meta class."""
        fields = [
            'subdomain',
        ]


class AwardBaseSerializer(serializers.ModelSerializer):
    """Award base serializer."""

    title_translated = serializers.CharField(read_only=True, allow_null=True)

    class Meta:
        model = models.Award
        fields = [
            'id',
            'title_translated',
            'vintage_year',
            'image_url',
        ]


class AwardSerializer(AwardBaseSerializer):
    """Award serializer."""

    class Meta:
        model = models.Award
        fields = AwardBaseSerializer.Meta.fields + ['award_type', ]


class BackAwardSerializer(AwardBaseSerializer):
    """Award serializer."""

    class Meta:
        model = models.Award
        fields = AwardBaseSerializer.Meta.fields + [
            'award_type',
            'state',
            'content_type',
            'object_id',
        ]


class CarouselListSerializer(serializers.ModelSerializer):
    """Serializer for retrieving list of carousel items."""

    model_name = serializers.CharField()
    name = serializers.CharField()
    toque_number = serializers.IntegerField()
    public_mark = serializers.IntegerField()
    image = serializers.URLField(source='image_url')
    awards = AwardBaseSerializer(many=True)
    vintage_year = serializers.IntegerField()
    last_award = AwardBaseSerializer(source='the_most_recent_award', allow_null=True)

    class Meta:
        """Meta class."""
        model = models.Carousel
        fields = [
            'id',
            'model_name',
            'name',
            'awards',
            'toque_number',
            'public_mark',
            'image',
            'vintage_year',
            'last_award',
            'slug',
            'link',
        ]


class PageBaseSerializer(serializers.ModelSerializer):
    """Serializer for model Page"""

    class Meta:
        """Meta class."""
        model = models.Page
        fields = [
            'id',
            'image_url',
            'width',
            'height',
            'advertisement',
        ]
        extra_kwargs = {
            'advertisement': {'write_only': True},
            'image_url': {'required': True},
            'width': {'required': True},
            'height': {'required': True},
        }


class PageExtendedSerializer(PageBaseSerializer):
    """Extended serializer for model Page."""
    source_display = serializers.CharField(read_only=True,
                                           source='get_source_display')

    class Meta(PageBaseSerializer.Meta):
        """Meta class."""
        fields = PageBaseSerializer.Meta.fields + [
            'source',
            'source_display',
        ]


class PageTypeBaseSerializer(serializers.ModelSerializer):
    """Serializer fro model PageType."""

    class Meta:
        """Meta class."""
        model = models.PageType
        fields = [
            'id',
            'name',
        ]


class ContentTypeBackSerializer(serializers.ModelSerializer):
    """Serializer for model ContentType."""

    class Meta:
        model = ContentType
        fields = '__all__'


class PanelSerializer(serializers.ModelSerializer):
    """Serializer for Custom panel."""
    user_id = serializers.PrimaryKeyRelatedField(
        queryset=User.objects.all(),
        source='user',
        write_only=True
    )
    user = BackUserSerializer(read_only=True)

    class Meta:
        model = models.Panel
        fields = [
            'id',
            'name',
            'display',
            'description',
            'query',
            'created',
            'modified',
            'user',
            'user_id'
        ]


class PanelExecuteSerializer(serializers.ModelSerializer):
    """Panel execute serializer."""

    class Meta:
        model = models.Panel
        fields = [
            'id',
            'name',
            'display',
            'description',
            'query',
            'created',
            'modified',
            'user',
            'user_id'
        ]
