 (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]}})

           [[: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))))

;; Templates
;; -----------------------
(en/deftemplate onboarding (search-toggle "public/index.html" true)
  [header greeting greeting-msg steps]
  (get-in selectors [:common :header]) (en/substitute header)
  (get-in selectors [:onboarding :intro]) (en/substitute (onboarding-intro greeting greeting-msg))
  (get-in selectors [:onboarding :steps]) (en/content (map-indexed (fn dostep [n desc] (onboarding-step (inc n) desc)) steps)))

(en/deftemplate admin (search-toggle "public/admin/index.html" false)
  [header greeting greeting-msg steps-str]
  (get-in selectors [:common :header]) (en/substitute header)
  [:body#admin] (en/set-attr :data-search-active false)
  [:div.config-onboarding :h2] (en/content "Configure Page: Onboarding")
  [:main#content :h1] (en/content "Aspire Administration")
  [:form#config-onboarding] (en/set-attr :action "/config/page/onboarding")
  [:form#config-onboarding :input#greeting] (en/set-attr :value greeting)
  [:form#config-onboarding :input#greeting-msg] (en/set-attr :value greeting-msg)
  [:form#config-onboarding :textarea#steps] (en/content steps-str))


  [: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 onboarding-main "public/index.html" (get-in selectors [:common :main])
  [greeting greeting-msg steps]
  (get-in selectors [:onboarding :intro]) (en/substitute (onboarding-intro greeting greeting-msg))
  (get-in selectors [:onboarding :steps]) (en/content (map-indexed (fn dostep* [n desc] (onboarding-step (inc n) desc)) steps)))

(en/defsnippet admin-main "public/admin/index.html" (get-in selectors [:common :main])
  [greeting greeting-msg steps-str]
  [:h1] (en/content "Aspire Administration")
  [:div.config-onboarding :h2] (en/content "Configure Page: Onboarding")
  [:form#config-onboarding] (en/set-attr :action "/config/page/onboarding")
  [:form#config-onboarding :input#greeting] (en/set-attr :value greeting)
  [:form#config-onboarding :input#greeting-msg] (en/set-attr :value greeting-msg)
  [:form#config-onboarding :textarea#steps] (en/content steps-str))

(en/defsnippet menu-item "public/index.html" [:nav#side-nav :ul :> en/first-child]
  [{:keys [name href]}]
  [:a] (en/do->
        (en/content name)
        (en/set-attr :href href)))

(en/defsnippet common-menu "public/index.html" [:nav#side-nav]
  [menu-items]
  [:ul] (en/content (map menu-item menu-items)))

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