   (:use [seesaw.mig])
   (:use [seesaw.widgets.log-window])
   (:require [clojure.string :refer [blank?]])
-  (:require [clojure.core.async :refer [chan alts!! timeout thread]])
+  (:require [clojure.core.async :refer [chan >!! <!! <! >! alts!! alts! timeout thread put! go go-loop close!]])
   )
-; (require '[clojure.core.async :refer [chan alts!! alts! timeout thread put! go-loop]])
-; (require '[clojure.string :refer [blank?]])
+
+(require '[clojure.core.async :refer [chan >!! <!! <! >! alts!! alts! timeout thread put! go go-loop close!]])
 
 (defn build-avatars-tab []
   (let [bg-stamp-pos (button-group)

   :content (label "hello") ;; TODO replace label => (build-content)
   ))

(defn -main [& args]
  (invoke-later
   (->

                                               :columns 80
                                               ))
                       "span"]])]

    (defn stop-listening [active-chan comm-chan log-widget te-name]
      (log log-widget "Stopping listening\n")
      (go
       (if (<! active-chan)
         (do
           (if-let [ c (<! comm-chan)]
             (do
               (log log-widget "closed keys channel from comm-chanel\n")
               (close! c)
               (>! active-chan false)
               )
             )
           (log log-widget (str "SENDING " (text te-name) "\n"))
           )
         (log log-widget "read false from active chan")
         ))
      )
    (defn start-listening [active-chan comm-chan log-widget te-name]
      (log log-widget "Starting listening\n")
      (go
       (>! comm-chan (chan 1))
       (>! active-chan true)
       (loop []
         (let [keys-chan (<! comm-chan)]
           (>! comm-chan keys-chan)
           (let [[v ch] (alts! [keys-chan (timeout 1000)])]
             (if (not (blank? v))
               (recur)
               (do
                 (log log-widget "timeout reached\n")
                 (stop-listening active-chan comm-chan log-widget te-name))
               ))
           ))))

    ;; It is important in this scheme to return value to the channel as soon as it had been read:
    ;; so that others interested can read back

    (let [te-name (select form [:#username])
          lb-log (select form [:#log])
          active-chan (chan 1)
          comm-chan (chan 1)]
      (listen te-name
              :focus-gained (fn [e]
                              (put! active-chan false)
                              )

              :document (fn [e]
                          (go
                           (if (<! active-chan)
                             (do
                               (>! active-chan true)
                               (log lb-log "in active mode, sending keypress\n")
                               (let [keys-chan (<! comm-chan)]
                                 (>! comm-chan keys-chan)
                                 (>! keys-chan "1")
                                 ))
                             (do
                               (log lb-log "in not-active mode, initiating listening\n")
                               (start-listening active-chan comm-chan lb-log te-name))
                             ))
                          )
              :focus-lost (fn [e]
                            (log lb-log "focus lost, stopping\n")
                            (stop-listening active-chan comm-chan lb-log te-name)
                            )
              )

      )
    form)
  )

(defn build-posts-tab []
  (label "Posts"))
