 (defn session-paused? [state]
   (and (nil? (:timer state)) (not (nil? (:elapsed state)))))
 
+(defn pluck-message-id [sent-message]
+  (:message_id (:result sent-message)))
+
+(defn lang-remaing [remaining-time base-time byline]
+  (str "남은 시간은 " remaining-time "/" base-time "초 by " byline))
 
 
-(defn send-remaining [message-id byline]
-  (if (nil? message-id)
-    (->> (send-m (str "남은 시간은 " (remaining-time) "초") {:disable_notification true})
-         :result
-         :message_id
-         (swap! state assoc :message-id))
-    (edit-m (str "남은 시간은 " (remaining-time) "/" (ms->sec (base-time)) "초 by " byline) message-id)))
+(defn time-send [message _]
+  (let [sent (send-m message {:disable_notification true})]
+    (swap! state-atom assoc :message-id (pluck-message-id sent))))
 
-(defn remaining-each-10s [get-messageid]
-  (set-interval #(send-remaining (get-messageid) "interval") (secs 10)))
+(defn time-edit [message message-id]
+  (edit-m message message-id))
+
+
+(defn send-remaining [state byline]
+  (let [remaining (remaining-time)
+        base (ms->sec (base-time))
+        message (lang-remaing remaining base byline)
+        message-id (:message-id state)
+        action (cond (nil? message-id) time-send
+                     :else time-edit)]
+    (apply action [message message-id])))
+
+(defn remaining-each-10s [get-state]
+  (set-interval #(send-remaining (get-state) "interval") (secs 10)))
 
 
 
 (defn goto-x [key]
-  (let [mode (key modes)]
-    (when-not (nil? (:interval @state)) (future-cancel (:interval @state)))
-    (swap! state assoc
+  (let [mode (key modes)
+        interval (:interval @state-atom)]
+    (when-not (nil? interval) (future-cancel interval))
+    (swap! state-atom assoc
            :mode key
            :started (System/currentTimeMillis)
            :message-id nil
-           :interval (remaining-each-10s #(:message-id @state))
+           :interval (remaining-each-10s #(deref state-atom))
            :timer (set-timeout #(goto-x (:next mode)) (:during mode)))
 
     (when (= key :relax)


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





(def counted (atom 1))
(def state-atom (atom {:timer      nil
                       :interval   nil
                       :started    nil
                       :mode       nil
                       :message-id nil}))



