""" Background tasks done by Celery (develop) or QR (production) """
import os
import time
import gc
import gzip
import shutil
import numpy as np

from scipy import optimize

import rowingdata

from rowingdata import rowingdata as rdata

from celery import app
import datetime
import pytz
import iso8601

from matplotlib.backends.backend_agg import FigureCanvas
#from matplotlib.backends.backend_cairo import FigureCanvasCairo as FigureCanvas
import matplotlib.pyplot as plt

from rowsandall_app.settings import SITE_URL
from rowsandall_app.settings_dev import SITE_URL as SITE_URL_DEV
from rowsandall_app.settings import PROGRESS_CACHE_SECRET

import pandas as pd

from django_rq import job
from django.utils import timezone

from utils import deserialize_list

from rowers.dataprepnodjango import (
    update_strokedata, new_workout_from_file,
    getsmallrowdata_db, updatecpdata_sql,
    update_agegroup_db,fitnessmetric_to_sql,
    add_c2_stroke_data_db,totaltime_sec_to_string,
    create_c2_stroke_data_db
)

from django.core.mail import send_mail, EmailMessage
from django.db.utils import OperationalError

import datautils
import utils
import requests
import longtask
import arrow

# testing task


@app.task
def add(x, y):
    return x + y


@app.task
def handle_c2_import_stroke_data(c2token,
                                 c2id,workoutid,
                                 starttimeunix,
                                 csvfilename,debug=True,**kwargs):
    
    authorizationstring = str('Bearer ' + c2token)
    headers = {'Authorization': authorizationstring,
	       'user-agent': 'sanderroosendaal',
	       'Content-Type': 'application/json'}
    url = "https://log.concept2.com/api/users/me/results/"+str(c2id)+"/strokes"
    s = requests.get(url,headers=headers)
    if s.status_code == 200:
        strokedata = pd.DataFrame.from_dict(s.json()['data'])
        result = add_c2_stroke_data_db(
            strokedata,workoutid,starttimeunix,
            csvfilename,debug=debug,
        )

        return 1
    else:
	url = "https://log.concept2.com/api/users/me/results/"+str(c2id)
	s = requests.get(url,headers=headers)

        if s.status_code == 200:
            workoutdata = s.json()['data']
            distance = workoutdata['distance']
            c2id = workoutdata['id']
            workouttype = workoutdata['type']
            verified = workoutdata['verified']
            startdatetime = iso8601.parse_date(workoutdata['date'])
            weightclass = workoutdata['weight_class']
            weightcategory = 'hwt'
            if weightclass == "L":
                weightcategory = 'lwt'
            totaltime = workoutdata['time']/10.
            duration = totaltime_sec_to_string(totaltime)
            duration = datetime.datetime.strptime(duration,'%H:%M:%S.%f').time()

            try:
                timezone_str = tz(workoutdata['timezone'])
            except:
                timezone_str = 'UTC'

            workoutdate = startdatetime.astimezone(
                pytz.timezone(timezone_str)
            ).strftime('%Y-%m-%d')
            starttime = startdatetime.astimezone(
                pytz.timezone(timezone_str)
            ).strftime('%H:%M:%S')

            result = create_c2_stroke_data_db(
                distance,duration,workouttype,
                workoutid,starttimeunix,
                csvfilename,debug=debug,
            )
            
            
            return 1
        
        return 0

    return 0

def getagegrouprecord(age,sex='male',weightcategory='hwt',
                      distance=2000,duration=None,indf=pd.DataFrame()):

    if not duration:
        df = indf[indf['distance'] == distance]
    else:
        duration = 60*int(duration)
        df = indf[indf['duration'] == duration]

    if not df.empty:
        ages = df['age']
        powers = df['power']

        #poly_coefficients = np.polyfit(ages,powers,6)
        fitfunc = lambda pars, x: np.abs(pars[0])*(1-x/max(120,pars[1]))-np.abs(pars[2])*np.exp(-x/np.abs(pars[3]))+np.abs(pars[4])*(np.sin(np.pi*x/max(50,pars[5])))
        errfunc = lambda pars, x,y: fitfunc(pars,x)-y

        p0 = [700,120,700,10,100,100]

        p1, success = optimize.leastsq(errfunc,p0[:],
                                       args = (ages,powers))

        if success:
            power = fitfunc(p1, float(age))

            #power = np.polyval(poly_coefficients,age)

            power = 0.5*(np.abs(power)+power)
        else:
            power = 0
    else:
        power = 0

    return power


