-- | This module provides a service for a single alert.
module Navi.Services.Custom.Single
  ( SingleToml,
    toEvent,
  )
where

import Data.Text qualified as T
import Navi.Data.NaviNote (NaviNote)
import Navi.Data.PollInterval (PollInterval (..))
import Navi.Event.Toml qualified as EventToml
import Navi.Event.Types
  ( AnyEvent (..),
    ErrorNote (..),
    Event (..),
    RepeatEvent (..),
  )
import Navi.Prelude
import Navi.Services.Custom.Single.Toml (SingleToml)
import Navi.Services.Types (ServiceType (..))
import Pythia.Data.Command (Command)

-- | Transforms toml configuration data into an 'AnyEvent'.
toEvent :: (MonadIORef m) => SingleToml -> m AnyEvent
toEvent :: forall (m :: Type -> Type).
MonadIORef m =>
SingleToml -> m AnyEvent
toEvent SingleToml
toml = do
  RepeatEvent Text
repeatEvent <- forall (m :: Type -> Type) a.
MonadIORef m =>
Maybe RepeatEventToml -> m (RepeatEvent a)
EventToml.mRepeatEventTomlToVal (SingleToml
toml forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall a. IsLabel "repeatEventCfg" a => a
#repeatEventCfg)
  ErrorNote
errorNote <- forall (m :: Type -> Type).
MonadIORef m =>
Maybe ErrorNoteToml -> m ErrorNote
EventToml.mErrorNoteTomlToVal (SingleToml
toml forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall a. IsLabel "errEventCfg" a => a
#errEventCfg)
  pure $
    forall result. (Eq result, Show result) => Event result -> AnyEvent
MkAnyEvent forall a b. (a -> b) -> a -> b
$
      Maybe Text
-> Command
-> PollInterval
-> (Text, NaviNote)
-> RepeatEvent Text
-> ErrorNote
-> Event Text
mkSingleEvent
        (SingleToml
toml forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall a. IsLabel "name" a => a
#name)
        (SingleToml
toml forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall a. IsLabel "command" a => a
#command)
        PollInterval
pi
        (Text -> Text
T.strip (SingleToml
toml forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall a. IsLabel "triggerVal" a => a
#triggerVal), SingleToml
toml forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall a. IsLabel "note" a => a
#note)
        RepeatEvent Text
repeatEvent
        ErrorNote
errorNote
  where
    pi :: PollInterval
pi = forall a. a -> Maybe a -> a
fromMaybe (Natural -> PollInterval
MkPollInterval Natural
30) (SingleToml
toml forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall a. IsLabel "pollInterval" a => a
#pollInterval)
{-# INLINEABLE toEvent #-}

mkSingleEvent ::
  Maybe Text ->
  Command ->
  PollInterval ->
  (Text, NaviNote) ->
  RepeatEvent Text ->
  ErrorNote ->
  Event Text
mkSingleEvent :: Maybe Text
-> Command
-> PollInterval
-> (Text, NaviNote)
-> RepeatEvent Text
-> ErrorNote
-> Event Text
mkSingleEvent Maybe Text
mname Command
cmd PollInterval
pi (Text
triggerVal, NaviNote
note) RepeatEvent Text
re ErrorNote
en =
  MkEvent
    { $sel:name:MkEvent :: Text
name = Text
name',
      $sel:pollInterval:MkEvent :: PollInterval
pollInterval = PollInterval
pi,
      $sel:serviceType:MkEvent :: ServiceType Text
serviceType = Command -> ServiceType Text
Single Command
cmd,
      $sel:raiseAlert:MkEvent :: Text -> Maybe NaviNote
raiseAlert = \Text
b -> if Text
b forall a. Eq a => a -> a -> Bool
== Text
triggerVal then forall a. a -> Maybe a
Just NaviNote
note else forall a. Maybe a
Nothing,
      $sel:repeatEvent:MkEvent :: RepeatEvent Text
repeatEvent = RepeatEvent Text
re,
      $sel:errorNote:MkEvent :: ErrorNote
errorNote = ErrorNote
en
    }
  where
    name' :: Text
name' = forall a. a -> Maybe a -> a
fromMaybe Text
"single" Maybe Text
mname
{-# INLINEABLE mkSingleEvent #-}