(ns wanijo.schema.view
  (:require [hiccup
             [form :as hform]
             [core :refer [h]]]
            [ring.util.anti-forgery :refer [anti-forgery-field]]
            [formulare.core :as form]
            [wanijo.infrastructure.view :as view]
            [wanijo.infrastructure.routing :refer [path]]
            [wanijo.infrastructure.time :refer [prettify-dt]]
            [wanijo.schema.db :as domain]
            [wanijo.schema.forms :as forms]))

(defn overview! [req]
  (let [session (:session req)
        uuid (:uuid session)
        schemas (domain/all-created-by! uuid)]
    (view/layout
     :request req
     :content
     [[:h1 "All schemas"]
      [:table
       [:thead
        [:tr
         [:th "Name"]
         [:th "Created"]]]
       [:tbody
        (for [schema schemas]
          [:tr
           [:td
            [:a {:href (path :schema-show schema)}
             (h (:name schema))]]
           [:td
            (prettify-dt (:created_at schema))]])]]
      [:h1 "New schema"]
      (hform/form-to [:post (path :schema-new)]
                     (form/render-widgets forms/schema {} req)
                     (hform/submit-button "Create"))])))

(defn show-schema! [schema attrs assign-form conn-form req]
  (view/layout
   :request req
   :content
   [[:h1 "Schema "
     [:span.schema-title__name
      (h (:name schema))]]
    [:h2 "Edit"]
    (hform/form-to [:post (path :schema-edit)]
                   (form/render-widgets forms/schema schema req)
                   (hform/submit-button "Edit"))
    [:h2 "Permissions"]
    [:h3 "Read permissions"]
    (hform/form-to [:post (path :schema-assign-users)]
                   (form/render-widgets assign-form
                                        (assoc schema :assigned
                                               (:assigned-read-users schema))
                                        req)
                   (hform/hidden-field "permission" "read")
                   (hform/submit-button "Assign"))
    [:h3 "Write permissions"]
    (hform/form-to [:post (path :schema-assign-users)]
                   (form/render-widgets assign-form
                                        (assoc schema :assigned
                                               (:assigned-write-users schema))
                                        req)
                   (hform/hidden-field "permission" "write")
                   (hform/submit-button "Assign"))
    [:h3 "Allowed schema connections"]
    (hform/form-to [:post (path :schema-assign-schemas)]
                   (form/render-widgets conn-form
                                        (assoc schema
                                               :connections
                                               (:assigned-schemas schema))
                                        req)
                   (hform/submit-button "Assign"))
    [:h2 "Attributes"]
    [:ul.schema-attributes
     (for [attr attrs]
       [:li
        (hform/form-to [:post (path :attribute-edit {:schema (:uuid schema)})]
                       (form/render-widgets forms/attr-form attr req)
                       (hform/hidden-field "schema" (:uuid schema))
                       (hform/submit-button "Save"))
        (hform/form-to [:delete (path :attribute-delete attr)]
                       (anti-forgery-field)
                       (hform/hidden-field "schema" (:uuid schema))
                       (view/delete-btn)
                       [:label "Created at "
                        [:em (prettify-dt (:created_at attr))]])])]
    [:h3 "New attribute"]
    (hform/form-to [:post (path :attribute-new)]
                   (form/render-widgets forms/attr-form {} req)
                   (hform/hidden-field "schema" (:uuid schema))
                   (hform/submit-button "Create"))
    [:h2 "Actions"]
    (hform/form-to {:class "inline"}
                   [:delete (path :schema-delete schema)]
                   (anti-forgery-field)
                   (view/delete-btn))]))