@app.task(bind=True)
def handle_getagegrouprecords(self,
                              df,
                              distances,durations,
                              age,sex,weightcategory,
                              **kwargs):
    wcdurations = []
    wcpower = []

    if 'debug' in kwargs:
        debug = kwargs['debug']
    else:
        debug = False


    df = pd.read_json(df)

    if sex == 'not specified':
        return 0
    
    for distance in distances:
        worldclasspower = getagegrouprecord(
            age,
            sex=sex,
            distance=distance,
            weightcategory=weightcategory,indf=df,
        )
        velo = (worldclasspower/2.8)**(1./3.)
        try:
            duration = distance/velo
            wcdurations.append(duration)
            wcpower.append(worldclasspower)
        except ZeroDivisionError:
            pass



    for duration in durations:
        worldclasspower = getagegrouprecord(
            age,
            sex=sex,
            duration=duration,
            weightcategory=weightcategory,indf=df
        )
        try:
            velo = (worldclasspower/2.8)**(1./3.)
            distance = int(60*duration*velo)
            wcdurations.append(60.*duration)
            wcpower.append(worldclasspower)
        except ValueError:
            pass

    update_agegroup_db(age,sex,weightcategory,wcdurations,wcpower,
                       debug=debug)

    return 1


@app.task(bind=True)
def long_test_task(self,aantal,debug=False,job=None,session_key=None):
    job = self.request

    return longtask.longtask(aantal,jobid=job.id,debug=debug,
                             session_key=session_key)
    
@app.task(bind=True)
def long_test_task2(self,aantal,**kwargs):
                    #debug=False,job=None,jobid='aap'):
    job = self.request
    job_id = job.id
    
    if 'jobkey' in kwargs:
        job_id = kwargs.pop('jobkey')
        
    kwargs['jobid'] = job_id
        
    return longtask.longtask2(aantal,**kwargs)


    

# create workout
@app.task
def handle_new_workout_from_file(r, f2,
                                 workouttype='rower',
                                 title='Workout',
                                 makeprivate=False,
                                 notes='',debug=False):
    return new_workout_from_file(r, f2, workouttype,
                                 title, makeprivate, notes)

# process and update workouts


@app.task
def handle_updatedps(useremail, workoutids, debug=False):
    for wid, f1 in workoutids:
        havedata = 1
        try:
            rowdata = rdata(f1)
        except IOError:
            try:
                rowdata = rdata(f1 + '.csv')
            except IOError:
                try:
                    rowdata = rdata(f1 + '.gz')
                except IOError:
                    havedata = 0

        if havedata:
            update_strokedata(wid, rowdata.df, debug=debug)

    subject = "Rowsandall.com Your Distance per Stroke metric has been updated"
    message = "All your workouts now have Distance per Stroke"

    email = EmailMessage(subject, message,
                         'Rowsandall <info@rowsandall.com>',
                         [useremail])

    res = email.send()

    return 1

# send email when a breakthrough workout is uploaded


@app.task
def handle_sendemail_breakthrough(workoutid, useremail,
                                  userfirstname, userlastname,
                                  btvalues=pd.DataFrame().to_json(),
                                  **kwargs):

    # send email with attachment
    subject = "A breakthrough workout on rowsandall.com"
    message = "Dear " + userfirstname + ",\n"
    message += "Congratulations! Your recent workout has been analyzed"
    message += " by Rowsandall.com and it appears your fitness,"
    message += " as measured by Critical Power, has improved!"
    message += " Critical Power (CP) is the power that you can "
    message += "sustain for a given duration. For more, see this "
    message += " article in the analytics blog:\n\n"
    message += " http://analytics.rowsandall.com/2017/06/17/how-do-we-calculate-critical-power/ \n\n"
    message += "Link to the workout http://rowsandall.com/rowers/workout/"
    message += str(workoutid)
    message += "/edit\n\n"
    message += "To add the workout to your Ranking workouts and see the updated CP plot, click the following link:\n"
    message += "http://rowsandall.com/rowers/workout/"
    message += str(workoutid)
    message += "/updatecp\n\n"

    btvalues = pd.read_json(btvalues)
    btvalues.sort_values('delta', axis=0, inplace=True)

    if not btvalues.empty:
        message += "These were the breakthrough values:\n"
        for t in btvalues.itertuples():
            delta = t.delta
            cpvalue = t.cpvalues
            pwr = t.pwr

            message += "Time: {delta} seconds\n".format(
                delta=delta
            )
            message += "New: {cpvalue:.0f} Watt\n".format(
                cpvalue=cpvalue
            )
            message += "Old: {pwr:.0f} Watt\n\n".format(
                pwr=pwr
            )

    message += "To opt out of these email notifications, deselect the checkbox on your Profile page under Account Information.\n\n"

    message += "Best Regards, the Rowsandall Team"

    email = EmailMessage(subject, message,
                         'Rowsandall <info@rowsandall.com>',
                         [useremail])

    res = email.send()

    # remove tcx file
    return 1

