             [com.lemonodor.gflags :as gflags]
             [com.lemonodor.xio :as xio])
   (:import (java.awt Graphics)
+           (java.awt.image BufferedImage)
            (java.net Socket)
            (javax.swing JFrame JPanel))
   (:gen-class))


(gflags/define-boolean "file"
  false
  "Read an input file.")

(gflags/define-float "fps"
  30.0

  "Turns on latency reduction.")


(defn display-image
  "Renders an image to a view."
  [^JPanel view ^BufferedImage image]
  (.drawImage (.getGraphics view) image 0 0 view))


(def drone-hostname "192.168.1.1")
(def drone-video-port 5555)


(defn drone-video-input-stream
  "Connects to a drone's video feed.  Returns an InputStream."
  [hostname]
  (println "Connecting to drone at" hostname)
  (let [is (.getInputStream (Socket. hostname drone-video-port))]
    (println "Connected.")
    is))


(defn get-input-stream
  "Returns an InputStream of video data.

   Depending on the command-line flags specified, either connects to a
   drone or opens a file."
  [args]
  (if (gflags/flags :file)
    (if (seq args)
      (io/input-stream (first args))
      ;; If we weren't given a path, read from stdin.
      System/in)
    (drone-video-input-stream (if (seq args) (first args) drone-hostname))))


(defn -main [& args]
  (let [args (gflags/parse-flags (cons nil args))
        ^JFrame window (JFrame. "Drone video")
        ^JPanel view (JPanel.)
        frame-queue (pave/make-frame-queue
                     :reduce-latency? (gflags/flags :reduce-latency))
        decoder (decode/decoder)
        is (get-input-stream args)
        display-thread-done (promise)
        frame-count (atom 0)
        frame-delay (if (gflags/flags :file)
                      (/ 1000.0 (gflags/flags :fps))
