             [sablono.core :as html :refer-macros [html]]
             [cljs.core.async :refer [put! <! >! chan timeout close!]]
             [searchbot.es :refer [es-agg filtered-agg]]
-            [searchbot.input.labels :as labels]))
+            [searchbot.input.labels :as labels]
+            [searchbot.charts :refer [es-chart]]))
 
 (defn- build-sub-aggs [sub-aggs]
   (if sub-aggs {:aggs sub-aggs} {}))

  (did-mount [_]
             (let [{:keys [comm parsets-agg]} (om/get-state owner)
                   parsets-div (om/get-node owner "parsets-div")
                   card-width (.-offsetWidth parsets-div)
                   get-svg #(new-svg "#parsets" card-width height)
                   chart #(make-parsets-chart
                           {:height height :width card-width
                            :get-parsets-agg (fn [_] (:parsets-agg (om/get-state owner)))})]
               (.attach js/Waves (om/get-node owner "parsets-div") "waves-yellow")
               (go (while (:continue? (om/get-state owner))

    (do-parsets-agg owner)
    ))

(defn- new-svg [{:keys [id height width-fn]}]
  (-> js/d3 (.select id) (.append "svg") (.attr "width" (width-fn)) (.attr "height" height)))

(defn- make-parsets-chart [{:keys [height width-fn get-parsets-agg]}]
  (-> js/d3
      .parsets
      (.tension 0.8)
      (.width (width-fn))
      (.height height)
      (.dimensions (clj->js (map name (get-parsets-agg))))
      (.value (fn [d i] (. d -value)))))

(defcomponent parsets [cursor owner {:keys [agg value-path url height] :or {height 600 value-path ["doc_count"]} :as opts}]
  (init-state [_]
              (let [{:keys [es-settings ref-state]} (om/get-shared owner)
                    prefix-terms "terms_"]
                {:continue? true
                 :comm (chan)
                 :url url
                 :agg-prefix-terms prefix-terms
                 :parsets-agg (->> agg :terms (map keyword))
                 :get-agg-terms #(:parsets-agg (om/get-state owner))
                 :make-agg-query #(->query {:agg-prefix-terms prefix-terms
                                            :agg-terms ((:get-agg-terms (om/get-state owner)))
                                            :sub-aggs (:sub agg)
                                            :show-pies? (:show-pies? (om/get-state owner))
                                            :filters (ref-state :filters)
                                            :default-filter (get-in (es-settings) [:default :filter])})
                 :value-path (apply vector (map keyword value-path))
                 :combined-query nil
                 :svg nil
                 :parsets-cursor (om/root-cursor (atom {:div {:width "90%" :height 200}}))
                 :show-pies (:show-pies opts)
                 :show-pies? #(:show-pies (om/get-state owner))}))
  (will-mount [_]
              (do-parsets-agg owner))
  (render-state [_ {:keys [parsets-agg get-agg-terms agg-prefix-terms parsets-cursor show-pies ]}]
                (html
                 [:.card {:ref "parsets-card"}
                  [:.card-content
                   [:.row
                    [:span.card-title.black-text "# PARSETS [ " [:strong (->> parsets-agg (map name) (clojure.string/join " > "))] " ]"]
                    [:p
                     [:input {:type "checkbox" :class "filled-in" :checked show-pies
                              :id "toggle-show-pies" :ref "toggle-show-pies"
                              :on-change (fn [_] (let [thisNode (om/get-node owner "toggle-show-pies")
                                                       checked (.-checked thisNode)]
                                                   (om/set-state! owner :show-pies checked)
                                                   (do-parsets-agg owner)))}]
                     [:label {:for "toggle-show-pies"} "Show Pie Charts"]]
                    [:div.right {:style {:max-width "50%"}}
                     (let [label-string (->> (get-agg-terms) (map name) (clojure.string/join " "))]
                       (om/build labels/labels cursor
                                 {:opts {:labels (labels/labelize label-string)
                                         :on-label-updated! (partial update-agg-terms-with-label owner)}}))]
                    ]
                   [:.row
                    [:.col {:class (if show-pies "s9" "s12") :id "parsets" :ref "parsets-div"}]
                    [:.col {:class (if show-pies "s3" "hide")}
                     (for [term (get-agg-terms)]
                       (let [term-name (name term)]
                         (om/build es-chart
                                   parsets-cursor
                                   {:opts {:type "es-chart"
                                           :id (str term-name "_pie")
                                           :agg-key "terms-aggs"
                                           :agg-top (str agg-prefix-terms term-name)
                                           :agg-view ["key" "sum_usage"]
                                           :draw-fn "draw-ring"
                                           :chart {:bounds {:x "5%" :y "15%" :width "80%" :height "80%"}
                                                   :plot "pie"
                                                   :p-axis "sum_usage"
                                                   :c-axis "key"}}}
                                   )
                         ))
                     ]
                    ]]]
                 ))
  (did-update [_ _ _]
              (if-let [filters ((:ref-state (om/get-shared owner)) :filters)]
                (let [local-combined-query (-> (om/get-state owner) :combined-query)