# send email when a breakthrough workout is uploaded


@app.task
def handle_sendemail_hard(workoutid, useremail,
                          userfirstname, userlastname,
                          btvalues=pd.DataFrame().to_json(),
                          debug=False,**kwargs):

    # send email with attachment
    subject = "That was a pretty hard workout on rowsandall.com"
    message = "Dear " + userfirstname + ",\n"
    message += "Congratulations! Your recent workout has been analyzed"
    message += " by Rowsandall.com and it appears that it was pretty hard work."
    message += " You were working pretty close to your Critical Power\n\n"
    message += " Critical Power (CP) is the power that you can "
    message += "sustain for a given duration. For more, see this "
    message += " article in the analytics blog:\n\n"
    message += " http://analytics.rowsandall.com/2017/06/17/how-do-we-calculate-critical-power/ \n\n"
    message += "Link to the workout http://rowsandall.com/rowers/workout/"
    message += str(workoutid)
    message += "/edit\n\n"

    message += "To opt out of these email notifications, deselect the checkbox on your Profile page under Account Information.\n\n"

    message += "Best Regards, the Rowsandall Team"

    email = EmailMessage(subject, message,
                         'Rowsandall <info@rowsandall.com>',
                         [useremail])

    res = email.send()

    # remove tcx file
    return 1


# send email to me when an unrecognized file is uploaded
@app.task
def handle_sendemail_unrecognized(unrecognizedfile, useremail,
                                  debug=False,**kwargs):

    # send email with attachment
    fullemail = 'roosendaalsander@gmail.com'
    subject = "Unrecognized file from Rowsandall.com"
    message = "Dear Sander,\n\n"
    message += "Please find attached a file that someone tried to upload to rowsandall.com. The file was not recognized as a valid file type.\n\n"
    message += "User Email " + useremail + "\n\n"
    message += "Best Regards, the Rowsandall Team"

    email = EmailMessage(subject, message,
                         'Rowsandall <info@rowsandall.com>',
                         [fullemail])

    try:
        email.attach_file(unrecognizedfile)
    except IOError:
        pass

    res = email.send()

    # remove tcx file
    try:
        os.remove(unrecognizedfile)
    except:
        pass

    return 1


# send email to owner when an unrecognized file is uploaded
@app.task
def handle_sendemail_unrecognizedowner(useremail, userfirstname,
                                       debug=False,**kwargs):

    # send email with attachment
    fullemail = useremail
    subject = "Unrecognized file from Rowsandall.com"
    message = "Dear " + userfirstname + ",\n\n"
    message += """
    The file you tried to send to rowsandall.com was not recognized by 
    our email processing system. You may have sent a file in a format 
    that is not supported. Sometimes, rowing apps make file format changes. 
    When that happens, it takes some time for rowsandall.comm to make
    the necessary changes on our side and support the app again.

    The file has been sent to the developer at rowsandall.com for evaluation.


    """

    message += "Best Regards, the Rowsandall Team"

    email = EmailMessage(subject, message,
                         'Rowsandall <info@rowsandall.com>',
                         [fullemail])

    res = email.send()

    return 1


# Send email with TCX attachment
@app.task
def handle_sendemailtcx(first_name, last_name, email, tcxfile,**kwargs):

    # send email with attachment
    fullemail = first_name + " " + last_name + " " + "<" + email + ">"
    subject = "File from Rowsandall.com"
    message = "Dear " + first_name + ",\n\n"
    message += "Please find attached the requested file for your workout.\n\n"
    message += "Best Regards, the Rowsandall Team"

    email = EmailMessage(subject, message,
                         'Rowsandall <info@rowsandall.com>',
                         [fullemail])

    email.attach_file(tcxfile)

    res = email.send()

    # remove tcx file
    os.remove(tcxfile)
    return 1


