 
 ;;; Board functions
 
-(defn init-board! [board width height]
-  (let [state (->> (repeat width :empty)
-                   vec
-                   (repeat height)
-                   vec)]
-    (reset! board state)))
-
-(defn change-cell-in-board! [board x y new-state]
-  (swap! board assoc-in [y x] new-state))
-
-(defn update-region-of-board! [board [from-x from-y] [to-x to-y] new-state]
+(defn init-board [width height]
+  (->> (repeat width :empty)
+       vec
+       (repeat height)
+       vec))
+
+(defn change-cell-in-board [board x y new-state]
+  (assoc-in board [y x] new-state))
+
+(defn update-region-of-board [board [from-x from-y] [to-x to-y] new-state]
   (let [cells-to-change (for [x (range-inc from-x to-x)
                               y (range-inc from-y to-y)]
                           [y x])
-        new-board (reduce #(assoc-in %1 %2 new-state) @board cells-to-change)]
-    (reset! board new-board)))
+        new-board (reduce #(assoc-in %1 %2 new-state) board cells-to-change)]
+    new-board))
 
 (defn pad [nums beg end value]
   (vec (concat (repeat beg value)

;  [:tr [:td]        [:td#.num 2] [:td#.cell.c0.r2] [:td#.cell.c1.r2] [:td#.cell.c2.r2] [:td#.cell.c3.r2]]
;  [:tr [:td]        [:td#.num 4] [:td#.cell.c0.r3] [:td#.cell.c1.r3] [:td#.cell.c2.r3] [:td#.cell.c3.r3]]])

(def current-fill-style (atom nil)) ;one of [:empty :crossed :filled nil]

(def board (atom nil))

(def drag-type (atom :rect)) ; possible values :rect, :by-one

(defn range-inc [a b]
  (range (min a b) (inc (max a b))))


    2 :right
    :unknown))

(defn handle-mouse-down-on-cell [evt state]
  (let [cell (.-target evt)
        style (new-cell-style-after-click cell (button evt))
        x (attr-int cell :data-x)
        y (attr-int cell :data-y)]
    (change-cell-style! cell style)
    (assoc state
      :fill-style style
      :base-cell [x y]
      :last-cell [x y]
      :board (change-cell-in-board (:board state) x y style))))

(defn handle-mouse-enter-on-cell [evt state]
  (if-not (:fill-style state)
    state
    (let [cell (.-target evt)
          x (attr-int cell :data-x)
          y (attr-int cell :data-y)
          {:keys [fill-style base-cell last-cell drag-type]} state]
      (if (= :rect drag-type)
        (do
          (update-cells-region-style! base-cell last-cell (fn [x y] (get-in state [:board y x])))
          (update-cells-region-style! base-cell [x y] (constantly fill-style))
          (assoc state :last-cell [x y]))
        (do
          (change-cell-style! cell fill-style)
          (assoc state
            :last-cell [x y]
            :board (change-cell-in-board (:board state) x y fill-style)))))))

(defn stop-dragging [initial-state {:keys [fill-style base-cell last-cell board drag-type]}]
  (assoc initial-state :board
    (if (and fill-style (= :rect drag-type))
      (update-region-of-board board base-cell last-cell fill-style)
      board)))

(defn disable-context-menu [evt]
  (.preventDefault evt)
