 (defn enqueue-track [queue track]
   (update-in queue [:tracks] conj track))
 
-(defn search-results []
-  (om/ref-cursor (:results (:search (om/root-cursor app-state)))))
-
-(defn player-queue []
-  (om/ref-cursor (:player-queue (om/root-cursor app-state))))
-
-(defn player-state []
-  (om/ref-cursor (:player (om/root-cursor app-state))))
-
-(defn player-pause []
-  (om/transact! (player-state) #(update-in % [:playing] not)))
-
-(defn current-track []
-  (nth (player-queue) (:track-number (player-state)) nil))
-
-(defn player-next []
-  ;; how do we make this fail if there are no more tracks?
-  (om/transact! (player-state) #(update-in % [:track-number] inc)))
-
-(defn player-playing [e]
-  (let [player (.-target e)
-        offset (.-currentTime player)]
-    (om/transact! (player-state)
-                  #(update-in % [:track-offset]
-                              (fn [time] offset)))))
-
-(defn player-prev []
-  (let [dec0 #(max (dec %) 0)]
-    (om/transact! (player-state) #(update-in % [:track-number] dec0))))
-
-
-(defn player-el []
-  (aget (.getElementsByTagName js/document "audio") 0))
-
-(defn enqueue-track [track]
-  (om/transact! (player-queue) #(conj % track)))
-
-(defn dequeue-track [index]
-  (om/transact! (player-queue)
-                (fn [v] (vec (concat (subvec v 0 index)
-                                     (subvec v (inc index)))))))
 (deftest enqueue-test
   (let [q {:tracks [:a :b :c :d] :next-track 2}]
     (is (= (:tracks (enqueue-track q :z)) [:a :b :c :d :z]))))
 
-(defn dequeue-all []
-  (om/transact! (player-queue) (fn [v] [])))
 
 (defn mmss [seconds]
   (let [m (quot seconds 60)

    om/IRender
    (render [this]
      (let [search-chan (om/get-shared owner :search-channel)
            artist (dom/span
                    #js {:className "artist"
                         :onClick #(put! search-chan

           (om/build queue-view state)]
          [:div {:className "queue tracks" } " "])
        (om/build transport-buttons-view state)
        (om/build audio-el (:audio-el (:player state)))
        ]))))

(defmulti dispatch-command (fn  [command & args] command))

(defmethod dispatch-command :enqueue [_ track]
  (swap! app-state #(update-in % [:player :queue] enqueue-track track)))

(defmethod dispatch-command :toggle-pause [_]
  (swap! app-state #(update-in % [:player :want-play] not)))

(defmethod dispatch-command :next-track [_]
  (swap! app-state #(update-in % [:player :queue] advance-queue)))

;; still to add: [dequeue previous-track delete-queue]

(defn command-loop []
  (let [ch (chan)]
    (go
      (loop []
        (let [command (<! ch)]
          (apply dispatch-command command)
          (recur))))
    ch))

(defn init []
  (add-watch app-state :transport sync-transport)
  (let [el (. js/document (getElementById "om-app"))
        command-chan (command-loop)
        search (chan)]
    (om/root app-view app-state
             {:target el
              :shared {:command-channel command-chan
                       :search-channel search}})))

(.addEventListener js/window "load" init)
(cljs.test/run-tests)