@app.task
def handle_zip_file(emailfrom, subject, file,**kwargs):
    message = "... zip processing ... "
    try:
        debug = kwargs['debug']
    except KeyError:
        debug = False
        
    if debug:
        print message
        
    email = EmailMessage(subject, message,
                         emailfrom,
                         ['workouts@rowsandall.com'])
    email.attach_file(file)
    if debug:
        print "attaching"
    res = email.send()
    if debug:
        print "sent"
    time.sleep(60)
    return 1

# Send email with CSV attachment


@app.task
def handle_sendemailcsv(first_name, last_name, email, csvfile,**kwargs):

    # send email with attachment
    fullemail = first_name + " " + last_name + " " + "<" + email + ">"
    subject = "File from Rowsandall.com"
    message = "Dear " + first_name + ",\n\n"
    message += "Please find attached the requested file for your workout.\n\n"
    message += "Best Regards, the Rowsandall Team"

    email = EmailMessage(subject, message,
                         'Rowsandall <info@rowsandall.com>',
                         [fullemail])

    if os.path.isfile(csvfile):
        email.attach_file(csvfile)
    else:
        csvfile2 = csvfile
        with gzip.open(csvfile + '.gz', 'rb') as f_in, open(csvfile2, 'wb') as f_out:
            shutil.copyfileobj(f_in, f_out)

        email.attach_file(csvfile2)
        os.remove(csvfile2)

    res = email.send()

    return 1

# Calculate wind and stream corrections for OTW rowing


@app.task(bind=True)
def handle_otwsetpower(self,f1, boattype, weightvalue,
                       first_name, last_name, email, workoutid,
                       **kwargs):

    job = self.request
    job_id = job.id
    
    if 'jobkey' in kwargs:
        job_id = kwargs.pop('jobkey')
    if 'ps' in kwargs:
        ps = kwargs['ps']
    else:
        ps = [1,1,1,1]

    if 'ratio' in kwargs:
        ratio = kwargs['ratio']
    else:
        ratio = 1.0
    if 'debug' in kwargs:
        debug = kwargs['debug']
    else:
        debug = False

    if 'quick_calc' in kwargs:
        usetable = kwargs['quick_calc']
    else:
        usetable = False

    kwargs['jobid'] = job_id


    try:
        rowdata = rdata(f1)
    except IOError:
        try:
            rowdata = rdata(f1 + '.csv')
        except IOError:
            rowdata = rdata(f1 + '.gz')

    weightvalue = float(weightvalue)

    # do something with boat type
    boatfile = {
        '1x': 'static/rigging/1x.txt',
        '2x': 'static/rigging/2x.txt',
        '2-': 'static/rigging/2-.txt',
        '4x': 'static/rigging/4x.txt',
        '4-': 'static/rigging/4-.txt',
        '8+': 'static/rigging/8+.txt',
    }
    try:
        rg = rowingdata.getrigging(boatfile[boattype])
    except KeyError:
        rg = rowingdata.getrigging('static/rigging/1x.txt')

    # do calculation, but do not overwrite NK Empower Power data
    powermeasured = False
    try:
        w = rowdata.df['wash']
        if w.mean() != 0:
            powermeasured = True
    except KeyError:
        pass

    progressurl = SITE_URL
    siteurl = SITE_URL
    if debug:
        progressurl = SITE_URL_DEV
        siteurl = SITE_URL_DEV
    secret = PROGRESS_CACHE_SECRET

    progressurl += "/rowers/record-progress/"
    progressurl += job_id

    # determine cache file name
    physics_cache = 'media/'+str(boattype)+'_'+str(int(weightvalue))

    
    rowdata.otw_setpower(skiprows=5, mc=weightvalue, rg=rg,
                         powermeasured=powermeasured,
                         progressurl=progressurl,
                         secret=secret,
                         silent=True,
                         usetable=usetable,storetable=physics_cache,
    )

    # save data
    rowdata.write_csv(f1, gzip=True)
    update_strokedata(workoutid, rowdata.df, debug=debug)

    totaltime = rowdata.df['TimeStamp (sec)'].max(
    ) - rowdata.df['TimeStamp (sec)'].min()
    try:
        totaltime = totaltime + rowdata.df.ix[0, ' ElapsedTime (sec)']
    except KeyError:
        pass
    df = getsmallrowdata_db(
        ['power', 'workoutid', 'time'], ids=[workoutid],
        debug=debug)
    thesecs = totaltime
    maxt = 1.05 * thesecs
    logarr = datautils.getlogarr(maxt)
    dfgrouped = df.groupby(['workoutid'])
    delta, cpvalues, avgpower = datautils.getcp(dfgrouped, logarr)

    #delta,cpvalues,avgpower = datautils.getsinglecp(rowdata.df)
    res, btvalues, res2 = utils.isbreakthrough(
        delta, cpvalues, ps[0], ps[1], ps[2], ps[3], ratio)
    if res:
        handle_sendemail_breakthrough(
            workoutid, email,
            first_name,
            last_name, btvalues=btvalues.to_json())

    # send email
    fullemail = first_name + " " + last_name + " " + "<" + email + ">"
    subject = "Your Rowsandall OTW calculations are ready"
    message = "Dear " + first_name + ",\n\n"
    message += "Your Rowsandall OTW calculations are ready.\n"
    message += "Thank you for using rowsandall.com.\n\n"
    message += "Rowsandall OTW calculations have not been fully implemented yet.\n"
    message += "We are now running an experimental version for debugging purposes. \n"
    message += "Your wind/stream corrected plot is available here: "
    message += siteurl+"/rowers/workout/"
    message += str(workoutid)
    message += "/interactiveotwplot\n\n"
    message += "Please report any bugs/inconsistencies/unexpected results at rowsandall.slack.com or by reply to this email.\n\n"
    message += "Best Regards, The Rowsandall Physics Department."

    send_mail(subject, message,
                  'Rowsandall Physics Department <info@rowsandall.com>',
                  [fullemail])

    return 1


