   (reify
     om/IInitState
     (init-state [_]
-      {:chans {:clicked (chan (sliding-buffer 1))}})
+      {:chans {:selected (chan (sliding-buffer 1))}})
     om/IRenderState
     (render-state [_ {:keys [chans]}]
       (dom/div nil
            (dom/h3 #js {:key "head"} (str "Metering data - " (get-in cursor [:property])))
            (dom/p nil "Note: When you select something to plot on a given axis, you will only be able to plot other items of the same unit on that axis.")
-           ;; Builds chart component
+           (om/build date-picker cursor {:key :hecuba/name :init-state chans})
            (om/build chart-item cursor {:key :hecuba/name :init-state chans})
-           ;; Builds table containing form components for left and right axis
-           (println "Devices is" (:devices cursor))
-           #_(dom/div #js {:id "device-form"}
-                (om/build device-form (:devices cursor)
-                    {:key :hecuba/name :init-state chans}))))))
+          ))))
+
+

    (doall result-seq)))


;;;;;;;;;;; Data ;;;;;;;;;;;

(def mock-data {"01"
                [{"device_id" "01" "device_name" "External temperature" "month" "01/01/2011" "reading" 0.8}
                 {"device_id" "01" "device_name" "External temperature" "month" "01/02/2011" "reading" 0.9}
                 {"device_id" "01" "device_name" "External temperature" "month" "01/03/2011" "reading" 0.8}
                 {"device_id" "01" "device_name" "External temperature" "month" "01/04/2011" "reading" 0.75}
                 {"device_id" "01" "device_name" "External temperature" "month" "01/05/2011" "reading" 0.65}
                 {"device_id" "01" "device_name" "External temperature" "month" "01/06/2011" "reading" 0.50}
                 {"device_id" "01" "device_name" "External temperature" "month" "01/07/2011" "reading" 0.55}
                 {"device_id" "01" "device_name" "External temperature" "month" "01/08/2011" "reading" 0.6}
                 {"device_id" "01" "device_name" "External temperature" "month" "01/09/2011" "reading" 0.66}
                 {"device_id" "01" "device_name" "External temperature" "month" "01/10/2011" "reading" 0.68}
                 {"device_id" "01" "device_name" "External temperature" "month" "01/11/2011" "reading" 0.71}
                 {"device_id" "01" "device_name" "External temperature" "month" "01/12/2011" "reading" 0.9}]
                "02"
                [{"device_id" "02" "device_name" "External humidity" "month" "01/01/2011" "reading" 6}
                 {"device_id" "02" "device_name" "External humidity" "month" "01/02/2011" "reading" 10}
                 {"device_id" "02" "device_name" "External humidity" "month" "01/03/2011" "reading" 12}
                 {"device_id" "02" "device_name" "External humidity" "month" "01/04/2011" "reading" 15}
                 {"device_id" "02" "device_name" "External humidity" "month" "01/05/2011" "reading" 18}
                 {"device_id" "02" "device_name" "External humidity" "month" "01/06/2011" "reading" 20}
                 {"device_id" "02" "device_name" "External humidity" "month" "01/07/2011" "reading" 25}
                 {"device_id" "02" "device_name" "External humidity" "month" "01/08/2011" "reading" 31}
                 {"device_id" "02" "device_name" "External humidity" "month" "01/09/2011" "reading" 20}
                 {"device_id" "02" "device_name" "External humidity" "month" "01/10/2011" "reading" 17}
                 {"device_id" "02" "device_name" "External humidity" "month" "01/11/2011" "reading" 12}
                 {"device_id" "02" "device_name" "External humidity" "month" "01/12/2011" "reading" 9}]})

;;;;;;;;; Utils ;;;;;;;;;;;;;;;;;;;;;;

(def truthy? (complement #{"false"}))

;;;;;;;;;;; Component 1:  Form containing list of devices for both axis plots ;;;;;;;;;;

#_(defn device-form
  [cursor owner]
  (reify
    om/IRenderState
    (render-state [_ {:keys [clicked]}]
      (dom/table #js {:id "form-table" :cellSpacing "10"}
           (let [cols {:left "Left axis plots" :right "Right axis plots"}]
             (dom/tr nil
                  (into-array
                   (for [col cols]
                     (dom/td nil
                          (dom/h4 nil (val col))
                          #_(dom/form nil
                               (into-array
                                (for [device cursor]
                                  (let [id (str (:hecuba/name device))
                                        name (str (:name device))
                                        axis (first col)]
                                    (dom/div nil
                                         (dom/input #js
                                              {:type "checkbox"
                                               :onChange
                                               (fn [e]
                                                 (let [checked (= (.. e -target -checked) true)]
                                                   (put! clicked
                                                         {:id id
                                                          :axis axis
                                                          :checked checked})))})
                                         (dom/label nil name)
                                         (dom/br #js {})))))))))))))))

;;;;;;;;;;;;; Component 2: Chart ;;;;;;;;;;;;;;;;


  (reify
    om/IWillMount
    (will-mount [_]
      (let [clicked-items (om/get-state owner [:selected])]
        (go (while true
              (let [sel        (<! clicked-items)
                    start-date (get sel :start-date)
                    end-date   (get sel :end-date)
                    entity-id  (get sel :entity-id)
                    device-id  (get-in sel [:sensor :deviceId])
                    url        (url entity-id device-id start-date end-date)
                    ]
                 (GET url {:handler #(om/transact! cursor [:measurements] (constantly %))
                                   :headers {"Accept" "application/json"}
                                   :response-format :json
                                   :keywords? true})
                )))))
    om/IRender
    (render [_] 
      (prn "[I render]")
       (dom/div nil
                 (dom/div #js {:id "chart" :width 500 :height 550})))
    om/IDidUpdate
    (did-update [this prev-props prev-state root-node]
      (let [n (.getElementById js/document "chart")]
        (while (.hasChildNodes n)
          (.removeChild n (.-lastChild n))))
      (prn "[I did update]")
      (let [Chart      (.-chart dimple)
            svg        (.newSvg dimple "#chart" 500 500)
            type       (get-in cursor [:sensor :type])
            data       (get-in cursor [:measurements])
            dimple-chart (.setBounds (Chart. svg) 60 30 350 350)
            x (.addCategoryAxis dimple-chart "x" "timestamp")
            y (.addMeasureAxis dimple-chart "y" "value")
            s (if (not (empty? data )) (.addSeries dimple-chart (name type) js/dimple.plot.line (clj->js [x y])))]
        (.log js/console data)
        (if (not (nil? s)) (aset s "data" (clj->js data)))
        (.addLegend dimple-chart 60 10 300 20 "right")
        (.draw dimple-chart)))))

;;;;;;;;;;; Bootstrap ;;;;;;;;;;;;

