 (ns munchkin-toolbox.core
-  (:require-macros [cljs.core.async.macros :refer [go]])
+  (:require-macros [cljs.core.async.macros :refer [go alt!]])
   (:require [om.core :as om :include-macros true]
-            [cljs.core.async :refer [put! chan <! go]]
+            [cljs.core.async :refer [put! chan <!]]
             [sablono.core :as html :refer-macros [html]]))
 
 (enable-console-print!)

(defmethod change-level ::inc [op] (fn [l] (if (< l 10) (inc l) l)))
(defmethod change-level ::dec [op] (fn [l] (if (> l 1) (dec l) l)))

(defn player-info
  "Row with info about the player."
  [{:keys [name level] :as player} owner]
  (reify
    om/IInitState
    (init-state [_]
      {:level-changed (chan)})
    om/IWillMount
    (will-mount [_]
      (let [level-changed-c (om/get-state owner :level-changed)]
        (go (loop []
              (let [op (<! level-changed-c)]
                (om/transact! player :level (change-level op))
                (recur))))))
    om/IRenderState
    (render-state [_ state]
      (html
       [:div {:class "row"}
        (om/build (editable-label :name) player)
        (om/build level-counter
                  level
                  {:init-state (select-keys state [:level-changed])})
        [:hr]]))))

(defn players-list

  [n]
  "Creates a default description for n-th player."
  {:name (str "Player " n)
   :level 1
   :strength 0})

(def app-state
  (atom {:players (mapv default-player (range 1 4))}))
