(local pxl8 (require :pxl8))
(local music (require :mod.music))

(var paused false)
(var wireframe false)
(var gui nil)

(fn init []
  (set gui (pxl8.create_gui)))

(fn show []
  (set paused true)
  (pxl8.set_relative_mouse_mode false)
  (pxl8.center_cursor))

(fn hide []
  (set paused false)
  (pxl8.set_relative_mouse_mode true))

(fn toggle []
  (when (not gui) (init))
  (if paused
      (hide)
      (show)))

(fn update []
  (when gui
    (let [(mx my) (pxl8.get_mouse_pos)]
      (gui:cursor_move mx my))

    (when (pxl8.mouse_pressed 1)
      (gui:cursor_down))

    (when (pxl8.mouse_released 1)
      (gui:cursor_up))))

(fn draw []
  (when gui
    (gui:begin_frame)

    (pxl8.gui_window 200 100 240 200 "pxl8 demo")

    (when (gui:button 1 215 140 210 30 "Resume")
      (hide))

    (let [music-label (if (music.is-playing) "Music: On" "Music: Off")]
      (when (gui:button 3 215 175 210 30 music-label)
        (if (music.is-playing)
            (music.stop)
            (music.start))))

    (let [wire-label (if wireframe "Wireframe: On" "Wireframe: Off")]
      (when (gui:button 4 215 210 210 30 wire-label)
        (set wireframe (not wireframe))))

    (when (gui:button 2 215 245 210 30 "Quit")
      (pxl8.quit))

    (if (gui:is_hovering)
        (pxl8.set_cursor :hand)
        (pxl8.set_cursor :arrow))

    (gui:end_frame)))

{:is-paused (fn [] paused)
 :is-wireframe (fn [] wireframe)
 :toggle toggle
 :show show
 :hide hide
 :update update
 :draw draw}
