   (:require-macros [cljs.core.async.macros :refer [go]]))
 
 (defonce app-state (atom {:text "Hello Chestnut!"}))
+
+(defn log [& args]
+  (.apply (.-log js/console) js/console (clj->js args)))
+
+(defn stringify [msg] (.stringify js/JSON (clj->js msg)))
 
 (defn main []
   (om/root

    app-state
    {:target (. js/document (getElementById "app"))}))

;; (set! (.-title js/document) "uf")
;; (.log js/console (.-title js/document))

(defn on-message [event]
  (.log js/console event))

(defn s4 []
  (let [r (+ 1 (Math/random))
        r2 (* r 0x10000)

(defn guid "Generates a random GUID" []
  (str (s4) (s4) "-" (s4) "-" (s4) "-" (s4) "-" (s4) (s4) (s4)))

(declare send on-message)

(def socket (atom nil))
(def connected? (atom false))
(def identified? (atom false))
(def queue (atom []))
(def sent-messages (atom {}))

(defn on-open
  [socket event]
  (reset! connected? true)
  (send socket "identify" {}))

(defn init []
  (let [url "ws://localhost:9000/api/ws"
        s (js/WebSocket. url)]
    (set! (.-onopen s ) (partial on-open s))
    (set! (.-onmessage s ) #(on-message (.parse js/JSON (.-data %))))

    (reset! socket s)))

(defn send [socket action data]
  (let [params {:api_key "foo"
                :user (guid)
                :action action
                :data data
                :id (guid)
                :timestamp (.getTime (js/Date.))}]
    (if @connected?
      (do
        (swap! sent-messages assoc (:id params) params)
        (.send socket (stringify params)))
      (swap! queue conj params))))

(defn flush-queue []
  (log "flushing queue"))

(defn on-message [data]
  (condp = (.-action data)
    "ack" (do
            (let [id (.-id data)]
              (when (= "identify" (get-in @sent-messages [id :action]))
                (reset! identified? true)
                (flush-queue))
              (swap! sent-messages dissoc id)))
    (log "unrecognized message" data)))
