 (ns cljs-morphic.morph.halo
   (:require [cljs-morphic.morph :refer [rectangle image ellipse find-morph
-                                        set-prop position-in-world]]
-            [cljs-morphic.helper :refer-macros [morph-fn] :refer [add-points]]))
+                                        set-prop position-in-world $morph redefine properties]]
+            [cljs-morphic.helper :refer-macros [morph-fn] :refer [add-points]]
+            [fresnel.lenses :refer [fetch]]))
 
 (declare resize-button inspect-button close-button)
 
 (defn halo [world-state id]
   "Render a halo element for morph with id"
-  (let [[_ {ext :extent pos :position} _] (find-morph world-state id)]    
+  (let [[_ {ext :extent pos :position} _] (fetch world-state ($morph id))]    
     (conj world-state
           (-> (rectangle {:id "halo" :border-color "red" :border-width 1
                           :layout (fn [self props submorphs]

(morph-fn close-button [halo props submorphs]
          (halo props 
                submorphs
                (-> (image {:position {:x -15 :y -15} 
                            :url "http://lively-web.org/core/media/halos/close.svg" 
                            :extent {:x 15 :y 15}
                            :id "closeButton"}))))

(morph-fn inspect-button [halo props submorphs]
          (halo props 
                submorphs
                (-> (image {:position (add-points {:x ((props :extent) :x) :y 0} {:x 5 :y -15}) 
                            :url "http://lively-web.org/core/media/halos/info.svg" 
                            :extent {:x 15 :y 15}
                            :id "infoButton"}))))
                          :id "resizeButton"
                          :position (add-points (props :extent) {:x 0 :y 0})
                          :draggable? true 
                          :on-drag (fn [world id {dx :x dy :y}]
                                     (-> world
                                         (redefine ($morph target-id) 
                                                   (fn [target props submorphs]
                                                     (target (assoc props :extent (add-points (:extent props) {:x (- dx) :y (- dy)})) submorphs)))
                                        (redefine ($morph "halo") 
                                                  (fn [halo props buttons]
                                                    (let [new-extent (add-points (:extent props) {:x (- dx) :y (- dy)})]
                                                      (-> (halo (assoc props :extent new-extent) buttons)
                                                        (set-prop "infoButton" :position (add-points {:x (:x new-extent) :y 0} {:x 5 :y -15}))))))
                                       ))}
                         (image {:id "resizeImage"
                                 :position {:x -5 :y -5} 
                                 :url "http://lively-web.org/core/media/halos/resize.svg" 
