 (defn createjsstartgame []
   (let [canvas (dom/get-element "target")
         stage (createjs/Stage. canvas)]
-    (swap! gamestate assoc :stage stage)
+    (swap! renderer assoc :stage stage)
     ;; the preloader loads all the assets and then calls startgame with the results
     (preloader/preloadgame stage manifest startgame)))
 


;;(repl/connect "http://localhost:9000/repl")

;; gameassets
(def gameassets)

;; game state as an atom
(def gamestate (atom {:stage nil :map nil :characters nil }))

(defn clearscreen [gamescreen]
  (let [width (-> gamescreen .-canvas .-width)
        height (-> gamescreen .-canvas .-width)]
    (aset gamescreen "fillStyle" "#000000")
    (.fillRect gamescreen 0 0 width height)))

(defn simpletitle [gamescreen]
  (let [canvas (.-canvas gamescreen)
        width (.-width canvas)
        height (.-height canvas)

    (aset gamescreen "fillStyle" "orange")
    (.fillText gamescreen "argh!" textx texty)))

#_(defn addimage [stage img]
  (let [bitmap (createjs/Bitmap. img)]
    (.addChild stage bitmap)
    (aset bitmap "x" 400)
    (aset bitmap "y" 200)
    (.update stage)))


(defn updatecharactersprite [character]
  ;; create sprite if needed
  (let [charactersprite (:sprite character)
        newx (* tilespacing (:x character))
        newy (* tilespacing (:y character))]
    (aset charactersprite "x" newx)
    (aset charactersprite "y" newy)))

(defn addcharactersprite [character]
  (let [freshsprite (getspritesheet ["icons"])
        characterstage (:stage-characters @renderer)]
    (.addChild characterstage freshsprite)
    (assoc character :sprite freshsprite)))

(defn removecharactersprite [character]
  (let [characterstage (:stage-characters @renderer)]
    (.removeChild characterstage (:sprite character))
    (dissoc character :sprite)))

(defn action-addcharacter [gamestate freshcharacter]
  (conj gamestate freshcharacter))

(defn action-movecharacter [gamestate character newx newy]
  (let [characters (:characters gamestate)
        movedcharacter (-> character (assoc :x newx) (assoc :y newy))
        modifiedcharacters (conj (disj characters character) movedcharacter)]
    (assoc gamestate :characters modifiedcharacters)))

(defn action-removecharacter [gamestate character]
  (let [modifiedcharacters (disj (:characters gamestate) character)]
    (assoc gamestate :characters modifiedcharacters)))

(defn makecharacters []
  
  )


(defn chooseterrain [x y]
  (if (> x y)
    39
    54))

(defn generatemap [{:keys [width height]}]
  (for [x (range 0 width)
        y (range 0 height)]
    {:x x :y y :terrain (chooseterrain x y)}))

(defn rendermap [gamemap]
  (let [gameboardcontainer (:stage-map @renderer)
        spritesheet (getspritesheet "icons")]
    (.removeAllChildren gameboardcontainer)
    (doseq [tile gamemap
            :let [{:keys [x y terrain]} tile
                  freshtile (createjs/BitmapAnimation. spritesheet)]]
      (aset freshtile "x" (* x tilespacing))
      (aset freshtile "y" (* y tilespacing))
      (aset freshtile "scaleX" spritescale)
      (aset freshtile "scaleY" spritescale)
      (.gotoAndStop freshtile terrain)
      (.addChild gameboardcontainer freshtile))))

(defn setupboard []
  ;; should have the gamestate setup by now
  (let [gamemap (generatemap {:width 10 :height 10})]
    ;; generate a bunch of icons based off of the map data
    #_(js/console.log mapicons)
    (swap! gamestate assoc :map gamemap)
    (rendermap gamemap)
    (.update (:stage @renderer))))


(defn createnamedcontainer [name]
  (let [freshcontainer (createjs/Container.)]
    (aset freshcontainer "id" name)
    freshcontainer))

(defn startgame [loadedassets]
  (set! gameassets loadedassets)
  #_(js/console.log loadedassets)
  (let [stage (:stage @renderer)
        tilemap (createnamedcontainer "tilemap")
        characters (createnamedcontainer "characters")]
    (.addChild stage tilemap)
    (aset tilemap "id" "tilemap")
    (aset tilemap "x" 100)
    (aset tilemap "y" 100)
    (.addChild stage characters)
    (aset characters "id" "characters")
    (aset characters "x" 100)
    (aset characters "y" 100)
    ;; embed direct references to the map and character containers for
    ;; easy manipulation later
    (swap! renderer assoc :stage-map tilemap)
    (swap! renderer assoc :stage-characters characters))
  (setupboard))

(def manifest [{:id "icons" :src "gfx/placeholders_tigsource.png"}])
