# This is just a scratch pad to temporarily park code, just in case I need
# it later. Hardly used since i have proper versioning

# 
def createc2workoutdata_grouped(w):
    filename = w.csvfilename
    row = rowingdata(filename)

    # resize per minute
    df = row.df.groupby(lambda x:x/10).mean()
    
    averagehr = int(df[' HRCur (bpm)'].mean())
    maxhr = int(df[' HRCur (bpm)'].max())

    # adding diff, trying to see if this is valid
    t = 10*df.ix[:,' ElapsedTime (sec)'].values
    t[0] = t[1]
    d = df.ix[:,' Horizontal (meters)'].values
    d[0] = d[1]
    p = 10*df.ix[:,' Stroke500mPace (sec/500m)'].values
    t = t.astype(int)
    d = d.astype(int)
    p = p.astype(int)
    spm = df[' Cadence (stokes/min)'].astype(int)
    spm[0] = spm[1]
    hr = df[' HRCur (bpm)'].astype(int)
    stroke_data = []
    for i in range(len(t)):
	thisrecord = {"t":t[i],"d":d[i],"p":p[i],"spm":spm[i],"hr":hr[i]}
	stroke_data.append(thisrecord)


    try:
	durationstr = datetime.strptime(str(w.duration),"%H:%M:%S.%f")
    except ValueError:
	durationstr = datetime.strptime(str(w.duration),"%H:%M:%S")


    data = {
	"type": w.workouttype,
	"date": w.startdatetime.isoformat(),
	"distance": int(w.distance),
	"time": int(10*makeseconds(durationstr)),
	"weight_class": c2wc(w.weightcategory),
	"timezone": "Etc/UTC",
	"comments": w.notes,
	"heart_rate": {
	    "average": averagehr,
	    "max": maxhr,
	    },
	"stroke_data": stroke_data,
	}


    return data

@login_required()
def workout_edit_view(request,id=0):
    if request.method == 'POST':
	# Form was submitted
	form = WorkoutForm(request.POST)
	if form.is_valid():
	    # Get values from form
	    name = request.POST['name']
	    date = request.POST['date']
	    workouttype = request.POST['workouttype']
	    notes = request.POST['notes']
	    try:
		# check if valid ID exists (workout exists)
		row = Workout.objects.get(id=id)
		# check if user is owner of this workout
		if checkworkoutuser(request.user,row):
		    row.name  = name
		    row.date = date
		    row.workouttype = workouttype
		    row.notes = notes
		    row.save()
		    response = HttpResponse("data updated")
		else:
		    response = HttpResponse("You are not allowed to change this workout")
	    except Workout.DoesNotExist:
		# create new workout
		r = Rower.objects.get(user=request.user)
		w = Workout(name=name,date=date,workouttype=workouttype,
			    user=r)
		w.save()
		response = HttpResponse("New workout created")
	else:
	    response = HttpResponse("invalid form")

	return response
    else:
	try:
	    row = Workout.objects.get(id=id)
	    form = WorkoutForm(instance=row)
	    # check if user is owner of this workout

	    if (checkworkoutuser(request.user,row)==False):
		return HttpResponse("You are not allowed to edit this workout")
	except Workout.DoesNotExist:
	    form = WorkoutForm(
		initial = {'workouttype' : 'rower'}
	    )

	return render(request, 'workout_form.html',
		      {'form':form,})