@app.task
def handle_updateergcp(rower_id,workoutfilenames,debug=False,**kwargs):
    therows = []
    for f1 in workoutfilenames:
        try:
            rowdata = rdata(f1)
        except IOError:
            try:
                rowdata = rdata(f1 + '.csv')
            except IOError:
                try:
                    rowdata = rdata(f1 + '.gz')
                except IOError:
                    rowdata = 0
        
	if rowdata != 0:
	    therows.append(rowdata)

    cpdata = rowingdata.cumcpdata(therows)
    cpdata.columns = cpdata.columns.str.lower()
    
    updatecpdata_sql(rower_id,cpdata['delta'],cpdata['cp'],
                     table='ergcpdata',distance=cpdata['distance'],
                     debug=debug)

    return 1

@app.task
def handle_updatefitnessmetric(user_id,mode,workoutids,debug=False,
                               **kwargs):

    powerfourmin = -1
    power2k = -1
    powerhour = -1
    
    mdict = {
        'user_id': user_id,
        'PowerFourMin': powerfourmin,
        'PowerTwoK': power2k,
        'PowerOneHour': powerhour,
        'workoutmode': mode,
        'last_workout': max(workoutids),
        'date': timezone.now().strftime('%Y-%m-%d'),
             }

    result = fitnessmetric_to_sql(mdict,debug=debug,doclean=False)

    columns = ['power','workoutid','time']
    df = getsmallrowdata_db(columns,ids=workoutids,debug=debug)
    df.dropna(inplace=True,axis=0)

    if df.empty:
        # change this
        return 2

    # df is not empty. We continue
    dfgrouped = df.groupby(['workoutid'])
    maxt = 1.05*df['time'].max()/1000.

    logarr = datautils.getlogarr(maxt)
        
    delta,cpvalue,avgpower = datautils.getcp(dfgrouped,logarr)

    powerdf = pd.DataFrame({
        'Delta':delta,
        'CP':cpvalue,
        })

    powerdf = powerdf[powerdf['CP']>0]
    powerdf.dropna(axis=0,inplace=True)
    powerdf.sort_values(['Delta','CP'],ascending=[1,0],inplace=True)
    powerdf.drop_duplicates(subset='Delta',keep='first',inplace=True)

    p1,fitt,fitpower,ratio = datautils.cpfit(powerdf)
    # This is code duplication from datautils -- correct asap
    fitfunc = lambda pars,x: abs(pars[0])/(1+(x/abs(pars[2]))) + abs(pars[1])/(1+(x/abs(pars[3])))

    powerfourmin = fitfunc(p1,240.)
    powerhour = fitfunc(p1,3600.)

    # 2k power
    velofourmin = (powerfourmin/2.8)**(1./3.)
    dfourmin = 240.*velofourmin
    dratio = 2000./dfourmin
    pacefourmin = 500./velofourmin

    # assume 5 sec per doubling drop
    pace2k = pacefourmin + 5.*np.log10(dratio)/np.log10(2.)
    velo2k = 500./pace2k
    t2k = 2000./velo2k
    pwr2k = fitfunc(p1,t2k)
    velo2 = (pwr2k/2.8)**(1./3.)
    if np.isnan(velo2) or velo2 <= 0:
        velo2 = 1.0

    t2 = 2000./velo2

    pwr2k = fitfunc(p1,t2)

    velo3 = (pwr2k/2.8)**(1./3.)

    t3 = 2000./velo3

    power2k = fitfunc(p1,t3)

    
    mdict = {
        'user_id': user_id,
        'PowerFourMin': powerfourmin,
        'PowerTwoK': power2k,
        'PowerOneHour': powerhour,
        'workoutmode': mode,
        'last_workout': max(workoutids),
        'date': timezone.now().strftime('%Y-%m-%d'),
             }

    result = fitnessmetric_to_sql(mdict,debug=debug,doclean=True)

    return result

