 (ns client.views.viz.plot
   (:require-macros [cljs.core.async.macros :refer [go go-loop]]
                    [cljs.core.match.macros :refer [match]])
+  (:import [goog.events EventType])
   (:require [strokes :refer (d3)]
             [dommy.utils :as utils]
             [dommy.core :as dommy]
+            [goog.events :as events]
             [om-tools.core :refer-macros [defcomponent]]
             [cljs.core.match :as m]
             [client.request :refer (r)]
-            [cljs.core.async :as async :refer [<! put! chan sub sliding-buffer]]
+            [cljs.core.async :as async :refer [<! put! chan sub sliding-buffer alts!]]
             [cljs-log.core :as log]
             [om.dom :as dom :include-macros true]
             [om.core :as om :include-macros true]

(def l (log/get-logger "3d"))
(strokes/bootstrap)

(defn ^:private v [x y z]
  (js/THREE.Vector3. x y z))


;; ref: http://bl.ocks.org/phil-pedruco/9852362
(defcomponent plot-view
  [app owner]
  (init-state
   [_]
   (let [w 960 h 500
         line-geo (js/THREE.Geometry.)
         line-material (js/THREE.LineBasicMaterial.  (clj->js  {:color 0x000000
                                                                :lineWidth 2}))]

                 (.log js/console camera)
                 (. camera (lookAt (.-position scene)))
                 (. renderer (render scene camera))
                 (let [down (chan)
                       up (chan)
                       move (chan)
                       ]
                   (events->chan js/window EventType.MOUSEDOWN down)
                   (events->chan js/window EventType.MOUSEUP up)
                   (events->chan js/window EventType.MOUSEMOVE move)
                   (go-loop [[event c] (alts! [down up move])]
                     (.log js/console event)
                     (.log js/console "up: " (= up c))
                     (.log js/console "down: " (= down c))
                     (.log js/console "move: " (= move c))
                     (.log js/console "c: " c)
                     (condp = c
                       down (do
                              (log/fine l "DOWN")
                              (om/set-state! owner :down true)
                              (om/set-state! owner :sx (.-clientX event))
                              (om/set-state! owner :sy (.-clientY event)))
                       up (do
                            (log/fine l "UP") 
                            (om/set-state! owner :down false))
                       move (when (om/get-state owner :down)
                              (println (om/get-state owner))
                              (let [{:keys [sx sy]} (om/get-state owner)
                                    dx (- (.-clientX event) sx)
                                    dy (- (.-clientY event) sy)]
                                (.log js/console sx sy)
                                (.log js/console dx dy)
                                (.log js/console (aget scatter-plot "rotation" "y"))
                                (aset scatter-plot "rotation" "y" (+ (aget scatter-plot "rotation" "y") (* dx 0.01)))
                                (aset camera "position" "y" (+ (aget camera "position" "y") dy))
                                (om/set-state! owner :sx (+ sx dx))
                                (om/set-state! owner :sy (+ sy dy))
                                ))
                       (log/warning l "nothing!"))
                     (recur (alts! [down up move])))
                   )

                 
                 (.log js/console renderer)
                 (.log js/console (.-info renderer))
                 (animate renderer camera scene (.getTime (js/Date.)))
                 )))
           ))))
  (render-state
