#
# Copyright 2010 Ruediger Gad <r.c.g@gmx.de>
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#

import cairo
from datetime import datetime
import gtk
import math

import clock

def get_name():
    return "Metric Clock"

class MetricClock(clock.Clock):
    seconds_by_day = 24*60*60
    def get_time_format(self):
        if self.custom_format:
           return self.custom_format
        if self.twelve_hour_mode:
            if self.show_seconds:
                return "d:dd:dd"
            else:
                return "d:dd"        
        if self.show_seconds:
            return "dd:dd.d"       
        return "dd:dd"

    def draw_clock(self):
        seconds = self.time.hour * 3600 + self.time.minute * 60 + self.time.second
        if seconds >= self.seconds_by_day:
            seconds = self.seconds_by_day-1
        metric_time = float(seconds)/self.seconds_by_day
        text = ""
        for c in self.get_time_format():
            if c == "d":
                metric_time = metric_time * 10
                text = text + ("0123456789"[int(metric_time)])
                metric_time = metric_time-int(metric_time)
            else:
                text = text + c
        self.set_show_seconds(self.get_time_format().count('d') >= 4)
        self.draw_text(text)

    def resize(self):
        text = self.get_time_format().replace('d','2')

        self.text_width = self.calculate_text_width(text)
        self.drawing_area.set_size_request(int(self.text_width), 36)

