from rest_framework import serializers

from .models import Driver, Kart, Params, Relay


class DriverSerializer(serializers.ModelSerializer):
    class Meta:
        model = Driver
        fields = ('id', 'short_name', 'first_name', 'last_name', 'order', 'ref_time')

    def update(self, instance, validated_data):
        instance.short_name = validated_data.get("short_name", instance.short_name)
        instance.first_name = validated_data.get("first_name", instance.first_name)
        instance.last_name = validated_data.get("last_name", instance.last_name)
        instance.order = validated_data.get("order", instance.order)
        instance.ref_time = validated_data.get("ref_time", instance.ref_time)
        instance.save()
        return instance

    def create(self, validated_data):
        return Driver.objects.create(**validated_data)


class KartSerializer(serializers.ModelSerializer):
    drivers = DriverSerializer(many=True, read_only=True)

    class Meta:
        model = Kart
        fields = ('id', 'name', 'autonomy', 'drivers')

    def update(self, instance, validated_data):
        instance.name = validated_data.get("name", instance.name)
        instance.autonomy = validated_data.get("autonomy", instance.autonomy)
        instance.comment = validated_data.get("comment", instance.comment)
        instance.save()
        return instance

    def create(self, validated_data):
        return Kart.objects.create(**validated_data)


class KartSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Kart
        fields = ('id', 'name', 'autonomy')

    def update(self, instance, validated_data):
        instance.name = validated_data.get("name", instance.name)
        instance.autonomy = validated_data.get("autonomy", instance.autonomy)
        instance.comment = validated_data.get("comment", instance.comment)
        instance.save()
        return instance

    def create(self, validated_data):
        return Kart.objects.create(**validated_data)


class RelaySerializer(serializers.ModelSerializer):

    drivers = DriverSerializer(many=True, read_only=True)

    class Meta:
        model = Relay
        fields = '__all__'
        depth = 1

    def update(self, instance, validated_data):
        instance.relay_start = validated_data.get("relay_start", instance.relay_start)
        instance.relay_end = validated_data.get("relay_end", instance.relay_end)
        instance.has_refuel = validated_data.get("has_refuel", instance.has_refuel)
        instance.driver_chrono = validated_data.get("driver_chrono", instance.driver_chrono)
        instance.relay_autonomy = validated_data.get("relay_autonomy", instance.relay_autonomy)
        instance.comment = validated_data.get("comment", instance.comment)
        instance.kart_id = validated_data.get("kart_id", instance.kart_id)
        instance.driver_id = validated_data.get("driver_id", instance.driver_id)
        instance.save()
        return instance

    def create(self, validated_data):
        return Relay.objects.create(**validated_data)


class ParamSerializer(serializers.ModelSerializer):
    class Meta:
        model = Params
        fields = ('id', 'default_relay', 'autonomy', 'stand_minimum_time', 'comment')

    def update(self, instance, validated_data):
        instance.default_relay = validated_data.get("default_relay", instance.default_relay)
        instance.autonomy = validated_data.get("autonomy", instance.autonomy)
        instance.stand_minimum_time = validated_data.get("stand_minimum_time", instance.stand_minimum_time)
        instance.comment = validated_data.get("comment", instance.comment)
        instance.save()
        return instance

    def create(self, validated_data):
        return Params.objects.create(**validated_data)
