 (ns aspire.templates
   (:require [net.cgrand.enlive-html :as en]
-            [hiccup.core :as hc]
             [aspire.util :as a-util]))
 
 ;; Selectors
 ;; -----------------------
 (def selectors {:common {:header [:nav#top-bar]
-                         :side-nav [:nav#side-nav]}
+                         :title [:head :title]
+                         :menu [:nav#side-nav]
+                         :main [:main#content]}
                 :onboarding {:intro [:main#content :div.intro]
                              :steps [:main#content :ol.onboarding-steps]
                              :step [:main#content :ol.onboarding-steps :li.step-1]}})

  [:h2] (en/content title)
  [:p] (en/content desc))

(en/defsnippet onboarding-step "public/index.html"
  (get-in selectors [:onboarding :step])
  [n desc]
  [:strong] (en/content (str "Step " n))
  [:span] (en/content desc))

#_(en/defsnippet menu "public/index.html" {})

;; Transformers
;; -----------------------

           [[:form#search (en/attr? :data-active)]] (en/set-attr :data-active tf)
           [[:body (en/attr? :data-search-active)]] (en/set-attr :data-search-active tf))))

(en/defsnippet common-head "public/index.html" [:head]
  [title desc]
  [:title] (en/content title)
  [[:meta (en/attr-has :name "description")]] (en/set-attr :content desc))

;; Template
;; -----------------------
(en/deftemplate base "public/index.html"
  ;; Do things in this order. main [probably] pulls in head, header,
  ;; and menu, so those transforms must follow main.
  [{:keys [head header menu]} main search-toggle]
  (get-in selectors [:common :main]) (en/substitute main)
  (get-in selectors [:common :header]) (en/substitute header)
  (get-in selectors [:common :menu]) (en/substitute menu)
  [:head] (en/substitute head)
  [[:form#search (en/attr? :data-active)]] (en/set-attr :data-active search-toggle)
  [[:body (en/attr? :data-search-active)]] (en/set-attr :data-search-active search-toggle))

;; Templaters
;; -----------------------
(defn common-snippets [ctx]
  {:head (common-head "TODO: Get the title out of the request." "TODO: Also get a description out of the request.")
   :header (common-header (rand-int 100) "http://google.com" "Bo Jackson")
   :menu (common-menu [{:name "Admin" :href "/admin"}
                       {:name "Home" :href "/"}
                       {:name "Add to your playlist" :href "/welcome"}])})

(defn onboarding [common-snippets & args]
  (let [main (apply onboarding-main args)]
    (render (base common-snippets main true))))

(defn admin [common-snippets & args]
  (let [main (apply admin-main args)]
    (render (base common-snippets main false))))
