 (ns tetris.core
   (:require [cljs.core.async :refer [chan >! <! timeout]])
   (:require-macros [cljs.core.async.macros :refer [go go-loop]]))
+
+(enable-console-print!)
 
 (def screen-ctx (-> js/document (.getElementById "screen") (.getContext "2d")))
 (def preview-ctx (-> js/document (.getElementById "preview-screen") (.getContext "2d")))

  (assoc state :level (-> (/ score level-up-score) int inc)))

(defmulti process-keyboard
  (fn [{key :pressed-key}] (if (contains? directions key) :move key)))

(defmethod process-keyboard :move [{key :pressed-key :as state}]
  (-> state
      (update-in [:tetrimino] move-tetrimino key)
      (maybe-rollback state)))

(defmethod process-keyboard :rotate [state]
  (-> state
      (update-in [:tetrimino] rotate-tetrimino)
      (maybe-rollback state)))

(defmethod process-keyboard :default [state] state)



(defn run-game-cycle
  "Puts everything together."
  [{:keys [mode tetrimino] :as state}]
  (cond-> state
          true process-time
          true process-keyboard
          true release-keyboard
          (= mode :game) process-gravity
          (= mode :game) maybe-score
          (= mode :game) maybe-level-up
          (= mode :game) maybe-next-tetrimino
          (= mode :game) maybe-game-over))

(.addEventListener js/document "keydown" #(swap! game-state handle-key-press (.-keyCode %)) false)
(render-loop)
