(ns wanijo.instance.view.show
  (:require [hiccup.form :as hform]
            [hiccup.core :refer [h]]
            [ring.util.anti-forgery :refer [anti-forgery-field]]
            [wanijo.infrastructure.view :as view]
            [wanijo.infrastructure.routing :refer [path]]
            [wanijo.infrastructure.time :refer [prettify-dt]]
            [wanijo.instance.view.view :as view-instance]
            [wanijo.tag.view :as view-tag]
            [markdown.core :as md]
            [wanijo.visualisation.viz :as viz]))

(defn links-table [heading direction instance]
  [:section.links-in
   [:h2 (h heading)]
   [:table
    [:thead
     [:tr
      [:th "Name"]
      [:th "Instance"]
      [:th "Schema"]
      [:th "Created"]]]
    [:tbody
     (for [{:keys [link source schema target] :as row} (direction instance)
           :let [name (:name link)
                 empty (empty? name)
                 name (if empty [:i "empty"] (h name))
                 other (or source target)]]
       [:tr
        [:td name
         (view-instance/tags-for-search row)]
        [:td [:a {:href (path :instance-show other)}
              (h (:name other))]]
        [:td [:a {:href (path :instance-list
                              {:schema-uuid (:uuid schema)})}
              (h (:name schema))]]
        [:td (prettify-dt (:created_at link))]])]]])

(defn show [instance schemas req]
  (view/layout
    :request req
    :content
    [[:h1
      (if (:starred instance)
        (hform/form-to {:class "inline"}
                       [:delete (path :instance-mark-starred instance)]
                       (anti-forgery-field)
                       (hform/submit-button "★"))
        (hform/form-to {:class "inline"}
                       [:post (path :instance-mark-starred instance)]
                       (anti-forgery-field)
                       (hform/submit-button "☆")))
      "&nbsp;"
      (h (-> instance :schema :name))
      "&nbsp;"
      [:small (h (:name instance))]]
     [:p [:small
          [:a {:href (path :instance-list
                           {:schema-uuid (-> instance :schema :uuid)})}
           "Back to List"]
          " | "
          [:a {:href (path :instance-edit-form instance)}
           "Edit Instance"]
          " | "
          [:a {:href (path :vis-explore {:instance-uuid (:uuid instance)})}
           "Explore from here"]
          " | "
          [:a {:href "#quick-edits"}
           "Quick edits"]]]
     (when (seq (:tags instance))
       [:section.tags
        (view-tag/tag-list (:tags instance))])
     (when (seq (:properties instance))
       [:section.properties
        [:h2 "Properties"]
        (for [prop (:properties instance)
              :let [attr (:attribute prop)
                    type (:type attr)
                    render-fn (case type
                                "date" #(str (prettify-dt %))
                                "markdown" md/md-to-html-string
                                #(str "<p>" % "</p>"))]]
          (list [:h3 (h (:name attr))]
                [:div {:class (str "instance-content "
                                   "attr-type-" type)}
                 (render-fn (:value prop))]))])
     (when (or (seq (:links-out instance))
               (seq (:links-in instance)))
       [:section.visualisation
        [:h2 "Visualisation"]
        [:p (viz/single-instance instance)]])
     (when (seq (:links-out instance))
       (links-table "Outgoing links" :links-out instance))
     (when (seq (:links-in instance))
       (links-table "Incoming links" :links-in instance))
     [:section.quick-edits
      [:h2 {:id "quick-edits"} "Quick edits"]
      [:section.link-instance
       [:h3 "Link Instance with Instance of Schema..."]
       [:ul
        (for [schema schemas]
          [:li
           [:a {:href (path :instance-link-selection
                            {:uuid (:uuid instance)
                             :schema-uuid (:uuid schema)})}
            (h (:name schema))]])]
       [:a {:href (path :instance-bulk-link-selection instance)}
        "Bulk create links"]]
      [:section.tag-instance
       [:h3 "Add or create Tags"]
       (view-tag/new-tag-form instance)]]]))
