 (ns meetup
   (:import (java.util Calendar)))
 
-(def day-offsets {:sunday    1
-                  :monday    2
-                  :tuesday   3
-                  :wednesday 4
-                  :thursday  5
-                  :friday    6
-                  :saturday  7})
+(def day-offsets
+  {:sunday    1
+   :monday    2
+   :tuesday   3
+   :wednesday 4
+   :thursday  5
+   :friday    6
+   :saturday  7})
 
-(def ordinals {:first  1
-               :second 2
-               :third  3
-               :fourth 4})
-
-(defn- abs [n]
-  (cond
-    (neg? n) (* -1 n)
-    :else    n))
+(def days-of-month
+  {:first 1
+   :second 8
+   :third 15
+   :fourth 22
+   :teenth 13})
 
 (defn- ^Calendar to-date
   [year month day]


(defn- days-to-move
  [weekday offset-and-direction]
  (let [offset    (abs offset-and-direction)
        direction (/ offset-and-direction offset)]
    (* (mod (* -1 direction (- weekday offset)) 7) direction)))


  (let [date (to-date year month 1)]
    (.getActualMaximum date Calendar/DAY_OF_MONTH)))

(defn- offset-for
  [schedule day-of-week]
  (if (= :last schedule)
    (* -1 day-of-week)
    day-of-week))

(defn meetup
  [month year day-name schedule]
  (let [day-of-month (if (= :last schedule)
                       (last-day-of-month year month)
                       (get days-of-month schedule))
        day-of-week (get day-offsets day-name)
        offset (offset-for schedule day-of-week)]
    (date-parts (offset-date year month day-of-month offset))))
