 
 (defmethod event-msg-handler :chsk/state
   [{:as ev-msg :keys [?data]}]
-  (if (= ?data {:first-open? true})
-    (log "Channel socket successfully established!")
+  (if (:first-open? ?data)
+    (do
+      (log "Channel socket successfully established!")
+      (log "Fetch initilize data from Tango server")
+      (chsk-send! [:event-manager/query [[:competition/name :competition/location]]]))
     (log (str "Channel socket state change: " ?data))))
 
 (defmethod event-msg-handler :chsk/recv
   [{:as ev-msg :keys [?data]}]
   (let [[topic payload] ?data]
-    ;(log (str "Push event from server: " ev-msg))
     (log (str "Push event from server: " topic))
     (when (= topic :event-manager/query-result)
-      (handle-query-result (second ?data)))))
+      (if (vector? payload)
+        (handle-query-result payload)
+        (handle-query-result (second ?data))))
+    (when (= topic :event-manager/transaction-result)
+      (chsk-send! [:event-manager/query [[:competition/name :competition/location]]]))
+    (log "Exit event-msg-handler")))
 
 (defmethod event-msg-handler :chsk/handshake
   [{:as ev-msg :keys [?data]}]
   (let [[?uid ?csrf-token ?handshake-data] ?data]
     (log (str "Handshake: " ?data))))
 
-(defmethod event-msg-handler :event-manager/query-result
-  [{:as ev-msg :keys [?data]}]
-  (log (str "Event Manager Query Result " ?data)))
-
 (defonce chsk-router
   (sente/start-chsk-router-loop! event-msg-handler* ch-chsk))
 
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; Import
+
+(defn on-file-read [e file-reader]
+  (let [result (.-result file-reader)]
+    (log "On file read : send :file/import")
+    (chsk-send! [:file/import {:content result}])))
+
+(defn on-click-import-file [e]
+  (log "Import clicked")
+  (let [file (.item (.. e -target -files) 0)
+        r (js/FileReader.)]
+    (set! (.-onload r) #(on-file-read % r))
+    (.readAsText r file)
+    ;(swap! app-state merge {:import-status :import-started})
+    ))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Read

  Object
  (render
   [this]
   (let [competitions (:app/competitions (om/props this))
         spage (:app/selected-page (om/props this))
         selected-competition (:app/selected-competition (om/props this))

  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Init DB
(defonce conn (d/create-conn uidb/schema))

(defn init-app []
  (d/transact! conn [{:db/id -1 :app/id 1}
                     {:db/id -1 :selected-page :competitions}
                     ;{:db/id -1 :app/selected-competition {}}
                     ]))

(defn app-started? [conn]
  (not
   (empty? (d/q '[:find ?e
                  :where
                  [?e :app/id 1]] (d/db conn)))))

;(log conn)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Server message handling

(declare reconciler)
(defn handle-query-result [d]
  (log "Handle Q R")
    ;; VERY TEMPORARY (KILL ME IF I DO NOT FIX IT)
    ;; Need to make difference between query for all comps. vs query for details for a comp.

  (if (vector? d)
    (let [clean-data  {:competitions d}      ;(uidb/sanitize (first d))
          ]
      (log "Compsss")
      (om/transact! reconciler `[(app/add-competition ~clean-data) :app/competitions])
      )
    (let [clean-data (uidb/sanitize d)]
      (log "Crazzzy")
      (om/transact! reconciler `[(app/add-competition ~clean-data) :app/competitions]))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Sente message handling
