 
 (defn movement-item []
   (let [editing (atom false)]
-    (fn [{:keys [id done title]}]
-      [:li {:class (str (if done "completed ")
-                        (if @editing "editing"))}
-       [:div.view
-        [:label {:on-double-click #(reset! editing true)} title]
-        [:button.button.button-primary {:on-click #(refresh-movement! id)}]
-        [:button.destroy {:on-click #(delete-movement! id)}]]
+    (fn [{:keys [id title category-ref]}]
+      [:div.row {:class (str (if @editing "editing"))}
+       [:label.three.columns {:on-double-click #(reset! editing true)} title]
+       [:button.one.column.button.button-primary
+        {:on-click #(refresh! id (:category (get (:categories @session) category-ref)))}]
+       [:button.one.column.destroy {:on-click #(delete! :movements id)}]
        (when @editing
-         [movement-edit {:class "edit" :title title
-                         :on-save #(update-movement! id %)
+         [movement-edit {:class   "edit" :title title
+                         :on-save #(update! :movements id %)
                          :on-stop #(reset! editing false)}])])))
 
 (defn category-item []
-  (fn [{:keys [id category title movements]}]
+  (fn [{:keys [id category title]} movements]
     [:div
-     [:label title]
-     (let [items (vals @movements)]
-       [:p (str items)]
-       (when (-> items count pos?)
-         [:div
-          [:section
-           [:ul
-            [:li "!!"]
-            #_(for [movement items]
-              ^{:key (:id movement)} [movement-item movement])]]])
-       [movement-input {:id          "new-movement"
-                        :placeholder "Add movement.."
-                        :on-save     #(add-movement! %1 id)}])]))
-
-(print @template)
+     [:h3 title]
+     (when (-> movements count pos?)
+       (for [m movements]
+         ^{:key (:id m)} [movement-item m]))
+     [movement-input {:id          "new-movement"
+                      :placeholder "Add movement.."
+                      :on-save     #(add-movement! %1 id)}]]))
 
 (defn home-page []
   [:div

(defn prep-name [kw]
  (str/replace (str/capitalize (name kw)) "-" " "))

(defn refresh-button [part]
  [:div.one.column
   {:type     "button"
    :class    "button"
    :on-click #(generate! (:name part) (:category part) 1)}
   "#"])

(defn list-movements [part]
  (for [movement (:movements part)]
    [:div.row
     [:div.six.columns {:draggable true} (prep-name movement)]
     (refresh-button part)]))

(defn update! [button]
  (do
    (reset! ttemplate [])
    (reset! buttons default-buttons)
    (button-selected! button)))

(defonce movements (atom (sorted-map)))
(defonce template (atom (sorted-map)))
(defonce m-counter (atom 0))
(defonce c-counter (atom 0))

(defn add-movement! [text category-id]
  (let [category (get @template category-id)
        id (swap! (:m-counter category) inc)]
    (swap! (:movements category) assoc id {:id id :title text})))

(defn add-category! [title category]
  (let [id (swap! c-counter inc)]
    (swap! template assoc id {:id id :title title
                              :category category :m-counter (atom 0)
                              :movements (atom (sorted-map))})))

(defn add-mmovement! [text]
  (let [id (swap! m-counter inc)]
    (swap! movements assoc id {:id id :title text :done false})))

(defn update-category! [id title] (swap! template assoc-in [id :title] title))
(defn delete-category! [id] (swap! template dissoc id))

(defn update-movement! [id title] (swap! movements assoc-in [id :title] title))
(defn delete-movement! [id] (swap! movements dissoc id))

(defn refresh-movement! [id] (update-movement! id (prep-name (first (take 1 (shuffle strength))))))

(defn mmap [m f a] (->> m (f a) (into (empty m))))

(defn movement-input [{:keys [title on-save on-stop]}]
  (let [val (atom title)

       {:type     "button"
        :class    (:ritual @buttons)
        :on-click #(do
                    (reset-session!)
                    (set-button-selected! :ritual)
                    (add-category! "Warmup" warmup)
                    (dotimes [n 1] (add-movement! (prep-name (nth (take 1 (shuffle warmup)) n)) 1))
                    (add-category! "Mobility" mobility)
                    (dotimes [n 5] (add-movement! (prep-name (nth (take 5 (shuffle mobility)) n)) 2))
                    (add-category! "Hanging" hanging)
                    (dotimes [n 1] (add-movement! (prep-name (nth (take 1 (shuffle hanging)) n)) 3))
                    (add-category! "Equilibre" equilibre)
                    (dotimes [n 1] (add-movement! (prep-name (nth (take 1 (shuffle equilibre)) n)) 4))
                    (add-category! "Strength" strength)
                    (dotimes [n 1] (add-movement! (prep-name (nth (take 1 (shuffle strength)) n)) 5)))}
       "Morning ritual"]
      [:div.three.columns
       {:type     "button"
        :class    (:strength @buttons)
        :on-click #(do
                    (reset-session!)
                    (set-button-selected! :strength)
                    (add-category! "Warmup" warmup)
                    (dotimes [n 1] (add-movement! (prep-name (nth (take 1 (shuffle warmup)) n)) 1))
                    (add-category! "Mobility" mobility)
                    (dotimes [n 6] (add-movement! (prep-name (nth (take 6 (shuffle mobility)) n)) 2))
                    (add-category! "Strength" strength)
                    (dotimes [n 4] (add-movement! (prep-name (nth (take 4 (shuffle strength)) n)) 3)))
        }
       "Strength"]
      [:div.three.columns
       {:type     "button"
        :class    (:mobility @buttons)
        :on-click #(do
                    (reset-session!)
                    (set-button-selected! :mobility)
                    (add-category! "Warmup" warmup)
                    (dotimes [n 1] (add-movement! (prep-name (nth (take 1 (shuffle warmup)) n)) 1))
                    (add-category! "Mobility" mobility)
                    (dotimes [n 6] (add-movement! (prep-name (nth (take 6 (shuffle mobility)) n)) 2))
                    (add-category! "Prehab" mobility)
                    (dotimes [n 4] (add-movement! (prep-name (nth (take 4 (shuffle mobility)) n)) 3)))}
       "Mobility/Prehab"]
      [:div.three.columns
       {:type     "button"
        :class    (:locomotion @buttons)
        :on-click #(do
                    (reset-session!)
                    (set-button-selected! :locomotion)
                    (add-category! "Warmup" warmup)
                    (dotimes [n 1] (add-movement! (prep-name (nth (take 1 (shuffle warmup)) n)) 1))
                    (add-category! "Mobility" mobility)
                    (dotimes [n 6] (add-movement! (prep-name (nth (take 6 (shuffle mobility)) n)) 2))
                    (add-category! "Locomotion" locomotion)
                    (dotimes [n 6] (add-movement! (prep-name (nth (take 6 (shuffle locomotion)) n)) 3)))}
       "Locomotion"]]
     [:div.row
      [:div.three.columns
       {:type     "button"
        :class    (:bas @buttons)
        :on-click #(do
                    (reset-session!)
                    (set-button-selected! :bas)
                    (add-category! "Warmup" warmup)
                    (dotimes [n 1] (add-movement! (prep-name (nth (take 1 (shuffle warmup)) n)) 1))
                    (add-category! "Mobility" mobility)
                    (dotimes [n 6] (add-movement! (prep-name (nth (take 6 (shuffle mobility)) n)) 2))
                    (add-category! "Bent Arm Strength" bas)
                    (dotimes [n 5] (add-movement! (prep-name (nth (take 5 (shuffle bas)) n)) 3)))}
       "BAS"]
      [:div.three.columns
       {:type     "button"
        :class    (:sass @buttons)
        :on-click #(do
                    (reset-session!)
                    (set-button-selected! :sass)
                    (add-category! "Warmup" warmup)
                    (dotimes [n 1] (add-movement! (prep-name (nth (take 1 (shuffle warmup)) n)) 1))
                    (add-category! "Mobility" mobility)
                    (dotimes [n 6] (add-movement! (prep-name (nth (take 6 (shuffle mobility)) n)) 2))
                    (add-category! "Straight Arm Scapular Strength" sass)
                    (dotimes [n 4] (add-movement! (prep-name (nth (take 4 (shuffle sass)) n)) 3)))}
       "SASS"]
      [:div.three.columns
       {:type     "button"
        :class    (:leg @buttons)
        :on-click #(do
                    (reset-session!)
                    (set-button-selected! :leg)
                    (add-category! "Warmup" warmup)
                    (dotimes [n 1] (add-movement! (prep-name (nth (take 1 (shuffle warmup)) n)) 1))
                    (add-category! "Mobility" mobility)
                    (dotimes [n 6] (add-movement! (prep-name (nth (take 6 (shuffle mobility)) n)) 2))
                    (add-category! "Leg Strength" leg-strength)
                    (dotimes [n 3] (add-movement! (prep-name (nth (take 3 (shuffle leg-strength)) n)) 3))
                    (add-category! "Auxiliary" auxiliary)
                    (dotimes [n 2] (add-movement! (prep-name (nth (take 2 (shuffle auxiliary)) n)) 4)))}
       "Leg strength"]
      [:div.three.columns
       {:type     "button"
        :class    (:movnat @buttons)
        :on-click #(do
                    (reset-session!)
                    (set-button-selected! :movnat)
                    (add-category! "Warmup Mobility (3 rounds)" movnat-warmup)
                    (dotimes [n 3] (add-movement! (prep-name (nth (take 3 (shuffle movnat-warmup)) n)) 1))
                    (add-category! "Skill (30 reps)" movnat)
                    (dotimes [n 1] (add-movement! (prep-name (nth (take 1 (shuffle movnat)) n)) 2))
                    (add-category! "Combo (4 rounds)" movnat)
                    (dotimes [n 4] (add-movement! (prep-name (nth (take 4 (shuffle movnat)) n)) 3)))}
       "MovNat"]]
     [:div.row
      [:div.three.columns
       {:type     "button"
        :class    (:maya @buttons)
        :on-click #(do
                    (reset-session!)
                    (set-button-selected! :maya)
                    (add-category! "Oppvarming/Bevegelighet (2 runder rolig)" m-oppvarming)
                    (dotimes [n 3] (add-movement! (prep-name (nth (take 3 (shuffle m-oppvarming)) n)) 1))
                    (add-category! "Styrke/Ferdighet (30 reps)" m-styrke)
                    (dotimes [n 1] (add-movement! (prep-name (nth (take 1 (shuffle m-styrke)) n)) 2))
                    (add-category! "Kombinasjon (3 runder hurtig)" m-kombinasjon)
                    (dotimes [n 4] (add-movement! (prep-name (nth (take 4 (shuffle m-kombinasjon)) n)) 3)))}
       "Maya"]]]]
   [:div.section.movements
    [:div.container
     (let [session @session
           categories (vals (:categories session))
           movements (vals (:movements session))]
       (when (-> categories count pos?)
         [:div.row
          (for [c categories]
            ^{:key (:id c)} [category-item
                             c
                             (filter
                               #(= (:id c) (:category-ref %))
                               movements)])]))]]
   [:div.footer
    [:div.container
     [:em "If you have suggestions for a new session template, some sorely missing movements
