{-# LANGUAGE CPP #-}

module Effectful.Notify.Internal.System.NotifySend
  ( notify,
  )
where

import Control.Exception (throwIO)
import Control.Exception.Utils qualified as Ex.Utils
import Data.Text qualified as T
import Effectful.Notify.Internal.Data.Note (Note)
import Effectful.Notify.Internal.Data.NotifyException
  ( NotifyException
      ( MkNotifyException,
        exception,
        fatal,
        note,
        notifySystem
      ),
  )
import Effectful.Notify.Internal.Data.NotifySystem
  ( NotifySystem (NotifySystemNotifySend),
  )
import Effectful.Notify.Internal.Data.NotifyTimeout
  ( NotifyTimeout (NotifyTimeoutMillis, NotifyTimeoutNever),
  )
import Effectful.Notify.Internal.Data.NotifyUrgency
  ( NotifyUrgency (NotifyUrgencyCritical, NotifyUrgencyLow, NotifyUrgencyNormal),
  )
import Effectful.Notify.Internal.Utils qualified as Utils
import Optics.Core ((^.))

notify :: Note -> IO ()
notify :: Note -> IO ()
notify Note
note =
  Note -> IO ()
sendNote Note
note IO () -> (SomeException -> IO ()) -> IO ()
forall (m :: * -> *) a.
(HasCallStack, MonadCatch m) =>
m a -> (SomeException -> m a) -> m a
`Ex.Utils.catchSync` \SomeException
ex ->
    NotifyException -> IO ()
forall e a. (HasCallStack, Exception e) => e -> IO a
throwIO (NotifyException -> IO ()) -> NotifyException -> IO ()
forall a b. (a -> b) -> a -> b
$
      MkNotifyException
        { exception :: SomeException
exception = SomeException
ex,
          fatal :: Bool
fatal = Bool
True,
          Note
note :: Note
note :: Note
note,
          notifySystem :: NotifySystem
notifySystem = NotifySystem
NotifySystemNotifySend
        }
  where
    sendNote :: Note -> IO ()
sendNote = HasCallStack => String -> IO ()
String -> IO ()
Utils.runProcessIO (String -> IO ()) -> (Note -> String) -> Note -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Note -> String
noteToNotifySend

noteToNotifySend :: Note -> String
noteToNotifySend :: Note -> String
noteToNotifySend Note
note =
  Text -> String
T.unpack
    (Text -> String) -> ([Text] -> Text) -> [Text] -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat
    ([Text] -> String) -> [Text] -> String
forall a b. (a -> b) -> a -> b
$ [ Text
"notify-send ",
        Text -> (Text -> Text) -> Maybe Text -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" ((Text
" --app-name " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>) (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
Utils.mkProcessText) (Note
note Note -> Optic' A_Lens NoIx Note (Maybe Text) -> Maybe Text
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx Note (Maybe Text)
#title),
        Text -> Text
Utils.mkProcessText (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Note
note Note -> Optic' A_Lens NoIx Note Text -> Text
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx Note Text
#summary,
        Text -> (Text -> Text) -> Maybe Text -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" Text -> Text
Utils.mkProcessText (Note
note Note -> Optic' A_Lens NoIx Note (Maybe Text) -> Maybe Text
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx Note (Maybe Text)
#body),
        Text -> (NotifyUrgency -> Text) -> Maybe NotifyUrgency -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" ((Text
" --urgency " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>) (Text -> Text) -> (NotifyUrgency -> Text) -> NotifyUrgency -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NotifyUrgency -> Text
forall {a}. IsString a => NotifyUrgency -> a
ulToNS) (Note
note Note
-> Optic' A_Lens NoIx Note (Maybe NotifyUrgency)
-> Maybe NotifyUrgency
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx Note (Maybe NotifyUrgency)
#urgency),
        Text -> (NotifyTimeout -> Text) -> Maybe NotifyTimeout -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" ((Text
" --expire-time " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>) (Text -> Text) -> (NotifyTimeout -> Text) -> NotifyTimeout -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NotifyTimeout -> Text
toToNS) (Note
note Note
-> Optic' A_Lens NoIx Note (Maybe NotifyTimeout)
-> Maybe NotifyTimeout
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx Note (Maybe NotifyTimeout)
#timeout)
      ]
  where
    ulToNS :: NotifyUrgency -> a
ulToNS NotifyUrgency
NotifyUrgencyLow = a
"low"
    ulToNS NotifyUrgency
NotifyUrgencyNormal = a
"normal"
    ulToNS NotifyUrgency
NotifyUrgencyCritical = a
"critical"

    toToNS :: NotifyTimeout -> Text
toToNS NotifyTimeout
NotifyTimeoutNever = Text
"0"
    toToNS (NotifyTimeoutMillis Int
n) = Int -> Text
forall a. Show a => a -> Text
Utils.showt Int
n