@app.task
def handle_updatecp(rower_id,workoutids,debug=False,table='cpdata',**kwargs):
    columns = ['power','workoutid','time']
    df = getsmallrowdata_db(columns,ids=workoutids,debug=debug)
    dfgrouped = df.groupby(['workoutid'])

    if not df.empty:
        maxt = 1.05*df['time'].max()/1000.
    else:
        maxt = 1000.

    logarr = datautils.getlogarr(maxt)

        
    delta,cpvalue,avgpower = datautils.getcp(dfgrouped,logarr)

    updatecpdata_sql(rower_id,delta,cpvalue,debug=debug,table=table)

    return 1
        
@app.task
def handle_makeplot(f1, f2, t, hrdata, plotnr, imagename,
                    debug=False,**kwargs):

    hrmax = hrdata['hrmax']
    hrut2 = hrdata['hrut2']
    hrut1 = hrdata['hrut1']
    hrat = hrdata['hrat']
    hrtr = hrdata['hrtr']
    hran = hrdata['hran']
    ftp = hrdata['ftp']
    powerzones = deserialize_list(hrdata['powerzones'])
    powerperc = np.array(deserialize_list(hrdata['powerperc'])).astype(int)

    rr = rowingdata.rower(hrmax=hrmax, hrut2=hrut2,
                          hrut1=hrut1, hrat=hrat,
                          hrtr=hrtr, hran=hran,
                          ftp=ftp, powerperc=powerperc,
                          powerzones=powerzones)
    try:
        row = rdata(f2, rower=rr)
    except IOError:
        row = rdata(f2 + '.gz', rower=rr)

    haspower = row.df[' Power (watts)'].mean() > 50

    nr_rows = len(row.df)
    if (plotnr in [1, 2, 4, 5, 8, 11, 9, 12]) and (nr_rows > 1200):
        bin = int(nr_rows / 1200.)
        df = row.df.groupby(lambda x: x / bin).mean()
        row.df = df
    nr_rows = len(row.df)
    if (plotnr == 1):
        fig1 = row.get_timeplot_erg(t)
    elif (plotnr == 2):
        fig1 = row.get_metersplot_erg(t)
    elif (plotnr == 3):
        fig1 = row.get_piechart(t)
    elif (plotnr == 4):
        if haspower:
            fig1 = row.get_timeplot_otwempower(t)
        else:
            fig1 = row.get_timeplot_otw(t)
    elif (plotnr == 5):
        if haspower:
            fig1 = row.get_metersplot_otwempower(t)
        else:
            fig1 = row.get_metersplot_otw(t)
    elif (plotnr == 6):
        fig1 = row.get_piechart(t)
    elif (plotnr == 7) or (plotnr == 10):
        fig1 = row.get_metersplot_erg2(t)
    elif (plotnr == 8) or (plotnr == 11):
        fig1 = row.get_timeplot_erg2(t)
    elif (plotnr == 9) or (plotnr == 12):
        fig1 = row.get_time_otwpower(t)
    elif (plotnr == 13) or (plotnr == 16):
        fig1 = row.get_power_piechart(t)

    canvas = FigureCanvas(fig1)

    #    plt.savefig('static/plots/'+imagename,format='png')
    canvas.print_figure('static/plots/' + imagename)
    # plt.imsave(fname='static/plots/'+imagename)
    plt.close(fig1)
    fig1.clf()
    gc.collect()

    return imagename

