- TODO:
--
-- - Use Haskell client to send notification instead of relying on external
--   application.
-- - Find another way how to play the message.  Maybe even ask for notification
--   service if it supports sounds, and play sound ourselves only if it doesn't.
-- - Using external play command blocks until the sound is played, obviously,
--   however, this is not desired behaviour for application like this.
doNotifyWhen :: NotifyConfig -> Bool -> Text -> ExitCode -> IO ()
doNotifyWhen NotifyConfig{..} p notificationMessage status = when p do
    execs "notify-send" (iconOpt <> urgencyOpt <> [msgArg])
    for_ (sound status) \soundFile ->
        execs "paplay" [fromString soundFile]
  where
    iconOpt = maybe [] (\i -> ["--icon=" <> i]) (icon status)

    urgencyOpt =
        maybe [] (\u -> ["--urgency=" <> urgencyToText u]) (urgency status)

    msgArg = composeMessage notificationMessage status

    execs cmd args = Turtle.procs cmd args empty

    urgencyToText = \case
        Low -> "low"
        Normal -> "normal"
        Critical -> "critical"

doNotifyWhen :: NotifyConfig -> Bool -> Text -> ExitCode -> IO ()
doNotifyWhen NotifyConfig{..} p notificationMessage status = when p do
    execs "notify-send" (iconOpt <> urgencyOpt <> [msgArg])
    for_ (sound status) \soundFile ->
        execs "paplay" [fromString soundFile]
  where
    iconOpt = maybe [] (\i -> ["--icon=" <> i]) (icon status)

    urgencyOpt =
        maybe [] (\u -> ["--urgency=" <> urgencyToText u]) (urgency status)

    msgArg = composeMessage notificationMessage status

    execs cmd args = Turtle.procs cmd args empty

    urgencyToText = \case
        Low -> "low"
        Normal -> "normal"
        Critical -> "critical"
doNotifyWhen NotifyConfig{..} p notificationMessage status = when p do
    execs "notify-send" (iconOpt <> urgencyOpt <> [msgArg])
    for_ (sound status) \soundFile ->
        execs "paplay" [fromString soundFile]
  where
    iconOpt = maybe [] (\i -> ["--icon=" <> i]) (icon status)

    urgencyOpt =
        maybe [] (\u -> ["--urgency=" <> urgencyToText u]) (urgency status)

    msgArg = composeMessage notificationMessage status

    execs cmd args = Turtle.procs cmd args empty

    urgencyToText = \case
        Low -> "low"
        Normal -> "normal"
        Critical -> "critical"