(define-module (game model food)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-9 gnu)
  #:use-module (game model level)
  #:use-module (game model hero)
  #:use-module (chickadee math vector)
  #:export (make-food
            food-position
            food-healthy?
            food-eaten?
            food-load
            food-update
            food-total-calories-eaten
            ))

(define-immutable-record-type <food>
  (make-food position healthy eaten)
  food?
  (position food-position)
  (healthy food-healthy?)
  (eaten food-eaten?)
  )

(define (food-collides? food position)
  (< (vec2-magnitude (vec2- (food-position food) position)) (/ level-cell-size 4)))


(define (food-eat food)
  (make-food (food-position food) (food-healthy? food) #t))

(define good-calories 60)
(define bad-calories 180)

(define (food-calories food)
  (if (food-healthy? food) good-calories bad-calories))

(define (make-good-food position)
  (make-food position #t #f))

(define (make-bad-food position)
  (make-food position #f #f))

(define (food-load level)
  (append
   (map make-good-food (level-find-good-foods level))
   (map make-bad-food (level-find-bad-foods level))))


(define (check-if-eaten food hero)
  (if (food-collides? food (hero-position hero))
      (food-eat food)
      food))

(define (food-total-calories-eaten foods)
  (fold + 0 
        (map food-calories 
             (filter food-eaten? foods))))

(define (food-update foods hero)
  (map 
   (lambda (food) (check-if-eaten food hero)) 
   foods))