# Team related remote tasks


@app.task
def handle_sendemail_invite(email, name, code, teamname, manager,
                            debug=False,**kwargs):
    fullemail = name + ' <' + email + '>'
    subject = 'Invitation to join team ' + teamname
    message = 'Dear ' + name + ',\n\n'
    message += manager + ' is inviting you to join his team ' + teamname
    message += ' on rowsandall.com\n\n'
    message += 'By accepting the invite, you will have access to your'
    message += " team's workouts on rowsandall.com and your workouts will "
    message += " be visible to "
    message += "the members of the team.\n\n"
    message += 'If you already have an account on rowsandall.com, you can login to the site and you will find the invitation here on the Teams page:\n'
    message += ' https://rowsandall.com/rowers/me/teams \n\n'
    message += 'You can also click the direct link: \n'
    message += 'https://rowsandall.com/rowers/me/invitation/' + code + ' \n\n'
    message += 'If you are not yet registered to rowsandall.com, '
    message += 'you can register for free at https://rowsandall.com/rowers/register\n'
    message += 'After you set up your account, you can use the direct link: '
    message += 'https://rowsandall.com/rowers/me/invitation/' + code + ' \n\n'

    message += 'You can also manually accept your team membership with the code.\n'
    message += 'You will need to do this if you registered under a different email address than this one.\n'
    message += 'Code: ' + code + '\n'
    message += 'Link to manually accept your team membership: '
    message += 'https://rowsandall.com/rowers/me/invitation\n\n'
    message += "Best Regards, the Rowsandall Team"

    email = EmailMessage(subject, message,
                         'Rowsandall <info@rowsandall.com>',
                         [fullemail])

    res = email.send()

    return 1


@app.task
def handle_sendemailnewresponse(first_name, last_name,
                                email,
                                commenter_first_name,
                                commenter_last_name,
                                comment,
                                workoutname, workoutid, commentid,
                                debug=False,**kwargs):
    fullemail = first_name + ' ' + last_name + ' <' + email + '>'
    subject = 'New comment on workout ' + workoutname
    message = 'Dear ' + first_name + ',\n\n'
    message += commenter_first_name + ' ' + commenter_last_name
    message += ' has written a new comment on the workout '
    message += workoutname + '\n\n'
    message += comment
    message += '\n\n'
    message += 'You can read the comment here:\n'
    message += 'https://rowsandall.com/rowers/workout/' + \
        str(workoutid) + '/comment'
    message += '\n\n'
    message += 'You are receiving this email because you are subscribed '
    message += 'to comments on this workout. To unsubscribe, follow this link:\n'
    message += 'https://rowsandall.com/rowers/workout/' + \
        str(workoutid) + '/unsubscribe'

    email = EmailMessage(subject, message,
                         'Rowsandall <info@rowsandall.com>',
                         [fullemail])

    res = email.send()

    return 1


@app.task
def handle_sendemailnewcomment(first_name,
                               last_name,
                               email,
                               commenter_first_name,
                               commenter_last_name,
                               comment, workoutname,
                               workoutid,
                               debug=False,**kwargs):
    fullemail = first_name + ' ' + last_name + ' <' + email + '>'
    subject = 'New comment on workout ' + workoutname
    message = 'Dear ' + first_name + ',\n\n'
    message += commenter_first_name + ' ' + commenter_last_name
    message += ' has written a new comment on your workout '
    message += workoutname + '\n\n'
    message += comment
    message += '\n\n'
    message += 'You can read the comment here:\n'
    message += 'https://rowsandall.com/rowers/workout/' + \
        str(workoutid) + '/comment'

    email = EmailMessage(subject, message,
                         'Rowsandall <info@rowsandall.com>',
                         [fullemail])

    res = email.send()

    return 1


