 (defroutes application-routes
   (GET "/" req "<h1>Tango on!</h1>")
   (GET "/admin" req {:body (slurp (clojure.java.io/resource "public/main.html"))
-                     :session {:uid 1}
+                     :session {:uid (rand-int 100)}
                      :headers {"Content-Type" "text/html"}})
   ;; Sente channel routes
   (GET  "/chsk" req (ring-ajax-get-or-ws-handshake req))

            [org.httpkit.server :as http-kit-server]
            [clojure.core.match :refer [match]]
            [tango.import :as imp]
            [ring.middleware.defaults :as defaults]))

(def memory-log (atom []))


  (swap! memory-log conj s))

(defn import-file [{:keys [content]}]
  (imp/dance-perfect-xml->data (imp/read-xml-string content)))

(def message-chan (chan))
(def message-send-chan (chan))

;; (go (println (<! test-chan)))
;; (>!! test-chan "Duudde")

(def message-log (atom []))
(def message-send-log (atom []))

(go (while true
      (let [msg (<! message-chan)]
        (swap! message-log conj msg)
        (message-dispatch msg message-send-chan))))

(go (while true
      (let [msg (<! message-send-chan)]
        (swap! message-send-log conj msg)
        (chsk-send! (:id msg) (:message msg)))))


(defn message-dispatch [{:keys [topic payload sender]} out-chan]
  (match [topic payload]
         [:file/import xml]
         (>!! out-chan {:id sender :message [:file/imported {:content (import-file xml)}]}) 
         [:client/ping p]
         (>!! out-chan {:id sender :message [:server/pong {:content 1}]})
         :else (println (str "Unmatched message topicz: " topic))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Sente socket setup
