             [clj-chrome-devtools.commands.page :as page]
             [clj-chrome-devtools.commands.input :as input]
             [clj-chrome-devtools.commands.runtime :as runtime]
+            [clj-chrome-devtools.commands.network :as network]
             [clj-chrome-devtools.events :as events]
             [clj-chrome-devtools.impl.connection :as connection]
             [clojure.core.async :as async :refer [go-loop go <!! <!]]

         :data decode
         (java.io.ByteArrayInputStream.)
         (io/copy (io/file "screenshot.png"))))))

     (some #(when (= (:name %) "innerText")
              (-> % :value :value))
           (:result (runtime/get-properties c {:object-id object-id :own-properties false}))))))



(defn file-download
  "Run the given interaction-fn which will interact with the page and cause a file download.
  Monitors network activity to receive a file where the request matches the given URL pattern.
  Returns a map describing the downloaded file and also has the content."
  ([url-pattern interaction-fn]
   (file-download @current-automation url-pattern interaction-fn))
  ([{c :connection :as ctx} url-pattern interaction-fn]
   (network/enable c {})
   (page/enable c {})
   (let [response-ch (events/listen c :network :response-received)
         timeout-ch (async/timeout (:navigate *wait-ms*))
         file-request
         (go-loop [[v ch] (async/alts! [response-ch timeout-ch])]
           (cond
             (= ch timeout-ch)
             nil

             (->> v :params :response :url (re-find url-pattern))
             (:params v)

             :default
             (recur (async/alts! [response-ch timeout-ch]))))]
     (interaction-fn)
     (let [file (<!! file-request)]
       (events/unlisten c :network :response-received response-ch)
       (when file
         ;; PENDING: get-response-body does not work for downloaded files
         (:response file))))))

(defn screenshot
  ([] (screenshot @current-automation))
