 (ns clj-money.web.shared
   (:require [clojure.tools.logging :as log]
             [clojure.set :as set]
+            [clojure.string :as string]
+            [environ.core :refer [env]]
             [hiccup.core :refer :all]
             [hiccup.page :refer :all]
             [clojure.string :as s]
             [cemerick.friend :as friend]
-            [clj-money.models.users :as users])
+            [clj-money.models.users :as users]
+            [clj-money.models.entities :as entities])
   (:use clj-money.inflection))
 
 (defn glyph-link

           items)]
     [:div.navbar-right
      [:ul.nav.navbar-nav.navbar-right
       (if-let [user (friend/current-authentication)]
         (html
           [:li
            [:a {:href "#"} (users/full-name user)]]
           [:li
            (glyph-link :log-out "/logout" {:title "Click here to sign out."
                                            :data-method :post})])
         (html
           [:li
            [:a {:href "/signup"} "Sign up"]]
           [:li
            (glyph-link :log-in "/login" {:title "Click here to sign in."})]))]]]]])

(defn primary-nav
  "Renders the site primary navigation"
  []
  (let [user (friend/current-authentication)
        items [{:url "/entities"     :caption "Entities"}
               {:url "/transactions" :caption "Transactions"}
               {:url "/commodities"  :caption "Commodities"}]]
    (bootstrap-nav items)))

(defn render-alerts
  "Renders notifications as HTML"

                                   (s/join " " (glyph-button-css options)))
                            (dissoc :size :level))))

(defn- authenticated-user-nav
  "Renders the top level navigation for the authenticated user"
  [user entity]
  (html
    [:li
     [:a {:href "#"} (users/full-name user)]]
    [:li.dropdown {:role "presentation"}
     [:a.dropdown-toggle {:href (str "/entities/" (:id entity) "/accounts")
                          :data-toggle "dropdown"
                          :role "button"
                          :aria-haspopup "true"
                          :aria-expanded "false"}
      (:name entity)
      [:span.caret]]
     [:ul.dropdown-menu
      (let [other-entities (remove #(= (:id %) (:id entity)) (entities/select (env :db) (:id user)))]
        (when (seq other-entities)
          (concat
            (map #(vector :li
                          [:a {:href (str "/entities/" (:id %) "/accounts")}
                           (:name %)])
                 other-entities)
            [[:li.divider {:role "separator"}]])))
      [:li
       [:a {:href "/entities"} "Manage entities"]]]]
    [:li
     (glyph-link :log-out "/logout" {:title "Click here to sign out."
                                     :data-method :post})]))

(defn- unauthenticated-nav
  "Renders the top level navigation for a non-authenticated user"
  []
  (html
    [:li
     [:a {:href "/signup"} "Sign up"]]
    [:li
     (glyph-link :log-in "/login" {:title "Click here to sign in."})]))

; TODO Wrap this up in a sharable library
(defn- bootstrap-nav
  "Renders the site navigation using the structure for twitter bootstrap"
  [items user entity]
  [:nav.navbar.navbar-default
   [:div.container
    [:div.navbar-header
