(define-module (game render level)
  #:use-module (game model level)
  #:use-module (game util assets)
  #:use-module (srfi srfi-64)
  #:use-module (chickadee graphics color)
  #:use-module (chickadee graphics sprite)
  #:use-module (chickadee graphics texture)
  #:use-module (chickadee graphics text)
  #:use-module (chickadee math vector)
  #:export (render-level-load
            render-level-draw
            render-level-set!
            render-victory
            render-defeat
            empty-index
            brick-index
            ladder-index
            ))


(define sprite-batch #f)

;; load the tile texture and split it into a tile atlas
(define (render-level-load)
  (set! sprite-batch (make-sprite-batch tile-texture #:capacity 2400))
  )



;; map tile symbols to tile indices in the tile atlas

(define empty-index 2500)
(define brick-index 3750)
(define ladder-index 3352)

(define (level-tile-index tile)
  (case tile
    ((empty) empty-index)
    ((brick) brick-index)
    ((wall) 3709)
    ((ladder) ladder-index)
    
    ((goal) empty-index)           ;; render empty tile for goal location
    ((hero) empty-index)          ;; render empty tile for hero start location
    ((other) empty-index)           ;; render empty tile for enemy start location
    ((good-food bad-food) empty-index)
    ((key) empty-index)
    ;; render visual reminder of unknown tile
    (else 3326)))


;; update the sprite-batch for the passed level
(define (render-level-set! level)
  (sprite-batch-clear! sprite-batch)
  (for-each 
   (lambda (row)
     (for-each
      (lambda (column)
        (let* 
            ((position (vec2 (* 16 column) (* 16 row)))
             (tile (level-tile-at level position))
             (texture-region (texture-atlas-ref tile-atlas (level-tile-index tile))))
          (if (equal? tile 'ladder)
              ;; add background behind ladder
              (if (or (equal? (level-tile-at level (vec2+ position (vec2 16 0))) 'brick)
                      (equal? (level-tile-at level (vec2- position (vec2 -16 0))) 'brick))
                  (sprite-batch-add! sprite-batch position 
                                     #:texture-region (texture-atlas-ref tile-atlas brick-index))
                  (sprite-batch-add! sprite-batch position 
                                     #:texture-region (texture-atlas-ref tile-atlas empty-index))))
          (sprite-batch-add! sprite-batch position 
                             #:texture-region texture-region)))
      (iota (level-width level) 0)))
   (iota (level-height level) 0)))


;; render the level tiles
(define (render-level-draw level)
  (draw-sprite-batch sprite-batch))


(define (render-victory)
  (draw-sprite victory-image (vec2 0 0)))

(define (render-defeat)
  (draw-sprite defeat-image (vec2 0 0)))

(test-begin "tile-translation")

(test-equal (level-tile-index 'empty) empty-index)
(test-equal (level-tile-index 'brick) 3750)
(test-equal (level-tile-index 'wall) 3709)
(test-equal (level-tile-index 'ladder) ladder-index)
(test-equal (level-tile-index 'goal) empty-index)
(test-equal (level-tile-index 'unknown) 3326)

(test-end "tile-translation")
