              [obi :as layout.obi]]
             [catalog.web.layout :as layout]
             [cemerick.friend :as friend]
-            [clj-time.core :as time.core]
+            [clj-time
+             [core :as time.core]
+             [format :as time.format]]
             [clojure
              [edn :as edn]
              [string :as string]]

            [schema.core :as s]))

(defn- download-button
  ([href file-name]
   (download-button href file-name "Download"))
  ([href file-name label]
   [:a.btn.btn-default {:href href :role "button" :download file-name :aria-label label}
    [:span.glyphicon {:class "glyphicon-download" :aria-hidden "true"}] (str " " label)]))

(defn- download-url [& items]

(defmulti custom (fn [_ _ _ _ _ fmt _] fmt))

(defmethod custom :pdf [request year issue query customer fmt items]
  (let [filename (file-name :catalog-custom customer)
        temp-file (java.io.File/createTempFile filename ".pdf")]
    (-> items
        edn/read-string
        vubis/order
        (layout.fop/document :custom year issue nil nil :query query :customer customer)
        (layout.fop/generate-pdf! temp-file))
    (pdf-response temp-file filename)))

(defmethod custom :grossdruck [request year issue query customer fmt items]
  (let [filename (file-name :catalog-custom customer)
        temp-file (java.io.File/createTempFile filename ".pdf")]
    (-> items
        edn/read-string
        vubis/order
        (layout.fop/document :custom-grossdruck year issue nil nil :query query :customer customer)
        (layout.fop/generate-pdf! temp-file))
    (pdf-response temp-file filename)))

(defmethod custom :braille [request year issue query customer fmt items]
  (-> items
      edn/read-string
      vubis/order
      (layout.dtbook/dtbook {:query query :customer customer})
      (xml-response (str (file-name :catalog-custom customer) ".xml"))))

