(local state (require :state))
(var (screen-width screen-height) (love.window.getMode))
(var (mx my) (values (/ screen-width 2) (/ screen-height 2)))
(love.mouse.setGrabbed false)
(love.mouse.setRelativeMode false)

(love.graphics.setNewFont 30)

(var ticks 0)
;; Start screen logos
(var screen-logos [])
(var screen-logo-files (love.filesystem.getDirectoryItems "assets/images/logos"))
(each [_ v (ipairs screen-logo-files)]
   (local logo {})
   (tset logo :i (love.graphics.newImage (.. "assets/images/logos/" v)))
   (var imgdata (love.image.newImageData (.. "assets/images/logos/" v)))
   (tset logo :w (imgdata:getWidth))
   (tset logo :h (imgdata:getHeight))
   (table.insert screen-logos logo))

(var footer-font (love.graphics.newFont 11))
(local (major minor revision) (love.getVersion))

(fn helmet-hud []
  (love.graphics.setColor 0 1 1 1)
  (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))

(var menu-font (love.graphics.newFont 20))
(fn menu-button [x y t la ra sa]
   (love.graphics.polygon "line" (- x 100) y 
                                 (+ x 100) y
                                 (+ x 150) (+ y 20)
                                 (+ x 100) (+ y 40)
                                 (- x 100) (+ y 40)
                                 (- x 150) (+ y 20))
   (love.graphics.printf t (- x 109) (+ y 3) (+ x 100)))

(var show-logos true)
(var (screen-show logo-idx fade-time fade-mod) (values 0 1 0 1))
(fn reset-logo-timers []
   (set screen-show 0)
   (set fade-time 0)
   (set fade-mod 1))
(fn show-logo [x]
   (var logo (. screen-logos x))
   (if 
      (< screen-show 5)
         (do
            (love.graphics.setColor 0 1 1 fade-time)
            (love.graphics.draw (. logo :i)
                                (- (/ screen-width 2) (/ (. logo :w) 2))
                                (- (/ screen-height 2) (/ (. logo :h) 2))))
      ; else
         (do
            (if (. screen-logos (+ x 1))
               (set logo-idx (+ x 1))
            ; else
               (set show-logos false))
            (reset-logo-timers))))

{
 :draw 
   (fn draw []
      (if 
         show-logos
            (show-logo logo-idx)
         (do 
            (helmet-hud)
            (menu-button (/ screen-width 2) 100 "S U R V I V O R")
            (love.graphics.printf "Press P to Play"
               0 (+ (/ screen-height 2) 100)
               screen-width :center)
            (love.graphics.printf "Press X to Quit"
               0 (+ (/ screen-height 2) 150)
               screen-width :center)
            (love.graphics.printf
             (: "Made with Love [%s.%s.%s] and Fennel"
                :format  major minor revision) footer-font 20 (- screen-height 30) screen-width :left)
            (love.graphics.printf "A game by Bill Niblock" footer-font -20 (- screen-height 30) screen-width :right)))
   )

 :update
   (fn update [dt]
      (set ticks (+ ticks dt))
      (when (> fade-time 2)
         (set fade-mod -1))
      (set fade-time (+ fade-time (* fade-mod dt)))
      (when (> ticks 1)
         (set ticks (- ticks 1))
         (set screen-show (+ screen-show 1))))

 :keypressed 
   (fn keypressed [key set-mode]
      (when (= key "p")
         (var newseed (+ (love.timer.getTime) mx my))
         (state.setSeed newseed)
         (set-mode :mode-play))
      (when (= key "f")
         (love.window.setFullscreen (not (love.window.getFullscreen))))
      (when (= key "x")
         (love.event.quit)))
 :mousemoved
   (fn mousemoved [x y]
      (set (mx my) (values x y)))
}
