 (ns topiq.view
   (:require [topiq.db :refer [get-topiq get-topiqs get-arguments vote-count
-                                        add-topiq add-argument add-vote]]
+                                        add-topiq add-argument add-vote url-regexp]]
             [topiq.plugins :refer [render-content replace-hashtags]]
             [kioo.om :refer [html-content content after set-attr do-> substitute
                              listen prepend append html remove-class add-class]]

    (if (= "Not logged in" username)
      (js/alert "Please login or register.")
      (try
        (if selected-topiq
          (add-argument stage username selected-topiq (dom/value (dom/by-id "general-input-form")))
          (add-topiq stage username (dom/value (dom/by-id "general-input-form"))))
        (dom/set-value! (dom/by-id "general-input-form") "")
        (catch js/Object e
            (js/alert e))))))

  {[:.topiq-text] (html-content
                   (let [topiq (get-topiq (om/get-state owner :selected-topiq) @db)
                         text (replace-hashtags (:title topiq))]
                     (reduce
                      #(clojure.string/replace %1 %2 (str "<a href='" %2 "' target='_blank'> link </a>"))
                      text
                      (map first (re-seq url-regexp text)))))
   [:.topiq-author] (content (:author (get-topiq (om/get-state owner :selected-topiq) @db)))
   [:.topiq-ts] (content (compute-time-diff (:ts (get-topiq (om/get-state owner :selected-topiq) @db))))
   [:#back-btn] (listen :on-click #(om/set-state! owner :selected-topiq nil))
