import time
import datetime
import random
import pyttsx
from push_ups import get_push_up_str, get_plank_str, get_side_plank_str

try:
    import winsound
except:
    pass

filename = 'push_ups.txt'
engine = pyttsx.init()
rate = engine.getProperty('rate')
engine.setProperty('rate', rate-50)
engine.say('hello')
engine.runAndWait()

def start_exercies(exercies_str):
    engine.say("O boy O boy!")
    engine.say(exercies_str)
    engine.say('Start In')
    for i in range(3):
        engine.say(str(3 - i))
        time.sleep(0.2)
    engine.say('GO')
    engine.runAndWait()


def pushup_counter(n):
    time.sleep(0.5)
    song_range = [0, 7]
    songs = ['Is it too late now to say sorry?',
             'Shawty had them Apple Bottom jeans, Boots with the fur, the whole club was looking at her',
             'You\'re just to good to be true, can\'t take my eyes off of you',
             'Beach body, beach body, beach body, beach body, beach body!',
             'The roof, the roof, the roof is on fire',
             'Imagination, life is your creation. Come on barbie, lets go party!',
             'My milkshakes bring all the boys to the yard, and they\'re like it\'s better than yours',
             'Everything is awesome, everything is cool when you\'re part of a team']
    for i in range(n):
        string = str(n - i) + ' more'
        engine.say(string)
        if i % 5 == 0:
            engine.say(songs[int(random.randint(song_range[0], song_range[1]))])
        engine.runAndWait()
        time.sleep(2)

def second_counter(n, side =1):
    time.sleep(1)
    for l in range(side):
        start_time = datetime.datetime.now()
        time_left = n -(datetime.datetime.now() -start_time).seconds
        while time_left > 0:
            if time_left % 5 == 0:
                string = str(time_left) + ' seconds to go'
                engine.say(string)
                time.sleep(1)
            time_left = n -(datetime.datetime.now() -start_time).seconds
            engine.runAndWait()
        if side > 1:
            engine.say("Change sides")
            side -= 1
            time.sleep(5)
        engine.runAndWait()

try:
    exercises = {}
    exercises["pushups"] = get_push_up_str
    exercises["plank"] = get_plank_str
    exercises["sideplank"] = get_side_plank_str
    exercises_list = exercises.keys()
    while True:
        # Added time range for new push_up_number generator
        time_range = [5, 30]
        minutes = int(random.randint(time_range[0],time_range[1]))
        exercies = exercises_list[random.randrange(0,len(exercises_list))]
        time.sleep(minutes*60)
        exercies_string, amount = exercises[exercies](minutes, time_range)
        date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        print date +"\t" +exercies_string
        # beep sounds
        start_exercies(exercies_string)
        if exercies == "pushups":
            pushup_counter(amount)
        else:
            if exercies == "sideplank":
                second_counter(amount, 2)
            else:
                second_counter(amount)
        # write to file
        txtfile = open(filename, 'a')
        txtfile.write(str(date) + '\t' + exercies_string + '\n')
        txtfile.close()
except KeyboardInterrupt:
    pass
