<?xml version="1.0" encoding="UTF-8"?>

<display id="display" window-flags="sticky, below">

  <meta name="Fuzzy clock"
      version="0.1"
      category="Time/Analog"
      author="Neil Cumpstey"
      description="Time the way it should be told."/>

  <control id="mytime" interface="ITime:7qktelp6tw29ve5p8q3lxn6bs-2"/>

#gives the box an appropriate length and height
#the box is always the length of the longest fuzzy time (12:25pm)
  <group id="deflengthbox"  x="0" y="0" anchor="nw" bg-color="#00000000">
    <label id="deflength" value="twenty five past twelve in the afternoon" color="#00000000"/>
  </group>
  <group id="defheightbox" relative-to="deflengthbox, y" x="0" y="0" anchor="ne" bg-color="#00000000">
    <label id="defheight" value="height" color="#00000000"/>
  </group>  

#box containing date and digital clock
  <group id="dateboxbg" bg-color="#0000bbbb" x="100%" y="0" width="100%" anchor="ne" visible="false">
    <group id="datebox" x="100%" anchor="ne">
      <label id="digitime" color="#ffaaaa" anchor="nw"/>
      <label id="date" color="#ffaaaa" relative-to="digitime, x" anchor="ne"/>
    </group>
  </group>

#main box containing fuzzy clock
  <group id="fuzzytimebox" bg-color="#0000bbbb" x="100%" y="100%" width="100%" anchor="se"
 		on-enter="Dsp.dateboxbg.visible=1" on-click="Dsp.display.window_flags=['sticky', 'above']"
 		on-leave="Dsp.dateboxbg.visible=0; Dsp.display.window_flags=['sticky', 'below']">
    <label id="fuzzytime" color="#ffaaaa"  x="100%" anchor="ne"/>
  </group>

  <prefs callback="mycallback">
    <enum label="corner" bind="cornerpref">
      <item label="top left" value="nw"/>
      <item label="bottom right" value="se"/>
      <item label="top right" value="ne"/>
    </enum>
    <font label="time font" bind="bigfont"/>
    <font label="date font" bind="smallfont"/>
    <color label="font color" bind="fontcolor"/>
    <color label="background" bind="bgcolor"/>
  </prefs>


  <script><![CDATA[

  # the initial default value
  cornerpref = "se"
  bigfont = "Sans, 13"
  smallfont = "Sans, 10"
  fontcolor = "#ffaaaaff"
  bgcolor = "#0000bbbb"

  def mycallback(key, value):
      if (key == "cornerpref"):
          if (value == "nw"):
            Dsp.dateboxbg.anchor = "se"
            Dsp.dateboxbg.y = Unit(100, '%')
            Dsp.datebox.anchor = "nw"
            Dsp.datebox.x = Unit(0, '%')
            Dsp.digitime.anchor = "nw"
            Dsp.date.relative_to = ['digitime', 'x']
            Dsp.date.x = Unit(30, PX)
            Dsp.date.anchor = "nw"
            Dsp.fuzzytimebox.anchor = "ne"
            Dsp.fuzzytimebox.y = Unit(0, '%')
            Dsp.fuzzytime.anchor = "nw"
            Dsp.fuzzytime.x = Unit(0, '%')
          if (value == "ne"):
            Dsp.dateboxbg.anchor = "se"
            Dsp.dateboxbg.y = Unit(100, '%')
            Dsp.datebox.anchor = "ne"
            Dsp.datebox.x = Unit(100, '%')
            Dsp.digitime.anchor = "ne"
            Dsp.date.relative_to = ['digitime', 'x']
            Dsp.date.x = Unit(30, PX)
            Dsp.date.anchor = "nw"
            Dsp.fuzzytimebox.anchor = "ne"
            Dsp.fuzzytimebox.y = Unit(0, '%')
            Dsp.fuzzytime.anchor = "ne"
            Dsp.fuzzytime.x = Unit(100, '%')
          if (value == "se"):
            Dsp.dateboxbg.anchor = "ne"
            Dsp.dateboxbg.y = Unit(0, '%')
            Dsp.datebox.anchor = "ne"
            Dsp.datebox.x = Unit(100, '%')
            Dsp.digitime.anchor = "ne"
            Dsp.date.relative_to = ['digitime', 'x']
            Dsp.date.x = Unit(30, PX)
            Dsp.date.anchor = "nw"
            Dsp.fuzzytimebox.anchor = "se"
            Dsp.fuzzytimebox.y = Unit(100, '%')
            Dsp.fuzzytime.anchor = "ne"
            Dsp.fuzzytime.x = Unit(100, '%')
            
      if(key == "bigfont"):
        Dsp.fuzzytime.font = value
        Dsp.deflength.font = value
        
      if(key == "smallfont"):
        Dsp.digitime.font = value
        Dsp.date.font = value
        Dsp.defheight.font = value
        
      if(key == "fontcolor"):
        Dsp.digitime.color = value
        Dsp.date.color = value
        Dsp.fuzzytime.color = value
        
      if(key == "bgcolor"):
        Dsp.dateboxbg.bg_color = value
        Dsp.fuzzytimebox.bg_color = value

  ]]></script>
  <script><![CDATA[

	def mytimehandler(new_value):
		h, m, s = new_value

		if h < 12:
			TimeOfDay = " in the morning"
 
		if h >= 12:
			TimeOfDay = " in the afternoon"

		if h >= 18:
			TimeOfDay = " in the evening"

		Hours = h
		FuzzyMinute = 5 * (round((m + (s / 60.0)) / 5, 0))
		if FuzzyMinute > 30:
			Hours = Hours + 1

		FuzzyMinute = FuzzyMinute % 60
		Hours = 1 + ((Hours - 1) % 12)

		if FuzzyMinute <= 30:
			TextTime = " past "

		if FuzzyMinute > 30:
			TextTime = " to "

		if FuzzyMinute == 0:
			TextTime = " o\'clock"

		TextMins = ['five', 'ten', 'quarter', 'twenty', 'twenty five', 'half', 'twenty five', 'twenty', 'quarter', 'ten', 'five', 'nothing']
		TextMin = TextMins[int(FuzzyMinute / 5 -1)]
		TextHours = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve']
		TextHour = TextHours[int(Hours - 1)]

		Dsp.digitime.value = "%2d:%2d.%2d" % (h, m, s)
		if FuzzyMinute == 0:
			Dsp.fuzzytime.value = TextHour + TextTime + TimeOfDay
		else:
			Dsp.fuzzytime.value = TextMin + TextTime + TextHour + TimeOfDay

	def mydatehandler(new_value):
		y, m, d = new_value
		months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
		monthname = months[m-1]
		Dsp.date.value = "%1d " % (d) + monthname + " %1d" % (y)

	mytime.bind("time", mytimehandler)
	mytime.bind("date", mydatehandler)

  ]]></script>

</display>