 (defn hero-classes [app]
   (let [
         left-class (when (> 0 (get-in app [:hero :dx]))
-          "left "
-          )]
+                     "left "
+                     )]
     (if (moving? (app :hero))
       (str "hero " left-class "step-" (step-class app))
       (str "hero " left-class)
       )
     ))
 
-(defn move-horizontally [hero]
+(defn move-left [hero]
   (let [left-position (get-in hero [:position :left])]
     (if (moving? hero)
-      (update-in hero [:position :left] (fn [old-left] (+ old-left (hero :dx))))
+      (if (> 0 left-position)
+        (update-in hero [:position :left] (fn [_] 0))
+        (update-in hero [:position :left] (fn [old-left] (+ old-left (hero :dx)))))
       hero
-      )))
+      )
+    ))
+
+(defn move-right [hero]
+  (let [left-position (get-in hero [:position :left])]
+    (if (moving? hero)
+      (if (> left-position 1340)
+        (update-in hero [:position :left] (fn [_] 1340))
+        (update-in hero [:position :left] (fn [old-left] (+ old-left (hero :dx)))))
+      hero
+      )
+    ))
 
 (defn lava-damage [hero]
   hero)

  (.log js/console (clj->js obj)))

(def grid-content [
           [[0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0]]
           [[0] [0] [0] [0] [1] [0] [0] [0] [0] [0] [0] [0] [1] [0] [0] [0] [0] [0] [0] [0]]
           [[0] [0] [0] [0] [1] [1] [1] [1] [1] [0] [1] [1] [1] [0] [0] [0] [0] [0] [0] [0]]
           [[0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0]]
           [[0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0]]
           [[0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0]]
           [[0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0]]
           [[0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0] [0]]
           [[9] [1] [4] [4] [4] [4] [4] [4] [4] [4] [5] [5] [5] [5] [5] [5] [5] [5] [1] [0]]
           [[1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1]]
           ])

(defn indices [pred coll]
  (keep-indexed #(when (pred %2) %1) coll))

(defn contains-hero [row]
  (some #{9} row)
  )

(defn is-hero [cell]
  (= cell [9]))

(defn hero-start-position [grid-content]
  (let [row-index (first (indices contains-hero (reverse grid-content)))
        row (get (vec (reverse grid-content)) row-index)
        col (first (indices is-hero row))]
    (println row)
    (println col)
    {:bottom (* row-index 70) :left (* col 70)}
    )
  )

        (update-in newer-hero [:dy] (fn [_] 0))
        )
      new-hero
      )
    ))

(defn logger [key hero]
  ;;(console-log {key (get-in hero [:position :left])})
  hero
  )

(defn home? [original-hero hero]
  (if (on-block? (get-in original-hero [:position]) "4")
    (merge original-hero {:game-won true})
    hero
    )
  )

(defn game-over? [original-hero hero]
  (if (> 0 (get-in hero [:position :bottom]))
    (merge hero {:life 0})
    hero
    )
  )

(defn hero [app owner]
  (reify
    om/IWillMount
    (will-mount [_]
      (console-log "mounting...")
      (go-loop []
        (<! (timeout 30))

        (let [original-hero (get-in @app [:hero])
              new-hero (-> original-hero
                           grav
                           ((partial vertical-block original-hero))
                           move-left
                           move-right
                           bounce
                           ((partial home? original-hero))
                           ((partial game-over? original-hero))
                           )]

          (om/transact! app [:hero] (fn [hero] (merge hero new-hero)))
          )
        (recur)
        )
      )
    om/IRenderState
