 ;                      #js {:success (fn [result] (js/console.log result))
 ;                           :error #(js/console.log %2)})))
 
-(def defn-atom (reagent/atom {:text "" :author ""}))
+(def app-atom (reagent/atom {}))
+
+(def ^:const parse-defn-class "Definition")
+
+; (defmacro handler-fn
+;   ([& body]
+;     `(fn [~'event] ~@body nil)))  ; always return nil
+
+(defn parse-init []
+  (js/Parse.initialize
+   "JSfVAQ9Qo25p2Zdyqk1KeIxt3lGXmpX0AM9TGp5Q"
+   "btvf1ywruhW5b3opO6WkrzeCp73jOsRETZ0diJ5B"))
+
+(defn generate-user-token []
+  (let [rand-str (str (js/Date.now) (rand 100000))
+        token (-> rand-str js/CryptoJS.SHA1 .toString)]
+    (str token)))
+
+; This returns a JS promise b/c that's what the signUp returns.
+(defn save-anonymous-user []
+    (let [user (js/Parse.User.)
+        token (generate-user-token)]
+    (.set user "username" token)
+    (.set user "password" token)
+    (.signUp user)))
+
+;(defn save-defn [app-atom]
+;  (let [Definition (js/Parse.Object.extend "Definition")
+;        new-defn (Definition.)
+;        new]
+;    (def defn-saved-promise (.then user-saved-promise
+;      (fn [new-defn]
+;        (.setACL new-defn usr)
+;        (.save new-defn #js {:definedby curr-user
+;                             :definition (get @app-atom :definition)
+;                             :author (condp = (get @app-atom :author)
+;                                       nil "Anonymous"
+;                                       "" "Anonymous"
+;                                       (get @app-atom :author))
+;                             :email (get @app-atom :email)
+;                             :definitionSubject "history"
+;                             :mehuman "1"}))))
+;    (.then defn-saved-promise #(js/console.log "defn saved!"))
+;    (.fail defn-saved-promise #(js/console.log "defn save failed.")))))
 
 (defn get-defn [idx]
   (js/Parse.Cloud.run

;; -------------------------
;; Components

(defn defn-form-content []
  [:form {:class "defnForm"}
   [:div {:class "fieldsWrap"}
    [:input {:type "text"
             :class "authorInput"
             :placeholder "Your Name (Optional)"}]
    [:a {:class "button formToggle"} "Add Another Author's Definition"]
    [:br]
    [:input {:type "email"
             :class "emailInput"
             :placeholder "Enter Your Email"}]
    [:input {:type "email"
             :class "emailConfirm"
             :placeholder "Enter Your Email"}]
    [:span {:class "emailIsConfirmed no"}]
    [:textarea {:class "defnInput twelve columns"
                :placeholder "Please compose your definition here..."}]
    [:div {:class "btnWrap"}
     [:button {:class "button-primary defnSubmit"} "Submit"]]]])

(defn defn-form []
  [defn-form-content])

(defn definition [defn-atom]
  (fn [defn-atom]
    (if-not (= (get @defn-atom :text) "")
      [:section {:class "row"}
       [:p {:class "ten columns"} (get @defn-atom :text)
        [:span {:class "author"}" -- " (get @defn-atom :author)]]])))

(defn footer []
  [:footer

    (get-rand-defn)
    [:div {:class "container home"}
     [:header [:h2 {:class "title"} "What is History?"]]
     [definition app-atom]
     [:a {:href "/defineit"} "Contribute Your Own Definition"]
     [footer]]))

(defn defineit-page []
  (parse-init)
  (def usr-promise (save-anonymous-user))
  (.then usr-promise (fn [usr]
                       (.set usr "ip" (js/getIP))
                       (.save usr)))
  (fn []
    [:div {:class "container defineit"}
     [:header [:h2 {:class "title"} "What is History?"]]
