from datetime import datetime 
from math import cos, sin, pi

svgclock = """<?xml version="1.0"?>
<svg version="1.1" width="200" height="200" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 2.02 2.02">
	<defs>
		<style type="text/css">
			path {
				stroke: black;
				stroke-width: 0.02;
				fill: none;
			}
			line {
				stroke-linecap: round;
			}
			
			#seconds {
				stroke: red;
				stroke-width: 0.02;
			}
			#minutes {
				stroke: green;
				stroke-width: 0.05;
			}
			#hours {
				stroke: blue;
				stroke-width: 0.08;
			}
		</style>
	</defs>
			
	<g transform="rotate(-90) translate(-1,1)">
		<path d="
			M 1 0 A 1 1 0 1 1 -1 0 A 1 1 0 1 1 1 0
			M  1.000  0.000 L  0.900  0.000
			M  0.866  0.500 L  0.779  0.450
			M  0.500  0.866 L  0.450  0.779
			M  0.000  1.000 L  0.000  0.900
			M -0.500  0.866 L -0.450  0.779
			M -0.866  0.500 L -0.779  0.450
			M -1.000  0.000 L -0.900  0.000
			M -0.866 -0.500 L -0.779 -0.450
			M -0.500 -0.866 L -0.450 -0.779
			M  0.000 -1.000 L  0.000 -0.900
			M  0.500 -0.866 L  0.450 -0.779
			M  0.866 -0.500 L  0.779 -0.450
		"/>

		<line id="hours"   x1="0" y1="0" x2="%f" y2="%f"/>
		<line id="minutes" x1="0" y1="0" x2="%f" y2="%f"/>		
		<line id="seconds" x1="0" y1="0" x2="%f" y2="%f"/>
	</g>
</svg>
"""

class SvgClockTrigger(object):
	type = "image/svg+xml"
	def __init__(self, cb):
		self.cb = cb
	
	def check(self):
		self.cb(self.snapshot())
	
	def snapshot(self):
		d = datetime.now()
		s = d.second * pi / 30;
		m = d.minute * pi / 30 + s / 60;
		h = d.hour * pi / 6 + m / 12;
		angles = [  
					cos(h), sin(h),
					cos(m), sin(m),
					cos(s), sin(s),
				 ]

		coords = map(lambda x: x * 0.90, angles)
		
		return svgclock % tuple(coords)
