             [markdown.core :refer [md->html]]
             [ajax.core :as ajax :refer [GET POST]])
   (:import goog.History))
-
-;; MODEL
-
-(def app-state {:teas-list (reagent/atom nil) :new-tea (reagent/atom nil) :edit-tea-id (reagent/atom nil)
-                :edit-tea (reagent/atom nil) :edit-tea-snap (reagent/atom nil) :show-edit (reagent/atom false)})
-
-;; VIEW
 
 (defn nav-link [uri title page collapsed?]
   [:li {:class (when (= page (session/get :page)) "active")}

          [:tr
           [:td [:li]]
           [:td (second tea)]
           [:td [:input.btn.btn-success {:type "button" :value "Edit" :on-click #(reset-app-state (first tea) (second tea))}]]])]]]]))


(defn tea-adder

       [:div {:dangerouslySetInnerHTML
              {:__html (md->html docs)}}]]])])

(def teas-list (atom nil))
(def new-tea (atom nil))
(def edit-tea-id (reagent/atom nil))
(def edit-tea (reagent/atom nil))
(def edit-tea-snap (reagent/atom nil))
(def show-edit (reagent/atom false))

(defn get-teas
  "Gets the list of all teas from the website"
  []
  (ajax/GET "/api/teas" :handler (fn [response] (reset! teas-list response))))
(defn delete-tea
  "Sends a get request that deletes the tea argument"
  [name]
  (ajax/GET (str "/api/teas/" name "/delete")
                                  :error-handler (fn [response] (println "ERROR" response))
                                  :handler (fn [response] (println name " deleted") (get-teas) (reset! edit-tea-snap nil))))

(defn update-tea
  "Sends an update request to the API, which updates the tea in the db"
  [id newname]
  (ajax/GET (str "/api/teas/" id "/update/" newname)
                                          :error-handler (fn [response] (println "ERROR" response))
                                          :handler (fn [response] (println newname " updated") (get-teas) (reset! edit-tea nil))))
(defn post-to-teas-db
  "Sends an Ajax Request to the API route to post the tea from the form to the database"
  [tea-name]
  ;; (println "post-to-teas-db hit, params: " tea-name)
  (ajax/POST "/api/newtea" {:params {:new-tea tea-name} :format :json
                           :handler (fn [] (println "New Tea Posted") (get-teas) (reset! new-tea nil))
                           :error-handler (fn [err] (println "New Tea Failed To Post" err))}))

(defn show-edit-form 
  "show/hide the show-edit-form"
  [id name]
  (println "show-edit-form" id "||" name "||" show-edit "||" @show-edit)
  (reset! edit-tea-id (second id))
  (reset! edit-tea (second name))
  (reset! edit-tea-snap (second name))
  (swap! show-edit not))

(defn tealister
  "The page component for listing tea"
