;; -*- lexical-binding: t -*-

;;; utilities

(defun jao-mixer-get-level (&optional dev nomsg)
  (interactive)
  (let* ((dev (or dev "Master"))
         (s (shell-command-to-string (format "amixer sget %s" dev)))
         (s (car (last (split-string s "\n" t)))))
    (when (string-match ".*Front .*\\[\\([0-9]+\\)%\\] .*" s)
      (let ((level (match-string 1 s)))
        (unless nomsg (message "%s level: %s%%" dev level))
        (string-to-number level)))))

(defun jao-mixer-set (dev v)
  (jao-shell-exec* t "amixer" "sset" dev v)
  (jao-mixer-get-level dev))

(defun jao-mixer-master-toggle ()
  (interactive)
  (jao-mixer-set "Master" "toggle"))

(defun jao-mixer-master-up ()
  (interactive)
  (jao-mixer-set "Master" "10%+"))

(defun jao-mixer-master-down ()
  (interactive)
  (jao-mixer-set "Master" "10%-"))

(defun jao-mixer-capture-up ()
  (interactive)
  (jao-mixer-set "Capture" "10%+"))

(defun jao-mixer-capture-down ()
  (interactive)
  (jao-mixer-set "Capture" "10%-"))

(jao-shell-def-exec jao-audio-applet "pasystray")

(defun jao-toggle-audio-applet ()
  (interactive)
  (or (jao-shell-kill-p "paystray") (jao-audio-applet)))

(global-set-key (kbd "<f4>") #'jao-toggle-audio-applet)

;;; streaming aliases
(defalias 'jao-streaming-list #'ignore)
(defalias 'jao-streaming-like #'ignore)
(defalias 'jao-streaming-dislike #'ignore)
(defalias 'jao-streaming-toggle-shuffle #'ignore)
(defalias 'jao-streaming-lyrics #'ignore)
(defalias 'jao-streaming-toggle #'ignore)
(defalias 'jao-streaming-next #'ignore)
(defalias 'jao-streaming-prev #'ignore)
(defalias 'jao-streaming-current #'ignore)
(defalias 'jao-streaming-seek #'ignore)
(defalias 'jao-streaming-seek-back #'ignore)
(defalias 'jao-streaming-volume #'ignore)
(defalias 'jao-streaming-volume-down #'ignore)

(defun jao-toggle-pasystray-applet ()
  (interactive)
  (or (jao-shell-kill-p "pasystray") (jao-shell-exec "pasystray")))

(transient-define-prefix jao-transient-streaming ()
  [:description
   (lambda () (format "Streaming using %s" jao-mpris-player))
   ;; ["Search" :if jao-mpris-mopidy-p
   ;;  ("a" "album" jao-streaming-album)
   ;;  ("A" "artist" jao-streaming-artist)
   ;;  ("t" "track" jao-streaming-track)
   ;;  ("P" "playlist" jao-streaming-playlist)]
   ["Play"
    ("s" "toggle" jao-streaming-toggle)
    ("n" "next" jao-streaming-next)
    ("p" "previous" jao-streaming-prev)
    ("T" "toggle player" jao-streaming-toggle-player)]
   ["Seek & shout"
    ("f" "seek fwd" jao-streaming-seek :transient t)
    ("F" "seek bwd" jao-streaming-seek-back :transient t)
    ("u" "up" jao-streaming-volume :transient t)
    ("d" "down" jao-streaming-volume-down :transient t)]
   ["Browse"
    ;; ("l" "playing list" jao-streaming-list :if jao-mpris-mopidy-p)
    ("L" "lyrics" jao-streaming-lyrics)
    ("w" "currently playing" jao-streaming-current)]
   ;; ["Act"  :if jao-mpris-mopidy-p
   ;;  ("k" "like" jao-streaming-like)
   ;;  ("K" "dislike" jao-streaming-dislike)
   ;;  ("S" "toggle shuffle" jao-streaming-toggle-shuffle)]
   ]
  )

;;; mpris
(defun jao-mpris-lyrics (&optional force)
  (interactive "P")
  (jao-show-lyrics force #'jao-mpris-artist-title))

(use-package jao-mpris :demand t)

(defun jao-mpris-setup-aliases ()
  (setq espotify-play-uri-function #'espotify-play-uri-with-dbus)
  ;; (setq jao-mpris-player "mopidy")
  (defalias 'jao-streaming-list #'jao-mpc-show-playlist)
  (defalias 'jao-streaming-lyrics #'jao-mpris-lyrics)
  (defalias 'jao-streaming-toggle #'jao-mpris-play-pause)
  (defalias 'jao-streaming-next #'jao-mpris-next)
  (defalias 'jao-streaming-prev #'jao-mpris-previous)
  (defalias 'jao-streaming-current #'jao-mpris-show-osd)
  (defalias 'jao-streaming-seek #'jao-mpris-seek)
  (defalias 'jao-streaming-seek-back #'jao-mpris-seek-back)
  (defalias 'jao-streaming-volume #'jao-mpris-vol)
  (defalias 'jao-streaming-volume-down #'jao-mpris-vol-down))

(jao-mpris-register "playerctld" :session 70)

;;; - provide
(provide 'jao-custom-multimedia)
