       (str/replace #"\n" " ")
       (str/replace #" +" " ")))
 
-; Should convert hex string to hue for hsl color. Use string matching as proof of concept for now
+;; Should convert hex string to hue for hsl color. Use string matching as proof of concept for now
 (defn color-to-hue [s]
-  (cond
-    (= s "#FF0000")
-    "0"
-
-    (= s "#00FF00")
-    "120"
-
-    (= s "#0000FF")
-    "240"
-  )
-)
+  (case s
+    "#FF0000" "0"
+    "#00FF00" "120"
+    "#0000FF" "240"))
 
 (defn shape-aggregagation-extra-cols-sql [popup table-name prefix postfix]
-  (if
-    (= (count popup) 0)
+  (if (= (count popup) 0)
     ""
-    (str prefix (clojure.string/join "," (map (fn [popupObj] (str table-name "." (get popupObj "column"))) popup)) postfix)
-  )
-)
+    (str prefix
+         (clojure.string/join "," (map (fn [{:strs [column]}]
+                                         (str table-name "." column))
+                                       popup))
+         postfix)))
 
 (defn popup-and-label-cols [popup-cols label-col]
   (cond
-    (and (boolean popup-cols) (boolean label-col))
-    (distinct (conj popup-cols {"column" label-col}))
-
-    (boolean popup-cols)
-    popup-cols
-
-    (boolean label-col)
-    [{"column" label-col}]
-
-    :else
-    []
-  )
-)
-
-(defn shape-aggregation-sql [columns table-name geom-column popup-columns point-color-column where-clause current-layer layer-index tenant-conn]
-  (let [
-    {:keys [table-name columns]} (dataset-by-id tenant-conn {:id (get current-layer "aggregationDataset")})
-    point-table-name table-name
-    point-columns columns
-    {:keys [table-name columns]} (dataset-by-id tenant-conn {:id (get current-layer "datasetId")})
-    shape-table-name table-name
-    shape-columns columns
-    aggregation-method (get current-layer "aggregationMethod" "avg")
-    date-column-set (reduce (fn [m c]
+    (and (boolean popup-cols)
+         (boolean label-col)) (distinct (conj popup-cols {"column" label-col}))
+
+    (boolean popup-cols) popup-cols
+
+    (boolean label-col) [{"column" label-col}]
+
+    :else []))
+
+(defn shape-aggregation-sql
+  [columns table-name geom-column popup-columns point-color-column where-clause current-layer layer-index tenant-conn]
+  (let [{:keys [table-name columns]} (dataset-by-id tenant-conn {:id (get current-layer "aggregationDataset")})
+        point-table-name table-name
+        point-columns columns
+        {:keys [table-name columns]} (dataset-by-id tenant-conn {:id (get current-layer "datasetId")})
+        shape-table-name table-name
+        shape-columns columns
+        aggregation-method (get current-layer "aggregationMethod" "avg")
+        date-column-set (reduce (fn [m c]
                                   (if (= "date" (get c "type"))
                                     (conj m (get c "columnName"))
                                     m)) #{} point-columns)

                value)
              (pr-str color)))))

(defn point-cartocss [point-size point-color-column point-color-mapping layer-index]
  (format "#s {
              marker-allow-overlap: true;
              marker-fill-opacity: 0.8;
              marker-fill: %s;
              marker-line-color: #fff;
              marker-width: %s;
              %s
           }"
          (layer-point-color layer-index)
          (marker-width point-size)
          (str/join " " (point-color-css point-color-column point-color-mapping))))

(defn shape-cartocss [layer-index layer]
  (cond-> (format "#s {
              polygon-opacity: 0.8;
              polygon-fill: transparent;
              line-width: 2;
              line-color: rgba(0,0,0,0.3);
           }
           "
          (layer-point-color layer-index))
          (get layer "shapeLabelColumn")
          (str (format "#s::labels {
            text-name: [%s];
            text-face-name: 'DejaVu Sans Book';
            text-size: 10;
            text-fill: #000;
          }"
          (get layer "shapeLabelColumn"))))
)

(defn shape-aggregation-cartocss [layer-index layer]
  (cond-> (format "#s {
              polygon-opacity: 0.8;
              polygon-fill: [shapefill];
              line-width: 0.5;
              line-color: rgba(0,0,0,0.3);
           }
           "
          (layer-point-color layer-index))
          (get layer "shapeLabelColumn")
          (str (format "#s::labels {
            text-name: [%s];
            text-face-name: 'DejaVu Sans Book';
            text-size: 10;
            text-fill: #000;
          }"
          (get layer "shapeLabelColumn"))))
)

(defn cartocss [layer layer-index metadata-array]
  (cond
    (and (get layer "aggregationDataset")(get layer "aggregationColumn")(get layer "aggregationGeomColumn"))
    (shape-aggregation-cartocss layer-index layer)

    (= (get layer "layerType") "geo-shape")
    (shape-cartocss layer-index layer)

    :else
    (point-cartocss (get layer "pointSize") (get layer "pointColorColumn") (get (nth metadata-array layer-index) "pointColorMapping") layer-index)
  )
)

(defn trim-css [s]
  (-> s

(hugsql/def-db-fns "akvo/lumen/lib/dataset.sql")

(defn layer-point-color [layer-index]
  ({0 "#2ca409"
    1 "#096ba4"
    2 "#7a09a4"
    3 "#a4092a"
    4 "#fff721"} layer-index "#000000"))

(defn marker-width [point-size]
  (let [point-size (if (string? point-size)
