         ring.middleware.resource
         ring.middleware.file-info
         ring.util.response
-        midje.sweet)
+        midje.sweet
+        [asid.error-flow :only [fail->]])
 
   (:require [compojure.route :as route]
             [compojure.handler :as handler]

        (let [id-seed (slurp body)]
          (if (empty? id-seed)
            (ar/bad-request "Identity seed not supplied")
            (-> (w/new-wallet id-seed)
                (wr/save repo)
                ar/created))))

  (GET ["/:id", :id aid/grammar] [id :as {accepts :accepts}]
       (let [wallet (wr/get-wallet id repo)
             content-handlers {"text/html" (fn [_]
                                             (File. "resources/public/wallet/index.html"))
              
                               "application/vnd.org.asidentity.wallet+json" ar/resource}

             handler (first (remove nil? (map content-handlers accepts)))]
         (handler wallet)))

  (POST ["/:id/bag", :id aid/grammar] [id key value]
        (if (or (empty? key)
                (empty? value))
          (ar/bad-request "Either key or value or both not supplied.")
          (-> (wr/get-wallet id repo)
              (w/add-data key value)
              (wr/save repo)
              ar/resource)))

  (POST ["/:id/trustpool", :id aid/grammar] [id :as {pool-doc :json-doc}]
        (let [name (get pool-doc "name")]

              (ar/created pool)))))

  (GET "/:walletid/trustpool/:poolid" [walletid poolid]
       (fail-> (wr/get-wallet walletid repo)
               (tpr/pool-from-wallet poolid)
               ar/resource))

  (POST "/:walletid/trustpool/:poolid" [walletid poolid :as {calling-card :json-doc}]
        (let [wallet (wr/get-wallet walletid repo)
              pool (tpr/pool-from-wallet wallet poolid)]
          (fail-> (cc/new-calling-card (:identity calling-card)
                                       (:uri calling-card))
                  (cc/submit wallet pool)
                  ccr/save
                  (cc/attach pool)
                  (ar/created "application/vnd.org.asidentity.calling-card+json"))))

  (route/not-found (File. "resources/public/not-found.html")))

