 (defn pending-checker []
   (go-loop []
     (<! (timeout 3000))
-    (when-let [pending-pair (get-pending-pair (current-global-state))]
+    (when-let [pending-pair (gs/get-pending-pair (gs/current-global-state))]
       (let [[uuid1 uuid2] pending-pair
-            [channel1 channel2] (map #(channel-for-uuid (current-global-state) %) pending-pair)
+            [channel1 channel2] (map #(gs/channel-for-uuid (gs/current-global-state) %) pending-pair)
             game-id (u/gen-uuid)]
         (u/log "Taking two players for game " game-id " with uuids: " pending-pair "\n\n")
         (send-command {:type :start-game

                               :enemy uuid1}}
                       channel2)

        (update-global-state add-game game-id (f/make-game uuid1 uuid2))
        (update-global-state #(apply remove-from-pending % pending-pair))))
    (recur)))

(defn make-update-data [game-state player-uuid]


(defmethod handle-command "game-request" [command channel]
  (let [uuid (u/gen-uuid)]
    (gs/update-global-state (comp #(gs/add-channel % channel uuid)
                                  #(gs/add-pending % uuid)))
    (u/log "Processing game-request and generating uuid: " uuid)
    (send-command {:type :game-request-ok
                   :data {:uuid uuid
                          :pending (gs/get-pending-players (gs/current-global-state))}}
                  channel
                  )))

(defmethod handle-command "start-game-ok" [command channel]
  (let [game-id (u/uuid-from-string (get-in command [:data :game-id]))
        game (gs/get-game (gs/current-global-state) game-id)
        player-id (gs/uuid-for-channel (gs/current-global-state) channel)
        ]
    (u/log "Processing start-game-ok for game: " game-id
           "\n\n")
    (gs/update-global-state gs/update-game game-id f/mark-player-ready player-id)
    (when (f/both-players-ready (gs/get-game (gs/current-global-state) game-id))
      (gs/update-global-state gs/update-game game-id f/init-game)
      (send-game-updates game-id))))

(defmethod handle-command "action" [command channel]
  (let [game-id (u/uuid-from-string (get-in command [:data :game-id]))
        player-id (gs/uuid-for-channel (gs/current-global-state) channel)
        card-idx (get-in command [:data :card-idx])
        target (get-in command [:data :target])
        funstr-idx (get-in command [:data :funstruct-idx])]
    (gs/update-global-state gs/update-game game-id f/use-card player-id card-idx funstr-idx)
    (send-game-updates game-id)))

(defmethod handle-command "end-turn" [command channel]
  (let [game-id (u/uuid-from-string (get-in command [:data :game-id]))
        player-id (gs/uuid-for-channel (gs/current-global-state) channel)]

    (u/log "Processing end-turn for game: " game-id)

    (gs/update-global-state gs/update-game game-id f/end-turn-for-player)
    (let [updated-game (gs/get-game (gs/current-global-state) game-id)]
      (when (f/turn-finished? updated-game)
        (gs/update-global-state gs/update-game game-id f/end-turn)
        (send-game-updates game-id)))))

(defmethod handle-command "chat-message" [command channel]
  (let [game-id (u/uuid-from-string (get-in command [:data :game-id]))
        player-id (gs/uuid-for-channel (gs/current-global-state) channel)]
    ))

(defmethod handle-command :default [command channel]
  (u/printerr "Unrecognized command: " command))
