 ;; State
 
 (defonce people-atom (atom []))
-
+(defonce filters (atom ["(.startsWith :Name \"Ma\")"]))
+(def all-profile-keys '(:IsObjectivesAdmin :LaborCategoryID :FirstName :LaborRoleID :OversightPercent :WorkTeamID :BusinessUnitName :MobileNumber :PhotoFileName :IsCandidateAdmin :CanCommunicateClient :UserSystemID :Email :CreatedDate :BillingTargetHoursPerYear :Title :MobileNumberCountryCode :IsClient :IsScheduleConfirmationRulesEnforced :LastName :IsScheduleAdmin :UserName :Extension :TimeZoneName :HomeNumber :IsNotAPerson :UserID :KeyscanUpdated :HasDirectReports :IsAdmin :BusinessUnitID :CountryID :CompanyBusinessUnitID :TimeZoneID :PhotoPath :Name :Roles :CompanyBusinessUnitName :IsWeeklyReviewAdmin :Enabled :TagName :Supervisors :KeyscanStatus :OutOfOfficeReason :Status))
+; (take! (genome/<get-all-active-klickster-profiles) (fn [profiles] (->> (apply merge profiles ) keys prn)))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Actions

            [reagent.session :as session]
            [secretary.core :as secretary :include-macros true]
            [clojure.walk :as walk]

            [cljs.tools.reader :refer [read-string]]
            [cljs.js :refer [empty-state eval js-eval]])

        (fn [result] result)))


;; Todo: Add ":my-bla"
(defn create-filter-fn 
  "Creates a filter function of the form:
  (fn [user] expr-as-fn)
  from a string 'expr' with one twist: 
  If a keyword is part of the passed-in 'keys' vector then this keyword will be transformed into a lookup of the 'user' binding.
  For instance, calling 
  (create-filter-fn [:Name]  \"(.startsWith :Name \"Max\")\")
  will result in a function like
  (fn [user] (.startsWith (:name user) \"Max\"))

  This can be later used to filter genome users by arbitrary criteria at runtime 
  "
  [thekeys expr]
  (let [my-user (gensym "user")
        replace-where-appropriate (fn [sym]
                                    (cond
                                      (not (keyword? sym)) sym
                                      (some #{sym} thekeys) `(~sym ~my-user)
                                      :else sym))]
    (->>
     (read-string expr)
     (walk/postwalk replace-where-appropriate ,,,)
     ((fn [new-expr]
         `(fn [~my-user] ~new-expr))) 
     (eval-str))))



;; Todo:
;; partition-by :KeyscanStatus. Then display and add filters.
;; add a <users-details test data set monday morning
;; partition the users by 200

(defn filtered-people-atom []
  (if-not (empty? @filters)
    (let [my-filters (map #(create-filter-fn all-profile-keys %) @filters)]
      (for [[floor people] @people-atom]
        [floor (filter (apply every-pred my-filters) people)]))
    @people-atom))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; filter functions
(defn add-filter 
  [filter-str]
  (swap! filters conj filter-str))

(defn vec-remove
  "remove elem in coll"
  [coll pos]
  (vec (concat (subvec coll 0 pos) (subvec coll (inc pos)))))

(defn remove-filter 
  [idx]
  (when @filters
    (reset! filters
            (vec-remove @filters idx))))


;; -------------------------
