 (defn stop []
   (p/stop @channel))
 
+(defn restart []
+  (do (stop)
+      (start)))
+
 (defn -main
   "I don't do a whole lot ... yet."
   []

  (future (while true (do (Thread/sleep ms) (callback)))))

(defn set-timeout [callback ms]
  (swap! state assoc :timer (future (do (Thread/sleep ms)
                                        (swap! state assoc :timer nil)
                                        (callback)))))


(defn send-m

(defn check-remaining []
  (if (session-alive? @state)
    (send-remaining (:message-id @state) "manual")
    (send-m "세션이 진행중이 아닙니다")))

(defn cancel-session []
  (if (session-alive? @state)
    (do
      (future-cancel (:timer @state))
      (future-cancel (:interval @state))
      (swap! state assoc :timer nil)
      (send-m (str (:mode @state) " 취소되었습니다")))
    (send-m "취소할 세션이 없습니다")))

(defn send-counted [counted]
  (send-m (str counted)))

(defn pause-session []
  (if (session-alive? @state)
    (do
      (future-cancel (:timer @state))
      (future-cancel (:interval @state))
      (swap! state assoc
             :elapsed (elapsed-time)
             :timer nil)
      (send-m (str (:mode @state) " 잠시 중단 되었습니다")))
    (send-m "중단할 세션이 없습니다")))

(defn resume-session []
  (if (session-paused? @state)
    (let [elapsed (:elapsed @state)
          mode ((:mode @state) modes)
          during (- (:during mode) elapsed)]

      (swap! state assoc
             :started (- (System/currentTimeMillis) elapsed)
             :timer (set-timeout #(goto-x (:next mode)) during)
             :interval (remaining-each-10s #(:message-id @state)))
      (send-m "세션을 다시 시작합니다"))
    (send-m "다시 시작할 세션이 없습니다")))

(defhandler bot-api
            (command "go" [] (start-session))
            (command "check" [] (check-remaining))
            (command "count" [] (send-counted @counted))
            (command "cancel" [] (cancel-session))
            (command "pause" [] (pause-session))
            (command "resume" [] (resume-session)))

(def channel (atom nil))

