(defpoll mediastatus :interval "1s" "./lib/media-status.sh")
(defpoll audio-sinks :interval "1s" "pactl -f json list sinks")
(defpoll audio-info :initial "{}" :interval "1s" "pactl -f json info")
(defpoll volume-level :interval "1s" "./lib/get-volume-level.sh")

(defwidget media-tray-item []
  (box :orientation "h" :space-evenly false :halign "end" :class "media-tray-item"
    (eventbox
      :cursor "pointer"
      :class "interactive"
      :onscroll "./lib/change-volume.sh {}"
      :onclick "eww open --toggle media-menu"
      :onmiddleclick "playerctl play-pause"

      (box :class "volume-tray-icon"
        (label :class "volume-icon" :text {volume-level > 66 ? "" : volume-level > 0 ? "" : ""})
        (label :text "${volume-level}%")
      ))
  )
)

(defwidget audio-source-selector []
  (box :orientation "v" :valign "start" :class "audio-source-selector"
    (box :class "title"
      (label :halign "start" :text "output source")
    )

    (for sink in audio-sinks
      (eventbox :cursor "pointer"
        (button
          :halign "start"
          :class "audio-source interactive ${sink.name == audio-info.default_sink_name ? "active" : ""}"
          :onclick "pactl set-default-sink ${sink.index}"
          "󰓃  ${sink.description}"
        )
      )
    )
  )
)

(defwidget media-player []
  (box :orientation "v" :space-evenly false :class "media-player" :valign "top"
    (label :class "title" :text "${mediastatus.artist} - ${mediastatus.title}" :show-truncated false)
    (label :class "album" :text "${mediastatus.album}" :show-truncated false)
    (box :class "media-cover" :style "background: url('${mediastatus.artUrl}'); background-size: cover;" :height 300)

    (box :class "media-controls" :spacing 12 :space-evenly false :halign "center"
      (eventbox :cursor "pointer"
        (button :class "media-button" :class "media-button shuffle ${mediastatus.shuffle == "On" ? "active" : ""}" :onclick "playerctl shuffle toggle" "")
      )

      (eventbox :cursor "pointer"
        (button :class "media-button" :onclick "playerctl previous"  "")
      )

      (eventbox :cursor "pointer"
        (button
          :class "media-button ${mediastatus.status == "Playing" ? "pause" : "play"}"
          :onclick "./lib/media-play-pause.sh"
          "${mediastatus.status == "Playing" ? "" : ""}"
        )
      )

      (eventbox :cursor "pointer"
        (button :class "media-button" :onclick "playerctl next" "")
      )

      (eventbox :cursor "pointer"
        (button
          :class "media-button loop ${mediastatus.loop != "None" ? "active" : ""}"
          :onclick "playerctl loop ${mediastatus.loop == "Playlist" ? "track" : mediastatus.loop == "Track" ? "none" : "playlist"}"
          "${mediastatus.loop == "Track" ? "󰑘" : "󰑖"}"
        )

      )

    )
  )
)

(defwindow media-menu
  :monitor 0
  :namespace "media-menu"
  :stacking "overlay"
  :geometry (geometry :x "8px" :y "8px" :width "400px" :anchor "top right")
  :wm-ignore false
  :focusable false
  (box :class "eww-overlay media-menu" :orientation "v" :space-evenly false
    (media-player)
    (audio-source-selector)

    (eventbox :cursor "pointer"
      (button :class "interactive btn" :halign "start" :onclick "eww open --toggle media-menu & pavucontrol &" "󰴸   Open PulseAudio control")
    )

    ; TODO: For some reason audio-info is null if I don't invoke it outside the for. Investigate.x
    (label :class "hidden" :text "Default: ${audio-info.default_sink_name}")
  )
)
