#!/usr/bin/python
# -*- coding: utf-8 -*-

# -----------------------------------------------------------------------------
# Tux Droid - Pidgin InterFace
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
# -----------------------------------------------------------------------------
# $Id: $
# -----------------------------------------------------------------------------

"""
CHANGES
=======

2008-01-06 - version 0.1:
    - Initial version
2008-01-07 - version 0.2:
    - Added various randomization features, ergonomic reminders, and the snooze function makes sense now
2008-01-07 - version 0.2a:
    - Fixed a randomization bug
"""

__author__  = "Richard Crawford"
__appname__ = "Ergonomic Tux"
__version__ = "0.2a"
__date__    = "2008-01-06"
__license__ = "GPL"

# -----------------------------------------------
# Initalization of modules
# uses objects "tux" and "tss"
# -----------------------------------------------
import sys
import time
import random
sys.path.append('/opt/tuxdroid/api/python')
from tux import *

tux.daemon.connect()
tux.tts.connect()

reminders_on = 1;

go_away = ['go play outside','spend some time with your family','go eat some vegetables','go play with your cats','go call your mother']
ergo_reminders = ['stop slouching','keep your knees bent at a ninety degree angle','keep your wrists straight','do not sit so close to the monitor']

try:

    tux.cmd.leds_on()

    tux.tts.select_voice_us_male_tuxed()

    tux.tts.speak('I am an ergonomic penguin. Push my head button to start the clock.')

    while tux.status.head_bt() == 0:
        tux.sys.wait(0.1)

    while True:

        tux.cmd.wings_on_free(4,5)
        tux.cmd.mouth_open()
        tux.tts.speak('I am starting the clock now.')
        tux.cmd.mouth_close()
        tux.cmd.leds_off()

        snooze_count = 0

        end_time = time.time()+3000 # 50 minutes

        while time.time() < end_time:
            if tux.status.lwing_bt() == 1:
                if reminders_on == 1:
                    reminders_on = 0
                    tux.tts.speak('Very well. I will give you no more ergonomic tips.')
                else:
                    reminders_on = 1
                    tux.tts.speak('Very well. I will resume giving you ergonomic tips.')
                    
            if reminders_on == 1:
                if random.randrange(1,500) == 1:
                    ergo_hint_no = random.randrange(0,4)
                    tux.tts.speak(ergo_reminders[ergo_hint_no])

            minutes_left = int((end_time - time.time()) / 60)
            if tux.status.head_bt() == 1:
                tl = 'you have ' + `minutes_left` + ' minutes left'
                if random.randrange(1,6) == 1:
                    tux.tts.speak('Yes, I am awake. Stop hitting me.')
                tux.tts.speak(tl)
            if int(time.time()) % 5 == 0:
                if tux.status.lled() == 1:
                    tux.cmd.ledl_off()
                    tux.cmd.ledr_on()
                else:
                    tux.cmd.ledl_on()
                    tux.cmd.ledr_off()

        tux.cmd.wings_on_free(4,5)
        tux.cmd.spinl_on(8,5)
        tux.cmd.leds_on()
        tux.cmd.mouth_open()
        msg_no = random.randrange(0,4)
        go_away_msg = go_away[msg_no]
        tux.tts.speak ('you have spent enough time on the computer. ' + go_away_msg + '. You may return in ten minutes.')
        tux.cmd.mouth_close()

        break_end_time = time.time() + 600 # 10 minutes

        while time.time() < break_end_time:

            if tux.status.head_bt() == 1:
                snooze_count = snooze_count + 1
                tux.cmd.mouth_open()
                tux.tts.speak('Very well.  You may have five more minutes.')
                if snooze_count > 1:
                    tux.tts.speak('But I am serious. You must play outside.')
                tux.cmd.mouth_close()
                snooze_end = time.time()+300;
                while time.time() < snooze_end:
                    if tux.status.head_bt() == 1:
                        tux.cmd.mouth_open()
                        tux.tts.speak('No. You are already on snooze. I will tell you when snooze is done.')
                        tux.cmd.mouth_close()
                tux.cmd.mouth_open()
                tux.tts.speak('Your snooze period is over. Go away.')
                tux.cmd.mouth_close()
                break_end_time = break_end_time + 300 # 5 minutes

        tux.cmd.mouth_open()
        tux.tts.speak('You may now return to the computer. Have fun.')
        tux.cmd.mouth_close()

        while tux.status.head_bt() == 0:
            if random.randrange(0,100) == 1:
                tux.tts.speak('Are you back? Press my head button if you are back.')


finally:
    tux.destroy()
    sys.exit(0)

