 (ns bakyeono.litedocx.wpml
   "WordProcessingML templates & snippets used in litedocx."
   (:require [clojure.string :as str])
-  (:use [clojure.data.xml :only [element]])
+  (:require [clojure.data.xml :as xml])
   (:use [bakyeono.litedocx.util]))
 
+;;; e -> clojure.data.xml/element
+;;; Since clojure.data.xml/element is used so much,
+;;; use 'node' as an abbreviation of it.
+;
+(def ^:const ^{:private true} node clojure.data.xml/element)
+
+;;; Constants
 (def ^:const ^{:private true} content-types-xml-xmlns
   {:xmlns "http://schemas.openxmlformats.org/package/2006/content-types"})
 
 (def ^:const ^{:private true} relationships-xmlns 
   {:xmlns "http://schemas.openxmlformats.org/package/2006/relationships"})
-
 (def ^:const ^{:private true} doc-props-app-xml-xmlns
   {:xmlns:vt "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"
    :xmlns:properties "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"})

  {:tag :properties:Properties
   :attrs doc-props-app-xml-xmlns
   :content (remove-nil
              (map when-v-tag
                   [application app-version]
                   [:properties:Application :properties:AppVersion]))})


   :xmlns:w "http://schemas.openxmlformats.org/wordprocessingml/2006/main"})

(def ^:const ^{:private true} default-content-types
  [(node :Default
         {:Extension "rels"
          :ContentType "application/vnd.openxmlformats-package.relationships+xml"})
   (node :Override
         {:ContentType "application/vnd.openxmlformats-officedocument.extended-properties+xml"
          :PartName "/docProps/app.xml"})
   (node :Override
         {:ContentType "application/vnd.openxmlformats-package.core-properties+xml"
          :PartName "/docProps/core.xml"})
   (node :Override
         {:ContentType "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"
          :PartName "/word/document.xml"})])

(def ^:const ^{:private true} default-styles
  [(node :w:style
         {:w:type "paragraph" :w:styleId "a" :w:default "true"}
         [(node :w:name {:w:val "Normal"})
          (node :w:qFormat)
          (node :w:pPr
                {}
                [(node :w:widowControl {:w:val "false"})
                 (node :w:wordWrap {:w:val "false"})
                 (node :w:autoSpaceDE {:w:val "false"})
                 (node :w:autoSpaceDN {:w:val "false"})
                 (node :w:jc {:w:val "both"})])])
   (node :w:style
         {:w:type "character" :w:styleId "a0" :w:default "true"}
         [(node :w:name {:w:val "Default Paragraph Font"})
          (node :w:uiPriority {:w:val "1"})
          (node :w:semiHidden)
          (node :w:unhideWhenUsed)])
   (node :w:style
         {:w:type "table" :w:styleId "a1" :w:default "true"}
         [(node :w:name {:w:val "Normal Table"})
          (node :w:uiPriority {:w:val "99"})
          (node :w:semiHidden)
          (node :w:unhideWhenUsed)
          (node :w:qFormat)
          (node :w:tblPr
                {}
                [(node :w:tblInd {:w:w "0" :w:type "dxa"})
                 (node :w:tblCellMar
                       {}
                       [(node :w:top {:w:w "0" :w:type "dxa"})
                        (node :w:left {:w:w "108" :w:type "dxa"})
                        (node :w:bottom {:w:w "0" :w:type "dxa"})
                        (node :w:right {:w:w "108" :w:type "dxa"})])])])
   (node :w:style
         {:w:type "numbering" :w:styleId "a2" :w:default "true"}
         [(node :w:name {:w:val "No List"})
          (node :w:uiPriority {:w:val "99"})
          (node :w:semiHidden)
          (node :w:unhideWhenUsed)])])

(defn when-v-node
  "Returns an XML tag node when the value exists."
  [v tag]
  (cond (nil? v) nil
        (= "" v) (node tag)
        true (node tag {} (str v))))

(defn when-v-kv
  "Returns a vector of [k v] when the value exists."
