from rest_framework import serializers

from .models import (
    ETDRSGridAnnotation,
    MeasurementAnnotation,
    BooleanClassificationAnnotation,
    PolygonAnnotationSet,
    SinglePolygonAnnotation,
    LandmarkAnnotationSet,
    SingleLandmarkAnnotation,
)
from .validators import validate_grader_is_current_retina_user


class AbstractAnnotationSerializer(serializers.ModelSerializer):
    def validate_grader(self, value):
        """
        Validate that grader is the user creating the object for retina_graders group
        """
        validate_grader_is_current_retina_user(value, self.context)
        return value

    class Meta:
        abstract = True


class AbstractSingleAnnotationSerializer(serializers.ModelSerializer):
    def validate(self, data):
        """
        Validate that the user that is creating this object equals the
        annotation_set.grader for retina_graders
        """
        if data.get("annotation_set") is None:
            return data

        grader = data["annotation_set"].grader
        validate_grader_is_current_retina_user(grader, self.context)
        return data

    class Meta:
        abstract = True


class ETDRSGridAnnotationSerializer(AbstractAnnotationSerializer):
    class Meta:
        model = ETDRSGridAnnotation
        fields = ("id", "grader", "created", "image", "fovea", "optic_disk")


class MeasurementAnnotationSerializer(AbstractAnnotationSerializer):
    class Meta:
        model = MeasurementAnnotation
        fields = ("image", "grader", "created", "start_voxel", "end_voxel")


class BooleanClassificationAnnotationSerializer(AbstractAnnotationSerializer):
    class Meta:
        model = BooleanClassificationAnnotation
        fields = ("image", "grader", "created", "name", "value")


class SinglePolygonAnnotationSerializer(AbstractSingleAnnotationSerializer):
    annotation_set = serializers.PrimaryKeyRelatedField(
        queryset=PolygonAnnotationSet.objects.all()
    )

    class Meta:
        model = SinglePolygonAnnotation
        fields = ("id", "value", "annotation_set", "created")


class PolygonAnnotationSetSerializer(AbstractAnnotationSerializer):
    singlepolygonannotation_set = SinglePolygonAnnotationSerializer(
        many=True, read_only=True
    )

    class Meta:
        model = PolygonAnnotationSet
        fields = (
            "id",
            "image",
            "grader",
            "created",
            "name",
            "singlepolygonannotation_set",
        )


class SingleLandmarkAnnotationSerializer(AbstractSingleAnnotationSerializer):
    class Meta:
        model = SingleLandmarkAnnotation
        fields = ("image", "annotation_set", "landmarks")


class LandmarkAnnotationSetSerializer(AbstractAnnotationSerializer):
    singlelandmarkannotation_set = SingleLandmarkAnnotationSerializer(
        many=True, read_only=True
    )

    class Meta:
        model = LandmarkAnnotationSet
        fields = ("id", "grader", "created", "singlelandmarkannotation_set")

@@ -256,16 +256,110 @@
 tation,%0A
+    ImageQualityAnnotation,%0A    ImagePathologyAnnotation,%0A    RetinaImagePathologyAnnotation,%0A
 )%0Afrom .
@@ -3096,8 +3096,777 @@
 n_set%22)%0A
+%0A%0Aclass ImageQualityAnnotationSerializer(AbstractAnnotationSerializer):%0A    class Meta:%0A        model = ImageQualityAnnotation%0A        fields = (%22id%22, %22grader%22, %22image%22, %22quality%22)%0A%0A%0Aclass ImagePathologyAnnotationSerializer(AbstractAnnotationSerializer):%0A    class Meta:%0A        model = ImagePathologyAnnotation%0A        fields = (%22id%22, %22grader%22, %22image%22, %22pathology%22)%0A%0A%0Aclass RetinaImagePathologyAnnotationSerializer(AbstractAnnotationSerializer):%0A    class Meta:%0A        model = RetinaImagePathologyAnnotation%0A        fields = (%0A            %22id%22,%0A            %22grader%22,%0A            %22image%22,%0A            %22amd_present%22,%0A            %22dr_present%22,%0A            %22oda_present%22,%0A            %22myopia_present%22,%0A            %22cysts_present%22,%0A            %22other_present%22,%0A        )%0A

Add serializers for image quality, pathology presence and pathology type presence