# Serializers. Defines which fields from an object get to the JSON object
# Also optionally define POST, PATCH methods (create, update)

from rest_framework import serializers
from rowers.models import Workout,Rower,StrokeData,FavoriteChart

import datetime

# Serializers define the API representation.
class RowerSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Rower
        fields = (
            'id',
            'weightcategory',
            'max',
            'rest',
            'ut2',
            'ut1',
            'at',
            'tr',
            'an',
            'ftp',
            )

class FavoriteChartSerializer(serializers.ModelSerializer):
    class Meta:
        model = FavoriteChart
        fields = (
            'id',
            'xparam',
            'yparam1',
            'yparam2',
            'plottype',
            'workouttype',
            'reststrokes',
            'user'
            )
        
class WorkoutSerializer(serializers.ModelSerializer):
    class Meta:
        model = Workout
        fields = (
            'id',
            'name',
            'date',
            'workouttype',
            'starttime',
            'distance',
            'duration',
            'averagehr',
            'maxhr',
            'notes',
            'summary',
        )

    def create(self, validated_data):
        r = Rower.objects.get(user=self.context['request'].user)
        d = validated_data['date']
        t = validated_data['starttime']
        rowdatetime = datetime.datetime(d.year,
                                        d.month,
                                        d.day,
                                        t.hour,
                                        t.minute,
                                        t.second)
        w = Workout(user=r,
                    name=validated_data['name'],
		    date=validated_data['date'],
                    workouttype=validated_data['workouttype'],
		    duration=validated_data['duration'],
                    distance=validated_data['distance'],
		    weightcategory=r.weightcategory,
		    starttime=validated_data['starttime'],
		    csvfilename='',
                    notes=validated_data['notes'],
		    uploadedtoc2=0,
                    summary=validated_data['summary'],
		    averagehr=validated_data['averagehr'],
                    maxhr=validated_data['maxhr'],
		    startdatetime=rowdatetime)
        w.save()
        return w
        
    def update(self, instance, validated_data):
        d = validated_data['date']
        t = validated_data['starttime']
        rowdatetime = datetime.datetime(d.year,
                                        d.month,
                                        d.day,
                                        t.hour,
                                        t.minute,
                                        t.second)


        instance.name=validated_data['name']
	instance.date=validated_data['date']
        instance.workouttype=validated_data['workouttype']
	instance.duration=validated_data['duration']
        instance.distance=validated_data['distance']
	instance.starttime=validated_data['starttime']
        instance.notes=validated_data['notes']
        instance.summary=validated_data['summary']
	instance.averagehr=validated_data['averagehr']
        instance.maxhr=validated_data['maxhr']
	instance.startdatetime=rowdatetime
        instance.save()
        return instance

# This is just a fake one for URL registration purposes
class StrokeDataSerializer(serializers.Serializer):
    workoutid = serializers.IntegerField
    strokedata = serializers.JSONField
    
    def create(self, workoutid, strokedata):
        """
        Create and enter a new set of stroke data  into the DB
        """

        # do something
        print "fake serializer"
        return 1

    