@app.task
def handle_sendemail_request(email, name, code, teamname, requestor, id,
                             debug=False,**kwargs):
    fullemail = name + ' <' + email + '>'
    subject = 'Request to join team ' + teamname
    message = 'Dear ' + name + ',\n\n'
    message += requestor + ' is requesting admission to your team ' + teamname
    message += ' on rowsandall.com\n\n'
    message += 'Click the direct link to accept: \n'
    message += 'https://rowsandall.com/rowers/me/request/' + code + ' \n\n'
    message += 'Click the following link to reject the request: \n'
    message += 'https://rowsandall.com/rowers/me/request/' + str(id) + ' \n\n'
    message += 'You can find all pending requests on your team management page:\n'
    message += 'https://rowsandall.com/rowers/me/teams\n\n'
    message += "Best Regards, the Rowsandall Team"

    email = EmailMessage(subject, message,
                         'Rowsandall <info@rowsandall.com>',
                         [fullemail])

    res = email.send()

    return 1


@app.task
def handle_sendemail_request_accept(email, name, teamname, managername,
                                    debug=False,**kwargs):
    fullemail = name + ' <' + email + '>'
    subject = 'Welcome to ' + teamname
    message = 'Dear ' + name + ',\n\n'
    message += managername
    message += ' has accepted your request to be part of the team '
    message += teamname
    message += '\n\n'
    message += "Best Regards, the Rowsandall Team"

    email = EmailMessage(subject, message,
                         'Rowsandall <info@rowsandall.com>',
                         [fullemail])

    res = email.send()

    return 1


@app.task
def handle_sendemail_request_reject(email, name, teamname, managername,
                                    debug=False,**kwargs):
    fullemail = name + ' <' + email + '>'
    subject = 'Your application to ' + teamname + ' was rejected'
    message = 'Dear ' + name + ',\n\n'
    message += 'Unfortunately, '
    message += managername
    message += ' has rejected your request to be part of the team '
    message += teamname
    message += '\n\n'
    message += "Best Regards, the Rowsandall Team"

    email = EmailMessage(subject, message,
                         'Rowsandall <info@rowsandall.com>',
                         [fullemail])

    res = email.send()

    return 1


@app.task
def handle_sendemail_member_dropped(email, name, teamname, managername,
                                    debug=False,**kwargs):
    fullemail = name + ' <' + email + '>'
    subject = 'You were removed from ' + teamname
    message = 'Dear ' + name + ',\n\n'
    message += 'Unfortunately, '
    message += managername
    message += ' has removed you from the team '
    message += teamname
    message += '\n\n'
    message += "Best Regards, the Rowsandall Team"

    email = EmailMessage(subject, message,
                         'Rowsandall <info@rowsandall.com>',
                         [fullemail])

    res = email.send()

    return 1


@app.task
def handle_sendemail_team_removed(email, name, teamname, managername,
                                  debug=False,**kwargs):
    fullemail = name + ' <' + email + '>'
    subject = 'Team ' + teamname + ' was deleted'
    message = 'Dear ' + name + ',\n\n'
    message += managername
    message += ' has decided to delete the team '
    message += teamname
    message += '\n\n'
    message += 'The ' + teamname + ' tag has been removed from all your '
    message += 'workouts on rowsandall.com.\n\n'
    message += "Best Regards, the Rowsandall Team"

    email = EmailMessage(subject, message,
                         'Rowsandall <info@rowsandall.com>',
                         [fullemail])

    res = email.send()

    return 1


@app.task
def handle_sendemail_invite_reject(email, name, teamname, managername,
                                   debug=False,**kwargs):
    fullemail = managername + ' <' + email + '>'
    subject = 'Your invitation to ' + name + ' was rejected'
    message = 'Dear ' + managername + ',\n\n'
    message += 'Unfortunately, '
    message += name
    message += ' has rejected your invitation to be part of the team '
    message += teamname
    message += '\n\n'
    message += "Best Regards, the Rowsandall Team"

    email = EmailMessage(subject, message,
                         'Rowsandall <info@rowsandall.com>',
                         [fullemail])

    res = email.send()

    return 1


@app.task
def handle_sendemail_invite_accept(email, name, teamname, managername,
                                   debug=False,**kwargs):
    fullemail = managername + ' <' + email + '>'
    subject = 'Your invitation to ' + name + ' was accepted'
    message = 'Dear ' + managername + ',\n\n'
    message += name + ' has accepted your invitation to be part of the team ' + teamname + '\n\n'
    message += "Best Regards, the Rowsandall Team"

    email = EmailMessage(subject, message,
                         'Rowsandall <info@rowsandall.com>',
                         [fullemail])

    res = email.send()

    return 1


# Another simple task for debugging purposes
def add2(x, y,debug=False,**kwargs):
    return x + y
