(define-module (game render hero)
  #:use-module (game model hero)
  #:use-module (game util assets)
  #:use-module (chickadee)
  #:use-module (chickadee math vector)
  #:use-module (chickadee graphics sprite)
  #:use-module (chickadee graphics texture)
  #:use-module (chickadee graphics text)
  #:export (render-hero
            render-hero-load))

;; start index of the walk animation
(define hero-walking-offset 56)
(define hero-falling-offset 32)
(define hero-climbing-offset 48)

(define (hero-sprite-walking hero)
  (let* ((x (hero-x hero))
         (animation-frame (remainder x 8))
         (hero-index (+ hero-walking-offset animation-frame)))
    (texture-atlas-ref hero-atlas hero-index)))

(define (hero-sprite-stationary _hero)
  (texture-atlas-ref hero-atlas 56))


(define (hero-sprite-falling hero)
  (let* ((y (hero-y hero))
         (animation-frame (remainder y 8))
         (hero-index (+ hero-falling-offset animation-frame)))
    (texture-atlas-ref hero-atlas hero-index)))

(define (hero-sprite-climbing hero)
  (let* ((y (hero-y hero))
         (animation-frame (remainder y 8))
         (hero-index (+ hero-climbing-offset animation-frame)))
    (texture-atlas-ref hero-atlas hero-index)))

(define red-tile 3501)
(define green-tile 3502)
(define good-marker-tile 1227)
(define bad-marker-tile 1226)

(define (render-bloat-indicator bloat)
  (let ((red (texture-atlas-ref tile-atlas red-tile))
        (green (texture-atlas-ref tile-atlas green-tile))
        (good-marker (texture-atlas-ref tile-atlas good-marker-tile)) 
        (bad-marker (texture-atlas-ref tile-atlas bad-marker-tile)) 
        )
    (draw-sprite red (vec2 0 128))
    (draw-sprite red (vec2 0 144))
    (draw-sprite red (vec2 0 160))
    (draw-sprite green (vec2 0 176))
    (draw-sprite green (vec2 0 192))
    (draw-sprite green (vec2 0 208))
    (draw-sprite green (vec2 0 224))
    (draw-sprite green (vec2 0 240))
    (draw-sprite green (vec2 0 256))
    (draw-sprite red (vec2 0 272))
    (draw-sprite red (vec2 0 288))
    (draw-sprite red (vec2 0 304))
    (draw-sprite 
     (if (< 0.5 bloat 1.5) good-marker bad-marker)
     (vec2 0 (+ 120 (* 96 bloat))))
    
    ))

(define (render-hero hero)
  (let ((position (vec2+ (hero-position hero) 
                         (if (equal? 'go-left (hero-state hero))
                             (vec2* (vec2 16.0 1.0) (hero-bloat hero))
                             (vec2* (vec2 -16.0 1.0) (hero-bloat hero)))))
        (scale (if (equal? 'go-left (hero-state hero)) 
                   (vec2 (- (hero-bloat hero)) 1.0)
                   (vec2 (hero-bloat hero) 1.0)))
        (sprite (case (hero-state hero)
                  ((stationary) (hero-sprite-stationary hero))
                  ((fall) (hero-sprite-falling hero))
                  ((climb) (hero-sprite-climbing hero))
                  ((go-left) (hero-sprite-walking hero))
                  ((go-right) (hero-sprite-walking hero))
                  ))
        )
    (draw-sprite sprite position #:scale scale))
  
  (draw-text 
   (format #f "Calories Exercised: ~a" (floor (hero-exercise hero))) 
   (vec2 216 4))
  (draw-text 
   (format #f "Bloat: ~a" (hero-bloat hero)) 
   (vec2 516 4))

  (render-bloat-indicator (hero-bloat hero))
  
  )
