 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; filter functions
 (defn add-filter 
-  [filter-str]
-  (swap! filters conj filter-str))
+  [profile filter-str]
+  (let [transformed-filter (replace-current-user-symbols profile filter-str)]
+    (if transformed-filter
+      (do
+        (swap! filters conj transformed-filter)
+        transformed-filter)
+      nil)))
 
 (defn vec-remove
   "remove elem in coll"

  (with-out-str (cljs.pprint/pprint data)))

(defn Instructions 
  []
  (let [current-profile (atom "Nothing here")]
    (go
      (let [profile (<! (genome/<current-user-profile))]
        (reset! current-profile (-> profile prettify ))))
    (fn []
      [:div
       [:p "Here is a list of all available attributes:"
        [:p (str all-profile-keys)]
        "And for the record, here is your profile:"]
       [:pre @current-profile]])))

(defn New-Filter-Field [inputatm]
  [:div.row
   [:input.col.s9.offset-s1 {:type "text"
                             :placeholder "Add a new filter"
                             :value @inputatm
                             :on-change #(reset! inputatm (-> % .-target .-value))}]
   [:button.col.s1.btn {:on-click #(do (add-filter @inputatm)
                                       (reset! inputatm ""))} "Add"]])

(defn Current-Filters [inputatm]
  [:ul.collection

                    [:a {:on-click #(remove-filter idx)} [:i.material-icons "delete"]]
                    ]]) @filters)])

(defn Examples [current-profile filter-examples]
  [:ul.collection
   (map-indexed (fn [idx filter]
                  ^{:key filter}
                  [:li.collection-item filter [:a.secondary-content {:on-click #(add-filter @current-profile filter)} [:i.material-icons "playlist_add"]]]) filter-examples)])

(defn Filters []
  (let [inputatm (atom "")
        current-profile (atom nil)
        current-profile-str (atom "")]
    (go
      (let [profile (reset! current-profile (<! (genome/<current-user-profile)))]
        (reset! current-profile-str (-> profile prettify ))))
    (fn []
      [:div
       [:h3 "Filters"]
       [Current-Filters inputatm]
       [New-Filter-Field current-profile inputatm]
       [:hr]
       [:h3 "Examples"]
       [:p "Here are some example filters to get you started!"]
       [Examples current-profile [
                                  "(startsWith? :FirstName \"M\")" 
                                  "(= :LaborRoleID \"APPLDEVL\")"
                                  "(= :WorkTeamID $WorkTeamID)"
                                  "(substring? :Title \"Quality\")"
                                  "(>= :UserID $UserID)"
                                  "(= :KeyscanStatus \"In 4th Flr N\")"
                                  "(or (startsWith? :FirstName \"M\") (substring? :Title \"Edit\"))"
                  ]]
       [:hr]
       [:h3 "References"]
       [Instructions current-profile-str]])))

(defn current-page []
  [:div [(session/get :current-page)]])
