 (ns ^:figwheel-always web-audio-cljs.core
   (:require [om.core :as om :include-macros true]
             [om.dom :as dom :include-macros true]
+            [goog.dom :as googdom]
+            [goog.events :as events]
             [web-audio-cljs.audio :as audio]
             [web-audio-cljs.utils :refer [l]]
-            [cljs.core.async :refer [put! chan timeout <! >!] :as async])
+            [cljs.core.async :refer [put! chan timeout sliding-buffer <! >!]])
+  (:import [goog.events EventType])
   (:require-macros [cljs.core.async.macros :refer [go go-loop alt!]]))
 
 (enable-console-print!)

                     :canvas-context nil
                     :canvas-width 400
                     :canvas-height 100
                     :mouse-chan (chan)
                     :x-offset 0})

    om/IDidMount
    (did-mount [_]
      (let [{:keys [canvas-width canvas-height mouse-chan left-x x-offset]} (om/get-state owner)
            canvas (om/get-node owner "canvas-ref")
            canvas-context (.getContext canvas "2d")
            x-pos (.-left (.getBoundingClientRect canvas))]
        (go
          (while true
            (let [[target x y] (<! mouse-chan)
                  rel-x (- x x-pos)]
              (.log js/console "!!!target: " target)
              (.log js/console "!!!X" x)
              (.log js/console "OFFSET x: " rel-x)
              (.log js/console "!!!Y" y)
              (om/update-state! owner #(assoc % :x-offset rel-x)))))
        (om/update-state! owner #(assoc % :canvas-context canvas-context :canvas canvas))))

    om/IDidUpdate
    (did-update [_ _ {:keys [canvas-width canvas-height canvas-context x-offset]}]
      (if canvas-context
        (draw-select-rect! canvas-width canvas-height canvas-context x-offset)
        (om/refresh! owner)))

    om/IRenderState
    (render-state [_ {:keys [canvas-width canvas-height mouse-chan]}]
      (dom/canvas #js {:width canvas-width
                       :height canvas-height
                       :style #js {:opacity 0.3 :position "relative" :left (- canvas-width)}
                       :ref "canvas-ref"
                       :onMouseMove (fn [e]
                                      (.stopPropagation e)
                                      (.preventDefault e)
                                      (put! mouse-chan [(.-target e) (.-pageX e) (.-pageY e)]))} "no canvas"))))

(defn buffer-view [data owner]
  (reify

          (recur (inc i) cur-val)
          (recur (inc i) max-val))))))

(defn draw-select-rect! [canvas-width canvas-height canvas-context x mouse-down?]
  (.clearRect canvas-context 0 0 canvas-width canvas-height)
  (.fillRect canvas-context x 0 100 100)
  (when mouse-down?
      (aset canvas-context "strokeStyle" "aliceblue")
      (aset canvas-context "lineWidth" 6)
      (.strokeRect canvas-context x 2 100 96)))


(defn draw-buffer! [width height canvas-context data]
  (let [step (.ceil js/Math (/ (.-length data) width))
