from rest_framework_gis import serializers
from rest_framework import serializers as s

from .models import (
    Artificialisee2015to2018,
    Artificielle2018,
    CommunesSybarval,
    CouvertureSol,
    EnveloppeUrbaine2018,
    Ocsge,
    Renaturee2018to2015,
    Sybarval,
    Voirie2018,
    ZonesBaties2018,
    UsageSol,
)


def get_label(code="", label=""):
    if code is None:
        code = "-"
    if label is None:
        label = "inconnu"
    return f"{code} {label[:30]}"


class Artificialisee2015to2018Serializer(serializers.GeoFeatureModelSerializer):
    usage_2015 = s.SerializerMethodField()
    usage_2018 = s.SerializerMethodField()
    couverture_2015 = s.SerializerMethodField()
    couverture_2018 = s.SerializerMethodField()

    def get_usage_2015(self, obj):
        return get_label(code=obj.us_2015, label=obj.us_2015_label)

    def get_usage_2018(self, obj):
        return get_label(code=obj.us_2018, label=obj.us_2018_label)

    def get_couverture_2015(self, obj):
        return get_label(code=obj.cs_2015, label=obj.cs_2015_label)

    def get_couverture_2018(self, obj):
        return get_label(code=obj.cs_2018, label=obj.cs_2018_label)

    class Meta:
        fields = (
            "id",
            "surface",
            "usage_2015",
            "usage_2018",
            "couverture_2015",
            "couverture_2018",
        )
        geo_field = "mpoly"
        model = Artificialisee2015to2018


class Artificielle2018Serializer(serializers.GeoFeatureModelSerializer):
    couverture = s.SerializerMethodField()

    def get_couverture(self, obj):
        return get_label(code=obj.couverture, label=obj.couverture_label)

    class Meta:
        fields = (
            "id",
            "surface",
            "couverture",
        )
        geo_field = "mpoly"
        model = Artificielle2018


class CommunesSybarvalSerializer(serializers.GeoFeatureModelSerializer):
    """Marker GeoJSON serializer."""

    class Meta:
        """Marker serializer meta class."""

        fields = (
            "nom",
            "code_insee",
            "surface",
        )
        geo_field = "mpoly"
        model = CommunesSybarval


class EnveloppeUrbaine2018Serializer(serializers.GeoFeatureModelSerializer):
    couverture = s.SerializerMethodField()

    def get_couverture(self, obj):
        return get_label(code=obj.couverture, label=obj.couverture_label)

    class Meta:
        fields = (
            "id",
            "couverture",
            "surface",
        )
        geo_field = "mpoly"
        model = EnveloppeUrbaine2018


class OcsgeSerializer(serializers.GeoFeatureModelSerializer):
    couverture = s.SerializerMethodField()
    usage = s.SerializerMethodField()

    def get_couverture(self, obj):
        return get_label(code=obj.couverture, label=obj.couverture_label)

    def get_usage(self, obj):
        return get_label(code=obj.usage, label=obj.usage_label)

    class Meta:
        fields = (
            "id",
            "couverture",
            "usage",
            "millesime",
            "map_color",
            "year",
        )
        geo_field = "mpoly"
        model = Ocsge


class Renaturee2018to2015Serializer(serializers.GeoFeatureModelSerializer):
    usage_2015 = s.SerializerMethodField()
    usage_2018 = s.SerializerMethodField()
    couverture_2015 = s.SerializerMethodField()
    couverture_2018 = s.SerializerMethodField()

    def get_usage_2015(self, obj):
        return get_label(code=obj.us_2015, label=obj.us_2015_label)

    def get_usage_2018(self, obj):
        return get_label(code=obj.us_2018, label=obj.us_2018_label)

    def get_couverture_2015(self, obj):
        return get_label(code=obj.cs_2015, label=obj.cs_2015_label)

    def get_couverture_2018(self, obj):
        return get_label(code=obj.cs_2018, label=obj.cs_2018_label)

    class Meta:
        fields = (
            "id",
            "surface",
            "usage_2015",
            "usage_2018",
            "couverture_2015",
            "couverture_2018",
        )
        geo_field = "mpoly"
        model = Renaturee2018to2015


class SybarvalSerializer(serializers.GeoFeatureModelSerializer):
    class Meta:
        fields = (
            "id",
            "surface",
        )
        geo_field = "mpoly"
        model = Sybarval


