# -*- coding: iso-8859-1 -*-
############################################################################
#    Copyright (C) 2005 by Christoph Neuroth <delmonico@gmx.net>           #
#                                                                          #
#    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 of the License, 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.             #
############################################################################

"""
Python class to show a "fuzzy clock".

The class has been ported from PHP code by
Henrique Recidive <henrique at recidive.com> which was
in turn based on the Fuzzy Clock Applet of Frerich Raabe (KDE).
So most of the credit goes to this guys, thanks :-)

TODO: gettext support
"""

import time

class FuzzyClock:
    def __init__(self):
        self.__hour = 0
        self.__minute = 0
        self.__dayOfWeek = 0

        self.__hourNames = [ 'one', 'two', 'three', 'four', 'five', 'six',
                             'seven', 'eight', 'nine', 'ten', 'eleven',
                             'twelve']

        """ Strings to use for the output. %0 will be replaced with the preceding
            hour (e.g. "x PAST %0"), %1 with the coming hour (e.g. "x TO %1). """
        self.__normalFuzzy = [ '%0 o\'clock', 'five past %0', 'ten past %0',
                               'quarter past %0', 'twenty past %0',
                               'twenty five past %0', 'half past %0',
                               'twenty five to %1', 'twenty to %1',
                               'quarter to %1', 'ten to %1', 'five to %1',
                               '%1 o\'clock' ]

        """ A "singular-form". It is used when talking about hour 0 - I don't
            understand this, i just ported it ;-) Maybe some languages need
            this (not English and German though)? If you know, please drop me
            a note :-)
        """
        self.__normalFuzzyOne = [ '%0 o\'clock', 'five past %0',
                                  'ten past %0', 'quarter past %0',
                                  'twenty past %0', 'twenty five past %0',
                                  'half past %0', 'twenty five to %1',
                                  'twenty to %1', 'quarter to %1',
                                  'ten to %1', 'five to %1', '%1 o\'clock' ]

        self.__dayTime = [ 'Night', 'Early morning', 'Morning', 'Almost noon',
                           'Noon', 'Afternoon', 'Evening', 'Late evening' ]

        self.__fuzzyWeek = [ 'Start of week', 'Middle of week', 'End of week',
                             'Weekend!' ]

        self.setCurrent()

    def setHour(self,hour):
        self.__hour = int(hour)

    def setMinute(self,minute):
        self.__minute=int(minute)

    def setDayOfWeek(self,day):
        self.__dayOfWeek=int(day)

    def setTime(self,time):
        timeArray = time.split(":")
        self.setHour(timeArray[0])
        self.setMinute(timeArray[1])

    def setCurrent(self):
        hour=time.strftime("%H")
        minute=time.strftime("%M")
        day=time.strftime("%w")

        self.setTime(hour+":"+minute)
        self.setDayOfWeek(day)

    def getFuzzyTime(self, fuzzyness = 1):
        sector = 0
        realHour = 0

        if fuzzyness == 1 or fuzzyness == 2:
            if fuzzyness == 1:
                if self.__minute >2:
                    sector = (self.__minute - 3) / 5 +1
            else:
                if self.__minute > 6:
                    sector = ((self.__minute - 7) / 15 + 1) * 3

            newTimeStr = self.__normalFuzzy[sector]
            #%0 or %1?
            deltaHour = int(newTimeStr[newTimeStr.find("%")+1])

            if (self.__hour + deltaHour) % 12 > 0:
                realHour = (self.__hour + deltaHour) % 12 - 1
            else:
                realHour = 12 - ((self.__hour + deltaHour) % 12 + 1)

            if realHour == 0:
                newTimeStr = self.__normalFuzzyOne[sector]

            newTimeStr = newTimeStr.replace("%"+str(deltaHour),
                                            self.__hourNames[realHour])


        elif fuzzyness == 3:
            newTimeStr = self.__dayTime[self.__hour / 3]

        else:
            dayOfWeek = self.__dayOfWeek
            if dayOfWeek == 1:
                newTimeStr = self.__fuzzyWeek[0]
            elif dayOfWeek >= 2 and dayOfWeek <= 4:
                newTimeStr = self.__fuzzyWeek[1]
            elif dayOfWeek == 5:
                newTimeStr = self.__fuzzyWeek[2]
            else:
                newTimeStr = self.__fuzzyWeek[3]

        return newTimeStr
