# clocks.py
# WTP Solution
# This program implemenets digital and analog clocks


from graphics import *
import math

class Clock(object):
    MAX_TIME = 3600*24

    def __init__(self, hour, minute, second, pos):
        self.hour = hour
        self.minute = minute
        self.second = second
        self.pos = pos

    def __str__(self):
        ''' special method returns the object as a string
            called automatically if the object is given as a
            parameter to the print command
        '''

        # convert the hour to  12 hour clock
        hour = self.hour % 12
        if hour == 0:
            hour = 12
            
        time_str = '%02d:%02d:%02d' % (hour, self.minute, self.second)

        # figure out  if it AM or PM
        if self.hour >= 12:
            time_str = time_str + ' PM'
        else:
            time_str = time_str + ' AM'

        return time_str

    def draw(self, win):
        self.draw_face(win)
        self.draw_time(win)
        
    def draw_face(self, win):
        return

    def draw_time(self, win):
        print self

    def get_time_in_sec(self):
        minutes = self.hour * 60 + self.minute
        seconds = minutes * 60 + self.second
        return seconds

    def split_time(self, time_in_sec):
        seconds = time_in_sec % 60
        minutes = time_in_sec / 60
        hour = minutes / 60
        minutes = minutes % 60

        return (hour, minutes, seconds)

    def update_time(self, inc):
        new_time = (self.get_time_in_sec() + inc) % self.MAX_TIME
        self.hour, self.minute, self.second = self.split_time(new_time)

    def update(self, win, inc):
         self.update_time(inc)
         print self

    def tick(self, win, inc):
         self.update(win, inc)
         win.after(inc * 1000, self.tick, win, inc)
