# Appointment sublet file
# Created with sur-0.2
require 'action_view'

class Blah
  include ActionView::Helpers::DateHelper
end

configure :appointment do |s|
  s.interval = 30
  s.target_time = nil
  s.date_format = "%H:%M"
  s.red = Subtlext::Color.new("#ff0000")
end

helper do |s|
  def relative_time(to_time=nil)
    time = to_time || self.target_time
    if time && time = Time.parse(time)
      "#{Blah.new.time_ago_in_words(time)} from now"
    end
  end

  def set_time(time)
    self.target_time = time
  end

  def in_the_past?
    if self.target_time && (Time.parse(self.target_time) < Time.now)
      self.red + "<< LATE >>"
    end
  end

  def now
    Time.now.strftime(self.date_format)
  end

  def set_time_test
    to_time = "5pm"
    time = Time.parse(to_time)
    set_time(to_time)
    if time < Time.now
      self.target_time == nil
    else
      self.target_time == to_time
    end
  end
end

on :run do |s|
  s.data = s.in_the_past? || s.relative_time || s.now
end

on :data do |s, time|
  time = nil if time =~ /clear/
  s.set_time(time)
  s.data = s.in_the_past? || s.relative_time || s.now
end
