import datetime

from django.urls import reverse

from rest_framework import serializers

from .models import *

class VEVENT_Serializer(serializers.ModelSerializer):
    title = serializers.CharField(source='SUMMARY')
    start = serializers.DateTimeField(source='DTSTART')

    # end   = serializers.CharField(source='DTEND')
    end = serializers.SerializerMethodField()
    className = serializers.SerializerMethodField()
    url = serializers.SerializerMethodField()
    description = serializers.SerializerMethodField()
    # allDay = serializers.SerializerMethodField()

    def get_end(self, obj):
        x = obj.DURATION
        return obj.DTSTART + datetime.timedelta(hours=x.hour, minutes=x.minute, seconds=x.second)

    def get_className(self, obj):
        return 'c%d' % obj.mode

    def get_url(self, obj):
        # print(obj.treatment.id)
        return reverse('event-update', 
            kwargs={'pk': obj.id},
            )
        return reverse('treatment-detail', 
            # args=[obj.treatment.id],
            kwargs={'object_id': obj.treatment.id},
            )

    def get_description(self, obj):
        return None

        t = obj.treatment
        desc = t.patient.medical_records
        if t.surgeon:
            desc += ' '+t.surgeon.name
        elif t.oncologist:
            desc += ' '+t.oncologist.name
        print(desc)
        return desc

    def get_allDay(self, obj):
        return obj.DTSTART.hour < 8

    class Meta:
        model = VEVENT
        fields = ('title', 'start', 'end', 'id', 'className', 'url', 'description')


class PatientSerializer(serializers.ModelSerializer):
    # id = serializers.IntegerField(read_only=True)

    class Meta:
        model = Patient
        fields = '__all__'
        # fields = (
        #     'id', 'name',
        # )