 (ns stonecutter.view.profile
-  (:require [traduki.core :as t]
-            [net.cgrand.enlive-html :as html]
+  (:require [net.cgrand.enlive-html :as html]
             [stonecutter.config :as config]
             [stonecutter.routes :as r]
             [stonecutter.view.view-helpers :as vh]))
 
-(defn add-username [request enlive-m]
+(defn add-username [enlive-m request]
   (let [email (get-in request [:session :user-login])]
     (html/at enlive-m
              [:.clj--card-email] (html/content email))))
 
 (def library-template (vh/load-template "public/library.html"))
 
-(def application-list-item (first (html/select library-template [:.clj--authorised-app__list-item])))
+(defn application-list-item [library-m] (first (html/select library-template [:.clj--authorised-app__list-item])))
 
-(def empty-application-list-item (first (html/select library-template [:.clj--authorised-app__list-item--empty])))
+(defn empty-application-list-item [library-m] (first (html/select library-template [:.clj--authorised-app__list-item--empty])))
 
-(defn application-list-items [authorised-clients]
-  (html/at application-list-item
+(defn application-list-items [authorised-clients library-m]
+  (html/at (application-list-item library-m)
            [:.clj--authorised-app__list-item]
            (html/clone-for [client authorised-clients]
                            [:.clj--client-name] (html/content (:name client))

                                                                                     "?client_id="
                                                                                     (:client-id client))))))

(defn add-application-list [request enlive-m]
  (let [authorised-clients (get-in request [:context :authorised-clients])]
    (if-not (empty? authorised-clients)
      (html/at enlive-m [:.clj--app__list] (html/content (application-list-items authorised-clients)))
      (html/at enlive-m [:.clj--app__list] (html/content empty-application-list-item)))))

(defn set-sign-out-link [enlive-m]
  (html/at enlive-m

  (html/at enlive-m
           [:.clj--delete-account__link] (html/set-attr :href (r/path :show-delete-account-confirmation))))

(defn hide-admin-span [enlive-m request]
  (let [role (get-in request [:context :role])]
    (html/at enlive-m [:.clj--admin__span]
             (when (= role (:admin config/roles)) identity))))

(defn diplay-email-unconfirmed-message [enlive-m request]
  (if (= false (get-in request [:context :confirmed?]))
    enlive-m
    (vh/remove-element enlive-m [:.clj--unconfirmed-email-message-container])))

(defn set-flash-message [enlive-m request]
  (case (:flash request)
    :password-changed (html/at enlive-m [:.clj--flash-message-text] (html/set-attr :data-l8n "content:flash/password-changed"))
    :email-confirmed (html/at enlive-m [:.clj--flash-message-text] (html/set-attr :data-l8n "content:flash/email-confirmed"))
