 (defn check-results-window [tournament-id]
   (fn [& _] 
     (-> 
-      (ui/dialog
+      (swing/dialog
         :type :info
         :content (clojure.string/join \newline (cons "Seuraavien pöytien tulos puuttuu:" (watcher/get-missing-results tournament-id))))
-      ui/pack!
-      ui/show!)))
+      swing/pack!
+      swing/show!)))
 
 (defn create-tab [tournament-id]
-  (let [pairings-combo (ui/combobox :model []
+  (let [pairings-combo (swing/combobox :model []
                                     :id (pairings-combo-id tournament-id))
-        results-combo (ui/combobox :model []
+        results-combo (swing/combobox :model []
                                    :id (results-combo-id tournament-id))
-        info (ui/label "")
-        info! (fn [& args] (ui/text! info (apply str args)))
+        info (swing/label "")
+        info! (fn [& args] (swing/text! info (apply str args)))
         tab (mig/mig-panel :constraints ["fill, wrap 3" 
                                          "[100!][100!][200!]" 
                                          ""]
                            :id (tab-id tournament-id)
-                           :items [[(ui/label "Turnaus")]
-                                   [(ui/label "")]
-                                   [(ui/button :text "Lähetä"
-                                               :listen [:action (upload uploader/upload-tournament! (info! "Turnaus lähetetty") tournament-id)]
-                                               :id (tournament-button-id tournament-id)
-                                               :enabled? false)]
-                                   
-                                   [(ui/label "Tiimit")]
-                                   [(ui/label "")]
-                                   [(ui/button :text "Lähetä"
-                                               :listen [:action (upload uploader/upload-teams! (info! "Tiimit lähetetty") tournament-id)]
-                                               :id (teams-button-id tournament-id)
-                                               :enabled? false)]
-                                   
-                                   [(ui/label "Seatingit")]
-                                   [(ui/label "")]
-                                   [(ui/button :text "Lähetä"
-                                               :listen [:action (upload uploader/upload-seatings! (info! "Seatingit lähetetty") tournament-id)]
-                                               :id (seatings-button-id tournament-id)
-                                               :enabled? false)]
-                                   
-                                   [(ui/label "Pairingit")]
+                           :items [[(swing/label "Turnaus")]
+                                   [(swing/label "")]
+                                   [(swing/button :text "Lähetä"
+                                                  :listen [:action (upload uploader/upload-tournament! (info! "Turnaus lähetetty") tournament-id)]
+                                                  :id (tournament-button-id tournament-id)
+                                                  :enabled? false)]
+                                   
+                                   [(swing/label "Tiimit")]
+                                   [(swing/label "")]
+                                   [(swing/button :text "Lähetä"
+                                                  :listen [:action (upload uploader/upload-teams! (info! "Tiimit lähetetty") tournament-id)]
+                                                  :id (teams-button-id tournament-id)
+                                                  :enabled? false)]
+                                   
+                                   [(swing/label "Seatingit")]
+                                   [(swing/label "")]
+                                   [(swing/button :text "Lähetä"
+                                                  :listen [:action (upload uploader/upload-seatings! (info! "Seatingit lähetetty") tournament-id)]
+                                                  :id (seatings-button-id tournament-id)
+                                                  :enabled? false)]
+                                   
+                                   [(swing/label "Pairingit")]
                                    [pairings-combo]
-                                   [(ui/button :text "Lähetä"
-                                               :listen [:action (upload uploader/upload-pairings! 
-                                                                        (info! "Pairingit " (ui/selection pairings-combo) " lähetetty") 
-                                                                        tournament-id 
-                                                                        (ui/selection pairings-combo))]
-                                               :id (pairings-button-id tournament-id)
-                                               :enabled? false)]
-                                   
-                                   [(ui/label "Tulokset")]
+                                   [(swing/button :text "Lähetä"
+                                                  :listen [:action (upload uploader/upload-pairings! 
+                                                                           (info! "Pairingit " (swing/selection pairings-combo) " lähetetty") 
+                                                                           tournament-id 
+                                                                           (swing/selection pairings-combo))]
+                                                  :id (pairings-button-id tournament-id)
+                                                  :enabled? false)]
+                                   
+                                   [(swing/label "Tulokset")]
                                    [results-combo]
-                                   [(ui/vertical-panel :items [(ui/button :text "Lähetä"
-                                                                          :listen [:action (upload uploader/upload-results! 
-                                                                                                   (info! "Tulokset " (ui/selection pairings-combo) " lähetetty")
-                                                                                                   tournament-id 
-                                                                                                   (ui/selection results-combo))]
-                                                                          :id (results-button-id tournament-id)
-                                                                          :enabled? false)
-                                                               (ui/button :text "Julkaise"
-                                                                          :listen [:action (upload uploader/publish-results! 
-                                                                                                   (info! "Tulokset " (ui/selection pairings-combo) " julkaistu")
-                                                                                                   tournament-id 
-                                                                                                   (ui/selection results-combo))]
-                                                                          :id (publish-button-id tournament-id)
-                                                                          :enabled? false)
-                                                               (ui/button :text "Tarkista"
-                                                                          :listen [:action (check-results-window tournament-id)]
-                                                                          :id (check-button-id tournament-id)
-                                                                          :enabled? false)])]
-                                   
-                                   [(ui/label "Resetoi")]
-                                   [(ui/label "")]
-                                   [(ui/button :text "Resetoi"
-                                               :listen [:action (upload uploader/reset-tournament! (info! "Turnaus resetoitu") tournament-id)]
-                                               :id (reset-button-id tournament-id)
-                                               :enabled? false)]
+                                   [(swing/vertical-panel :items [(swing/button :text "Lähetä"
+                                                                                :listen [:action (upload uploader/upload-results! 
+                                                                                                         (info! "Tulokset " (swing/selection pairings-combo) " lähetetty")
+                                                                                                         tournament-id 
+                                                                                                         (swing/selection results-combo))]
+                                                                                :id (results-button-id tournament-id)
+                                                                                :enabled? false)
+                                                                  (swing/button :text "Julkaise"
+                                                                                :listen [:action (upload uploader/publish-results! 
+                                                                                                         (info! "Tulokset " (swing/selection pairings-combo) " julkaistu")
+                                                                                                         tournament-id 
+                                                                                                         (swing/selection results-combo))]
+                                                                                :id (publish-button-id tournament-id)
+                                                                                :enabled? false)
+                                                                  (swing/button :text "Tarkista"
+                                                                                :listen [:action (check-results-window tournament-id)]
+                                                                                :id (check-button-id tournament-id)
+                                                                                :enabled? false)])]
+                                   
+                                   [(swing/label "Resetoi")]
+                                   [(swing/label "")]
+                                   [(swing/button :text "Resetoi"
+                                                  :listen [:action (upload uploader/reset-tournament! (info! "Turnaus resetoitu") tournament-id)]
+                                                  :id (reset-button-id tournament-id)
+                                                  :enabled? false)]
                                    
                                    [info "span 3"]])]
     (disable-buttons tournament-id)

  (keyword (str "resultscombo" tournament-id)))

(defn disable-buttons [tournament-id]
  (doseq [btn (ui/select (ui/to-root tabbed-pane) [(id->selector (tab-id tournament-id)) :JButton])]
    (ui/config! btn :enabled? false))
  (-> (ui/select (ui/to-root tabbed-pane) [(id->selector (reset-button-id tournament-id))])
    (ui/config! :enabled? true)))

(defn update-pairings-combo [tournament-id]
  (let [combo (ui/select (ui/to-root tabbed-pane) [(id->selector (pairings-combo-id tournament-id))])
        cnt (watcher/get-pairings-count tournament-id)]
    (ui/config! combo :model (range 1 (inc cnt)))
    (ui/selection! combo (long cnt))))

(defn update-results-combo [tournament-id]
  (let [combo (ui/select (ui/to-root tabbed-pane) [(id->selector (results-combo-id tournament-id))])
        cnt (watcher/get-results-count tournament-id)]
    (ui/config! combo :model (range 1 (inc cnt)))
    (ui/selection! combo (long cnt))))

(defn enable-buttons [tournament-id]
  (let [root (ui/to-root tabbed-pane)] 
    (when (watcher/get-tournament tournament-id)
      (-> (ui/select root [(id->selector (tournament-button-id tournament-id))])
        (ui/config! :enabled? true)))
    (when (watcher/get-teams tournament-id)
      (-> (ui/select root [(id->selector (teams-button-id tournament-id))])
        (ui/config! :enabled? true)))
    (when (watcher/get-seatings tournament-id)
      (-> (ui/select root [(id->selector (seatings-button-id tournament-id))])
        (ui/config! :enabled? true)))
    (when (pos? (watcher/get-pairings-count tournament-id))
      (-> (ui/select root [(id->selector (pairings-button-id tournament-id))])
        (ui/config! :enabled? true)))
    (when (pos? (watcher/get-results-count tournament-id))
      (-> (ui/select root [(id->selector (results-button-id tournament-id))])
        (ui/config! :enabled? true))
      (-> (ui/select root [(id->selector (publish-button-id tournament-id))])
        (ui/config! :enabled? true)))
    (-> (ui/select root [(id->selector (reset-button-id tournament-id))])
      (ui/config! :enabled? true))
    (-> (ui/select root [(id->selector (check-button-id tournament-id))])
      (ui/config! :enabled? true))))

(defmacro upload [action done tournament-id & params]
  `(fn [_#]

  (enable-buttons id)) 

(def about-window
  (swing/frame
    :on-close :dispose
    :title "About MTG pairings"
    :size [100 :by 100]
    :content (swing/border-panel 
               :vgap 5
               :hgap 5
               :border ""
               :north "This is the WER database sniffer and result uploader"
               :south (swing/button :text "Close" :listen [:action (fn [event] (-> about-window swing/hide!))]))))
  
(defn about-handler [event]
  (-> about-window swing/pack! swing/show!))

(defn exit-handler [event]
  (System/exit 0))

(defn apikey-window []
  (swing/dialog
    :option-type :ok-cancel
    :title "API key"
    :size [300 :by 100]
    :content (swing/text :id :key
                      :text (property :api-key))
    :success-fn (fn [p] (let [value (swing/text (swing/select (swing/to-root p) [:#key]))]
                          (set-property! :api-key value)))))

(defn apikey-handler [& args]
  (-> (apikey-window) swing/pack! swing/show!))

(defn new-version-window []
  (swing/dialog
    :type :warning
    :content "Pairings-ohjelmasta on uusi versio. Lataa uusi versio painamalla OK."
    :success-fn (fn [p] 
