# Description: biorythm calculator
# Author: Wijnand 'tehmaze' Modderman
# Website: http://tehmaze.com/
# License: BSD

from gozerbot.commands import cmnds
from gozerbot.db import db
from gozerbot.examples import examples
from gozerbot.plughelp import plughelp
from gozerbot.users import users
import datetime
import math

plughelp.add('bio', 'biorythm calculator')

def bd_days(name):
    result = db.execute(""" SELECT birthday FROM birthday WHERE \
name = %s """, name)
    try:
        birthday = result[0][0]
    except TypeError:
        return 0
    nums = map(int, birthday.split('-'))
    date = datetime.datetime(nums[2], nums[1], nums[0])
    now  = datetime.datetime.now()
    diff = now - date
    return diff.days

def bio_str(bio):
    bio = int(round((bio+1.00)*3.5)) - 1
    return ['critical', 'bad', 'not so good', 'average', 'above average', 
        'good', 'very good'][bio]

def handle_bio(bot, ievent):
    user = ievent.args and ievent.args[0].lower() or users.getname(ievent.userhost)
    if not user or not users.exist(user):
        ievent.reply('user not found')
        return
    days = bd_days(user)
    if not days:
        ievent.reply('no birthday set (use setbd) for user %s' % user)
        return
    biop = bio_str(math.sin(2*math.pi*days / 23))
    bioe = bio_str(math.sin(2*math.pi*days / 28))
    bioi = bio_str(math.sin(2*math.pi*days / 33))
    ievent.reply('physical: %s, emotional: %s, intellectual: %s' % (biop, bioe, bioi))

cmnds.add('bio', handle_bio, 'USER')
examples.add('bio', 'calculates your biorythm, or for another user', 'bio owner')

