 (defn search-stations
   "Call the station searcher with various filters"
   [system-name &
-   {:keys [callback commodity-id pad-size search-type search-range]
+   {:keys [callback
+           allegiance-id commodity-id economy-id government-id
+           has-blackmarket has-outfitting has-repairs has-shipyard
+           pad-size search-type search-range]
     :or {callback identity
-         commodity-id nil
-         pad-size "Small"
-         search-type "Station Selling" ; or "Station Buying"; req for commodity-id
+         pad-size :Small
+         search-type :selling ; req for commodity-id
          search-range "15"}}]
-  (let [request-body {:CurrentLocation system-name
-                      :Commodity (if commodity-id true false)
-                      :CommodityId commodity-id
+  (let [request-body {:Allegiance (exists? allegiance-id)
+                      :AllegianceId (arg-value allegiance-id)
+                      :Blackmarket (exists? has-blackmarket)
+                      :Commodity (exists? commodity-id)
+                      :CommodityId (arg-value commodity-id)
+                      :CurrentLocation system-name
+                      :Economy (exists? economy-id)
+                      :EconomyId (arg-value economy-id)
+                      :Government (exists? government-id)
+                      :GovernmentId (arg-value government-id)
+                      :Outfitting (exists? has-outfitting)
                       :PadSize pad-size
-                      :SearchType search-type
-                      :SearchRange search-range}]
+                      :Repairs (exists? has-repairs)
+                      :SearchRange search-range
+                      :SearchType (get search-types search-type)
+                      :Shipyard (exists? has-shipyard)}]
     (println request-body)
     (http/post 
       search-url
       {:headers {"Content-Type" "application/json"}
-       :body request-body}
+       :body (generate-string request-body)}
       (fn [{:keys [error body]}]
-        (println body)
         (if error
           (do 
             (log "! Error searching" error "Request:" request-body)

            (.contains station-name input)
            (.contains system-name input))))
      (get-stations))))

;;
;; API calls

(defn on-search
  "Packet handler for :search"
  [ch packet]
  (if-let [system (:system packet)]
    ; NB this also seems overly complicated
    (apply search-stations
           (flatten (conj [system] 
                          (calculate-packet-to-seq packet)
                          :callback #(to-client ch {:type :search-result
                                                    :result %}))))
    (client-error ch "Must specify system")))

;;
