 (ns gpckwlf.app
   "Main application."
-  (:require [seesaw core]
+  (:require [seesaw bind core]
             [gpckwlf seesaw]))
+
+(declare add-tile delete-tile site-tile)
+
+(def tile-width 100)
+(def tile-height 100)
+
+(def tiles (atom (sorted-map)))
+
+(defn add-tile
+  ([tag]
+     (swap! tiles assoc tag (site-tile tag))))
+
+(defn delete-tile
+  ([tag]
+     (swap! tiles dissoc tag)))
+
+(defn add-form
+  ([]
+     (seesaw.core/grid-panel
+      :columns 2
+      :items ["Tag" (seesaw.core/text :id :tag)
+              "Username" (seesaw.core/text :id :username)
+              "Password" (seesaw.core/password :echo-char \*
+                                               :id :password)])))
+
+(defn add-dialog
+  ([]
+     (let [form (add-form)]
+       (seesaw.core/dialog
+        :content form
+        :option-type :ok-cancel
+        :type :question
+        :success-fn (fn [e] (add-tile (seesaw.core/select form [:#items
+                                                               :#tag])))))))
+
+(defn add-window
+  ([]
+     (seesaw.core/frame
+      :title "Add site"
+      :content (add-dialog)
+      :resizable? false
+      :on-close :dispose)))
+
+(defn add-popup
+  ([e]
+     (-> (add-window)
+         seesaw.core/pack!
+         seesaw.core/show!)))
+
+(def add-button
+  (seesaw.core/button :text "+"
+                      :listen [:action add-popup]))
+
+(defn site-tile
+  "Returns a new site tile with the given tag."
+  ([tag] {:pre (string? tag)}
+     (seesaw.core/vertical-panel
+      :items [(seesaw.core/horizontal-panel
+               :items [tag
+                       (seesaw.core/button
+                        :text "X"
+                        :listen [:action (fn [e] (delete-tile tag))])])]
+      :height tile-height
+      :width  tile-width)))
 
 (def login-username
   (seesaw.core/text :id :username))

(seesaw.core/listen exit-button
                    :action (fn [e] (System/exit 0)))

(def main-window
  (seesaw.core/frame
   :title "gpckwlf"

  (seesaw.core/scrollable
   (gpckwlf.seesaw/wrap-panel
    :align :left
    :items (concat (vals @tiles)
                   [add-button])
;    :items
;    ["These" "will" "be" "tiles." "Look" "how" "they" "rearrange"
;     "themselves." "Isn't" "that" "cool?"]
    )
   :hscroll :never
   :vscroll :as-needed))

