; Draw the overlay
; Relies on information about the player and the display
(local player (require :state))
(var (screen-width screen-height) (love.window.getMode))

; This draws the oxygen ui
; Sequential blocks, up to 10; each block 10%
(fn oxygen-ui [x y]
  (local (bx by bb o) (values 10 10 15 (player.getO)))
  (local bn (math.floor (/ o 10)))
  (var (dx dy) (values 0 50))
  (local bp (* 5 (% o 10)))
  (love.graphics.setColor 0 1 0 0.25)
  (for [i 1 10]
    (love.graphics.polygon "line" (+ x dx 1) (+ y by 1)
                                  (+ x dx bx 1) (+ y by 1)
                                  (+ x dx bx 1) (+ y by dy 1)
                                  (+ x dx 1) (+ y by dy 1))
    (if (<= i bn)
      (love.graphics.polygon "fill" (+ x dx) (+ y by)
                                    (+ x dx bx) (+ y by)
                                    (+ x dx bx) (+ y by dy)
                                    (+ x dx) (+ y by dy)))
    (if (= i (+ bn 1))
      (love.graphics.polygon "fill" (+ x dx) (+ y (- (+ dy by) bp))
                                    (+ x dx bx) (+ y (- (+ dy by) bp))
                                    (+ x dx bx) (+ y by dy)
                                    (+ x dx) (+ y by dy)))
    (set dx (+ dx bb))))

; This draws the power ui
; Inverse-sequential blocks, up to 10; each block 10%
(fn power-ui [x y]
  (local (bx by bb p) (values 10 10 15 (player.getP)))
  (local bn (math.floor (/ p 10)))
  (var (dx dy) (values 0 50))
  (local bp (* 5 (% p 10)))
  (love.graphics.setColor 1 1 0 0.25)
  (for [i 1 10]
    (love.graphics.polygon "line" (- x dx 1) (+ y by 1)
                                  (- x dx bx 1) (+ y by 1)
                                  (- x dx bx 1) (+ y by dy 1)
                                  (- x dx 1) (+ y by dy 1))
    (if (<= i bn)
      (love.graphics.polygon "fill" (- x dx) (+ y by)
                                    (- x dx bx) (+ y by)
                                    (- x dx bx) (+ y by dy)
                                    (- x dx) (+ y by dy)))
    (if (= i (+ bn 1))
      (love.graphics.polygon "fill" (- x dx) (+ y (- (+ dy by) bp))
                                    (- x dx bx) (+ y (- (+ dy by) bp))
                                    (- x dx bx) (+ y by dy)
                                    (- x dx) (+ y by dy)))
    (set dx (+ dx bb))))

; This draws barriers around the screen
; Eventually to be improved into a helmet
(fn helmet-hud []
  (love.graphics.setColor 0 1 1 0.25)
  (love.graphics.line 0 0
                      (+ (/ screen-width 6) 0) 35
                      (+ (/ screen-width 3) 0) 30
                      (- screen-width (/ screen-width 3)) 30
                      (- screen-width (/ screen-width 6)) 35
                      screen-width 0)
  ; (love.graphics.polygon "fill" (- screen-width 20) 0
  ;                               screen-width 0
  ;                               screen-width (- screen-height 20)
  ;                               (- screen-width 20) (- screen-height 20))
  ; (love.graphics.polygon "fill" 0 0
  ;                               20 0 
  ;                               20 screen-height
  ;                               0 screen-height)
  ; (love.graphics.polygon "fill" 0 (- screen-height 50)
  ;                               (/ screen-width 6) (- screen-height 35) 
  ;                               (/ screen-width 3) (- screen-height 30) 
  ;                               0 screen-height)
  ; (love.graphics.polygon "fill" screen-width (- screen-height 50)
  ;                               (- screen-width (/ screen-width 6)) (- screen-height 35) 
  ;                               (- screen-width (/ screen-width 3)) (- screen-height 30) 
  ;                               screen-width screen-height)
)

; This draws a compass bar at the top of the HUD
(var compass-font (love.graphics.newFont 20))
(var hc-bar-east ["-" "-" "|" "N" "|"
                 "-" "-" "|" "NE" "|" 
                 "-" "-" "|" "E" "|" 
                 "-" "-" "|" "SE" "|" 
                 "-" "|" "S" "|" "-" ])
(var hc-bar-west ["-" "-" "|" "N" "|"
                 "-" "-" "|" "NW" "|" 
                 "-" "-" "|" "W" "|" 
                 "-" "-" "|" "SW" "|" 
                 "-" "|" "S" "|" "-" ])
(var hc-bar-limit 6)
(fn circular-compass [l c n]
    (if (< (+ c n) 1) (length l)
        (> (+ c n) (length l)) 1
        (+ c n)))
(fn compass-bar []
  (love.graphics.setColor 0 1 1 0.25)
  (var (hc-output hc-padding) (values "" "              "))
  (var hc-idx (math.floor (+ 10 (* 10 (player.getDirX)))))
  (var hc-idx-mod 1)
  (for [i 1 (+ 1 hc-bar-limit)]
    (if (> (player.getDirY) 0) 
      (do
        (set hc-idx-mod 1) 
        (set hc-idx (circular-compass hc-bar-east hc-idx hc-idx-mod))
        (set hc-output (.. hc-output (. hc-bar-east hc-idx) hc-padding)))
      (do 
        (set hc-idx-mod 1)
        (set hc-idx (circular-compass hc-bar-west hc-idx hc-idx-mod))
        (set hc-output (.. (. hc-bar-west hc-idx) hc-padding hc-output)))))
  hc-output)

(fn hud-compass []
  (love.graphics.setColor 0.5 1 1 0.5)
  (var compass-bar-output (compass-bar))
  (love.graphics.printf compass-bar-output compass-font 0 50 screen-width :center))
  
(fn overlay [dx dy pos-x]
  (love.graphics.translate dx (- dy))
  (oxygen-ui (+ (/ screen-width 2) 100) (- screen-height 100))
  ; (power-ui (- (/ screen-width 2) 100) (- screen-height 100))
  (helmet-hud)
  (hud-compass)
  (love.graphics.setColor 1 1 1)
)

{: overlay}
