 
 (enable-console-print!)
 
-(def app-state (atom {:current-index [0]}))
+(def app-state (atom {:current-index [0]
+                      :repos []
+                      :registered-states []}))             
 
+;; cursors
 (defn current-index-cur []
   (om/ref-cursor (:current-index (om/root-cursor app-state))))
 
-(defn- repo-button [index repo]
-  (dom/button 
-    #js {:onClick
-           (fn [e] (om/update! (current-index-cur) [0] index))}
-    (get repo "full_name")))
-
+;; views
 (defn repo-list-view [state owner]
   (reify
     om/IRender
     (render [this]
-      (apply dom/div nil
-        (map-indexed repo-button state)))))  
+      (letfn [(repo-button [index repo]
+                (dom/button 
+                  #js {:onClick
+                         (fn [e] (om/update! (current-index-cur) [0] index))}
+                  (get repo "full_name")))]
+        (apply dom/div nil
+          (map-indexed repo-button (:repos @app-state)))))))  
 
 ;; {:auth - :repo -}
 (defn repo-config-panel [state owner]

  (reify  
    om/IInitState
    (init-state [_]
      {:me (js->clj (json/parse (.-cloc_me js/window)))
       :auth (.get (Cookies. js/document) "session_id")})

    om/IRenderState
    (render-state [this state]  
      (let [repos (vec (get-in state [:me "repos"]))]  
        (prn repos)
        (dom/div nil
          (om/build repo-list-view repos)
          (om/build repo-detail-view {:repos repos
                                      :auth (:auth state)}))))))                                       

(om/root widget app-state {:target (.getElementById js/document "content")})

;; {:repos - :auth -}
(defn repo-detail-view [state owner]
  (reify
    om/IRender
    (render [this]  
      (let [current-index (nth (om/observe owner (current-index-cur)) 0)
            repos (:repos @app-state)]
        (if (< current-index (count repos))
          (om/build repo-config-panel {:repo (nth repos current-index) 
                                       :auth (:auth state)})                              
