 (ns app.app
-  (:require [goog.dom :as dom]
+  (:require [goog.events :as events]
+            [goog.dom :as dom]
             [clojure.string :as string :refer [blank?]]
             [cljs.core.async :refer [<! >! put! close! chan
                                      timeout sliding-buffer]]

   :quality-of-evidence "Quality Of Evidence"
   :strength-of-recommendation "Strength of Recommendation"
   :evidence "Evidence"})

; (defsnippet result-data "templates/results.html" [:.list-row] [[k v]]
;   {[:.list-row] (kioo/content [:li (k labels)]
;                               [:li v])})
;
; (defsnippet result-card "templates/results.html" [:.card] [results]
;   {[:.list-column] (kioo/content [:li
;                                    [:ul {:class "list-row"}
;                                      (map result-data results)]])})
;
; (deftemplate result-cards "templates/results.html" []
;   {[:.cards] (kioo/content (map result-card
;                              (:results @state)))})

(defsnippet result-data "templates/results.html" [:.row] [[k v]]
  {[:.row] (kioo/content [:li (k labels)] [:li v])})

  (:require-macros [kioo.reagent :refer [defsnippet deftemplate]]
                   [cljs.core.async.macros :refer [go go-loop]]))

(def all-results-url "data/beers-2015.json")

; ElasticSearch API URL
(def query-url "http://168.235.155.245/beers/2015/_search")
;(def query-url "search_test.json")

; Application state
(def state (reagent/atom {:all-results {} :results {} :cache {}}))

(defn update-results [results]
  (swap! state assoc :results results))

(defn update-cache [key new-results]
  (swap! state assoc-in [:cache key] new-results))

(defn load-all-results [url]
  (go
    (swap! state assoc :all-results (:body (<! (http/get url))))
    (update-results (:all-results @state))))

; Templating
(def labels