class Voirie2018Serializer(serializers.GeoFeatureModelSerializer):
    couverture = s.SerializerMethodField()
    usage = s.SerializerMethodField()

    def get_couverture(self, obj):
        return get_label(code=obj.couverture, label=obj.couverture_label)

    def get_usage(self, obj):
        return get_label(code=obj.usage, label=obj.usage_label)

    class Meta:
        fields = (
            "id",
            "surface",
            "couverture",
            "usage",
        )
        geo_field = "mpoly"
        model = Voirie2018


class ZonesBaties2018Serializer(serializers.GeoFeatureModelSerializer):
    couverture = s.SerializerMethodField()
    usage = s.SerializerMethodField()

    def get_couverture(self, obj):
        return get_label(code=obj.couverture, label=obj.couverture_label)

    def get_usage(self, obj):
        return get_label(code=obj.usage, label=obj.usage_label)

    class Meta:
        fields = (
            "id",
            "couverture",
            "usage",
            "surface",
        )
        geo_field = "mpoly"
        model = ZonesBaties2018


class CouvertureSolSerializer(serializers.ModelSerializer):
    class Meta:
        fields = (
            "id",
            "parent",
            "code",
            "label",
            "is_artificial",
        )
        model = CouvertureSol


class UsageSolSerializer(serializers.ModelSerializer):
    class Meta:
        fields = (
            "id",
            "parent",
            "code",
            "label",
        )
        model = UsageSol

[((590, 615), 'rest_framework.serializers.SerializerMethodField', 's.SerializerMethodField', ([], {}), '()\n', (613, 615), True, 'from rest_framework import serializers as s\n'), ((633, 658), 'rest_framework.serializers.SerializerMethodField', 's.SerializerMethodField', ([], {}), '()\n', (656, 658), True, 'from rest_framework import serializers as s\n'), ((681, 706), 'rest_framework.serializers.SerializerMethodField', 's.SerializerMethodField', ([], {}), '()\n', (704, 706), True, 'from rest_framework import serializers as s\n'), ((729, 754), 'rest_framework.serializers.SerializerMethodField', 's.SerializerMethodField', ([], {}), '()\n', (752, 754), True, 'from rest_framework import serializers as s\n'), ((1543, 1568), 'rest_framework.serializers.SerializerMethodField', 's.SerializerMethodField', ([], {}), '()\n', (1566, 1568), True, 'from rest_framework import serializers as s\n'), ((2281, 2306), 'rest_framework.serializers.SerializerMethodField', 's.SerializerMethodField', ([], {}), '()\n', (2304, 2306), True, 'from rest_framework import serializers as s\n'), ((2676, 2701), 'rest_framework.serializers.SerializerMethodField', 's.SerializerMethodField', ([], {}), '()\n', (2699, 2701), True, 'from rest_framework import serializers as s\n'), ((2714, 2739), 'rest_framework.serializers.SerializerMethodField', 's.SerializerMethodField', ([], {}), '()\n', (2737, 2739), True, 'from rest_framework import serializers as s\n'), ((3271, 3296), 'rest_framework.serializers.SerializerMethodField', 's.SerializerMethodField', ([], {}), '()\n', (3294, 3296), True, 'from rest_framework import serializers as s\n'), ((3314, 3339), 'rest_framework.serializers.SerializerMethodField', 's.SerializerMethodField', ([], {}), '()\n', (3337, 3339), True, 'from rest_framework import serializers as s\n'), ((3362, 3387), 'rest_framework.serializers.SerializerMethodField', 's.SerializerMethodField', ([], {}), '()\n', (3385, 3387), True, 'from rest_framework import serializers as s\n'), ((3410, 3435), 'rest_framework.serializers.SerializerMethodField', 's.SerializerMethodField', ([], {}), '()\n', (3433, 3435), True, 'from rest_framework import serializers as s\n'), ((4419, 4444), 'rest_framework.serializers.SerializerMethodField', 's.SerializerMethodField', ([], {}), '()\n', (4442, 4444), True, 'from rest_framework import serializers as s\n'), ((4457, 4482), 'rest_framework.serializers.SerializerMethodField', 's.SerializerMethodField', ([], {}), '()\n', (4480, 4482), True, 'from rest_framework import serializers as s\n'), ((4968, 4993), 'rest_framework.serializers.SerializerMethodField', 's.SerializerMethodField', ([], {}), '()\n', (4991, 4993), True, 'from rest_framework import serializers as s\n'), ((5006, 5031), 'rest_framework.serializers.SerializerMethodField', 's.SerializerMethodField', ([], {}), '()\n', (5029, 5031), True, 'from rest_framework import serializers as s\n')]