(ns wanijo.infrastructure.view
  (:require [hiccup
             [page :refer [html5 include-js include-css]]
             [core :refer [h]]]
            [wanijo.infrastructure.routing :refer [path]]))

(defn btnlink
  ([target caption]
   (btnlink target caption ""))
  ([target caption clss]
   [:a {:href target}
    [:button {:class clss}
     caption]]))

(defn delete-btn []
  [:input {:type "submit"
           :class "delete-btn"
           :value "☒ Delete!"}])

(defn flash-error [content]
  [:section.flash--error
   [:h2.flash__heading--error "Warning"]
   content])

(defn layout
  [& {:keys [content
             title
             session
             request
             head]
      :or {content []
           title nil
           request {}
           session nil
           head nil}}]
  (let [session (or session (:session request))
        ident (:ident session)
        authed? (some? ident)
        devmode? (:devmode session)]
    (html5
     [:head
      [:meta {:charset "utf-8"}]
      [:meta {:name "viewport"
              :content "width=device-width,initial-scale=1,shrink-to-fit=no"}]
      [:title (str "wan ijo" (when title (str " - " title)))]
      (include-css "/css/app.css")
      (when devmode? (include-css "/css/devmode.css"))
      (include-js "/js/scripts.js")
      head]
     [:body
      [:section.grid
       [:header
        [:h1.app-title "wan ijo"
         (when authed?
           [:small.app-title__hello
            "Hi, "
            [:a {:href (path :user-profile)}
             (h ident)]])]
        (when authed?
          [:section.header-content
           (btnlink (path :schema-overview)
                    "Schemas"
                    "header-content__link")
           (btnlink (path :auth-logout)
                    "Logout!"
                    "header-content__link")])]
       [:nav
        (when authed?
          (list
           [:section.schemas
            [:h2 "Schemas"]
            [:ul
             (for [schema (:schemas session)]
               [:li [:a {:href (path :instance-list
                                     {:schema-uuid (:uuid schema)})}
                     (h (:name schema))]])]]
           [:secion
            [:h2 "Prefiltered"]
            [:ul
             [:li [:a {:href (path :instance-list-starred)}
                   "Starred"]]]]
           [:section
            [:h2 "Visualisation"]
            [:ul
             [:li [:a {:href (path :vis-all-instances)}
                   "All Instances"]]]]
           [:section
            [:h2 "Triology"]
            [:ul
             [:li [:a {:href (path :public-index)}
                   "Public"]]]]))]
       (into [:main
              (for [msg (:flash request)]
                (flash-error msg))]
             content)
       [:footer
        [:small "Ilo pali e ijo"]]]])))
