(ns no_intellect.graphics.pixi.canvas
  (:require ["pixi.js" :as PIXI]
            [buddho.core.state.idea :as idea]
            [buddho.core.state.property :as prop]))

(defn make-canvas! [width height]
  (let [canvas (PIXI/Application. #js {:width width :height height
                          :backgroundColor 0x444444})
        world (PIXI/Container.)
        [cid wid] (idea/have-idea! "pixi_cont_" 2)]
    (idea/update-idea! cid {:object canvas})
    (prop/register-property! cid [:canvas :main] [:pixi :container])

    (idea/update-idea! wid {:object world})
    (prop/register-property! wid [:world] [:pixi :container])

    (.appendChild js/document.body (.-view canvas))
    (.addChild (.-stage canvas) world)
    (set! (.-className (.-view canvas)) "game-view")

    (set! (.-SCALE_MODE (.-settings PIXI)) (.-NEAREST (.-SCALE_MODES PIXI)))
    (.set (-> canvas .-stage .-scale) 2)))

(defn clear-canvas! []
  (let [cur-canvas (.querySelector js/document.body ".game-view")
        canvas-idea (first (prop/with-property [:canvas :main]))]
    (when cur-canvas
      (.removeChild js/document.body cur-canvas))
    (when canvas-idea
      (idea/forget-idea! canvas-idea))))
