             [cine.core :as cine])
   (:gen-class))
 
+(defn http-get [url]
+  (let [c (a/chan)]
+    (http/get url #(a/put! c %))
+    c))
+
 (defn add-3d-info [mov]
   (if (.contains (str/lower-case (:name mov)) "3d")
     (assoc mov :3d true)

  [string x]
  (str/replace string x ""))

(defn list-movies []
  (->> (cine/mapify-current-movies)
       (map add-3d-info)
       (map #(assoc % :name (-> (:name %) 
                                (strip-x "(3D)")

                                     (str/replace "  " " ")
                                     (str/replace " " "+"))))))

(defn list-current-movies []
  (list-movies (cine/mapify-current-movies)))

(defn list-upcoming-movies []
  (list-movies (cine/mapify-upcoming-movies)))

(defn get-imdb-info [mov-name] 
  (http-get (str "http://www.omdbapi.com/?t=" mov-name)))

(defn current-movie-list-handler [req]
  {:status 200
   :headers {"Content-type" "text"}
   :body (let [mov-list-chan (map #(assoc % :imdb-chan (get-imdb-info (:imdb-name %))) (list-current-movies))
               mov-list (map #(do 
                                (assoc % :imdb-info (cheshire/parse-string (:body (<!! (:imdb-chan %)))))
                                (dissoc % :imdb-chan)) mov-list-chan)] 
           (cheshire/generate-string mov-list))})


(defn upcoming-movie-list-handler [req]
  {:status 200
   :headers {"Content-type" "text"}
   :body (cheshire/generate-string (let [mov-list (list-upcoming-movies)] 
                                     (assoc mov-list :imdb-info (get-imdb-info mov-list))))})

(defn cine-schedule-handler [req]
  {:status 200
