   (:require [clojure.browser.repl]
             [reversi.core :as rcore]))
 
-(def *border* (atom rcore/*default-border*))
-(def *phaser-border* (atom [[] [] [] [] [] [] [] []]))
-(def *visible-possible-grids-pos* (atom '()))
-(def ^:dynamic *game* (js/Phaser.Game. 905 605 js/Phaser.AUTO "game_div"))
-(def ^:dynamic *default-player-use-piece* rcore/*black-piece*)
-(def ^:dynamic *default-pc-use-piece* rcore/*white-piece*)
+(def *border*  rcore/*default-border*)
+(def *phaser-border* [[] [] [] [] [] [] [] []])
+(def *visible-possible-grids-pos* '())
+(def ^:dynamic *default-player-piece* rcore/*black-piece*)
+(def ^:dynamic *default-pc-piece* rcore/*white-piece*)
+(def *game* (js/Phaser.Game.
+             905 605
+             js/Phaser.AUTO "game_div"))
 
 (defn grid-name->rcore-grid [grid-name]
   (let [gn-rp-table

         rcore/*possible-grid* "possible-grid"}]
    (rp-gn-table rcore-grid)))

(defn another-color-piece [grid-name]
  (rcore-grid->grid-name
   (rcore/another-color-piece
    (grid-name->rcore-grid grid-name))))

(defn put-piece [piece [x y] border phaser-border]
  (let [result-border
        (rcore/put-piece piece
                         [x y]
                         border

                                 false)
                           ))]
    (when-not (empty? result-border)
      (doseq [pg-pos visible-possible-grids-pos]
        (set! (.-visible ((rcore/gridth pg-pos phaser-border)
                          "possible-grid")) false))
      (set! (.-visiblePossibleGridsPos game) '())
      (set! (.-border game) result-border)
      result-border)))

(defn pc-ai-put-piece [game]
  (let [border
        (.-border game)
        phaser-border
        (.-phaserBorder game)
        visible-possible-grids-pos
        (.-visiblePossibleGridsPos game)
        pc-possible-grids-pos
        (rcore/find-putable-empty-grids
         *default-pc-piece* border)]
    (if-not (empty? pc-possible-grids-pos)
      (put-piece *default-pc-piece*
                 (rand-nth pc-possible-grids-pos)
                 game))))

(defn show-possible-grid-on-pborder [game]
  (let [border
        (.-border game)
        phaser-border
        (.-phaserBorder game)
        visible-possible-grids-pos
        (.-visiblePossibleGridsPos game)
        player-possible-grids-pos
        (rcore/find-putable-empty-grids
         *default-player-piece* border)]
    (if-not (empty? player-possible-grids-pos)
      (do
        (set! (.-visiblePossibleGridsPos game)
              (concat visible-possible-grids-pos
                      player-possible-grids-pos))
        (doseq [pg-pos player-possible-grids-pos]
          (set! (.-visible ((rcore/gridth pg-pos phaser-border)
                            "possible-grid"))
                true))
        player-possible-grids-pos)
      (pc-ai-put-piece game))))

(defn on-click-possible-grid [pgrid]
  (let [game (.-game pgrid)
        border (.-border game)
        phaser-border (.-phaserBorder game)
        default-player-piece (.-defaultPlayerPiece game)
        default-pc-piece (.-defaultPcPiece game)]
    (put-piece default-player-piece
               (.-gridPosition pgrid)
               game)
    (pc-ai-put-piece game)
    (let [no-put-piece-ai-and-player?
          (nil? (show-possible-grid-on-pborder game))]
      (when (or (rcore/end-game? border) no-put-piece-ai-and-player?)
        (let [winner (rcore/who-win? border)]
          (if (= winner default-player-piece)
            (js/alert "you are winner!")
            (js/alert "you are loser!")))))
    (let [result-count (rcore/grid-count border)]
      (set! (.-text (.-pieceCountText game))
            (str "black-piece: " (result-count rcore/*black-piece*)
                 "\nwhite-piece: " (result-count rcore/*white-piece*))))))

(defn create-pieces [piece-name game]
  (let [piece-color (grid-name->rcore-grid piece-name)]
    (dotimes [y 8]
      (dotimes [x 8]
        (let [phaser-border (.-phaserBorder game)
              phaser-pos
              (grid-pos->phaser-pos [x y])
              piece
              (.sprite (.-add game) (first phaser-pos) (second phaser-pos) piece-name)]
          (set! (.-visible piece) false)
          (set! (.-gridPosition piece) [x y])
          (set! (.-gridColor piece) piece-color)
          (set! (.-phaserBorder game)
                (assoc-in phaser-border [y x piece-name] piece)))))))

(defn create-possible-grids [grid-name game]
  (dotimes [y 8]
    (dotimes [x 8]
      (let [phaser-border (.-phaserBorder game)
            pgrid
            (.sprite (.-add game) (+ 5 (* x 75)) (+ 5 (* y 75)) grid-name)]
        (set! (.-inputEnabled pgrid) true)
        (.add (.-onInputDown (.-events pgrid)) on-click-possible-grid game)
        (set! (.-visible pgrid) false)
        (set! (.-gridPosition pgrid) [x y])
        (set! (.-gridColor pgrid) rcore/*possible-grid*)
        (set! (.-useHandCursor (.-input pgrid)) true)
        (set! (.-phaserBorder game)
              (assoc-in phaser-border [y x grid-name] pgrid))))))

;; (defn game-loop []
;;   (js/alert "wiwi"))

(defn init-border [game]
  (let [phaser-border (.-phaserBorder game)]
    (set! (.-visible ((rcore/gridth [3 3] phaser-border) "black-piece")) true)
    (set! (.-visible ((rcore/gridth [4 4] phaser-border) "black-piece")) true)
    (set! (.-visible ((rcore/gridth [4 3] phaser-border) "white-piece")) true)
    (set! (.-visible ((rcore/gridth [3 4] phaser-border) "white-piece")) true)
    (set! (.-visible ((rcore/gridth [3 4] phaser-border) "white-piece")) true)
    (set! (.-visible ((rcore/gridth [4 2] phaser-border) "possible-grid")) true)
    (set! (.-visible ((rcore/gridth [3 5] phaser-border) "possible-grid")) true)
    (set! (.-visible ((rcore/gridth [5 3] phaser-border) "possible-grid")) true)
    (set! (.-visible ((rcore/gridth [2 4] phaser-border) "possible-grid")) true)
    (doseq [pos [ [4 2] [3 5] [5 3] [2 4] ]]
      (let [visible-possible-grids-pos (.-visiblePossibleGridsPos game)]
        (set! (.-visiblePossibleGridsPos game)
              (conj visible-possible-grids-pos pos))))))

(defn create-all-pieces [white-name black-name game]
  (doseq [piece-name [white-name black-name]]
