-- | This module provides a service for multiple alerts.
module Navi.Services.Custom.Multiple
  ( MultipleToml,
    toEvent,
  )
where

import Data.List.NonEmpty qualified as NE
import Data.Map.Strict qualified as Map
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.Multiple.Toml (MultipleToml, TriggerNoteToml (..))
import Navi.Services.Types (ServiceType (..))
import Pythia.Data.Command (Command)

-- | Transforms toml configuration data into an 'AnyEvent'.
toEvent :: (MonadIORef m) => MultipleToml -> m AnyEvent
toEvent :: forall (m :: Type -> Type).
MonadIORef m =>
MultipleToml -> m AnyEvent
toEvent MultipleToml
toml = do
  RepeatEvent Text
repeatEvent <- forall (m :: Type -> Type) a.
MonadIORef m =>
Maybe RepeatEventToml -> m (RepeatEvent a)
EventToml.mRepeatEventTomlToVal (MultipleToml
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 (MultipleToml
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
-> NonEmpty (Text, NaviNote)
-> PollInterval
-> RepeatEvent Text
-> ErrorNote
-> Event Text
mkMultipleEvent
        (MultipleToml
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)
        (MultipleToml
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)
        NonEmpty (Text, NaviNote)
triggerNotePairs
        PollInterval
pi
        RepeatEvent Text
repeatEvent
        ErrorNote
errorNote
  where
    triggerNotePairs :: NonEmpty (Text, NaviNote)
triggerNotePairs = forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap TriggerNoteToml -> (Text, NaviNote)
toPair (MultipleToml
toml forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall a. IsLabel "triggerNotes" a => a
#triggerNotes)
    toPair :: TriggerNoteToml -> (Text, NaviNote)
toPair (MkTriggerNoteToml Text
t NaviNote
n) = (Text -> Text
T.strip Text
t, NaviNote
n)
    pi :: PollInterval
pi = forall a. a -> Maybe a -> a
fromMaybe (Natural -> PollInterval
MkPollInterval Natural
30) (MultipleToml
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 #-}

mkMultipleEvent ::
  Maybe Text ->
  Command ->
  NonEmpty (Text, NaviNote) ->
  PollInterval ->
  RepeatEvent Text ->
  ErrorNote ->
  Event Text
mkMultipleEvent :: Maybe Text
-> Command
-> NonEmpty (Text, NaviNote)
-> PollInterval
-> RepeatEvent Text
-> ErrorNote
-> Event Text
mkMultipleEvent Maybe Text
mname Command
cmd NonEmpty (Text, NaviNote)
noteList PollInterval
pi RepeatEvent Text
re ErrorNote
en =
  MkEvent
    { $sel:name:MkEvent :: Text
name = Text
name',
      $sel:serviceType:MkEvent :: ServiceType Text
serviceType = Command -> ServiceType Text
Multiple Command
cmd,
      $sel:pollInterval:MkEvent :: PollInterval
pollInterval = PollInterval
pi,
      $sel:raiseAlert:MkEvent :: Text -> Maybe NaviNote
raiseAlert = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Map Text NaviNote
noteMap,
      $sel:repeatEvent:MkEvent :: RepeatEvent Text
repeatEvent = RepeatEvent Text
re,
      $sel:errorNote:MkEvent :: ErrorNote
errorNote = ErrorNote
en
    }
  where
    noteMap :: Map Text NaviNote
noteMap = forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList forall a b. (a -> b) -> a -> b
$ forall a. NonEmpty a -> [a]
NE.toList NonEmpty (Text, NaviNote)
noteList
    name' :: Text
name' = forall a. a -> Maybe a -> a
fromMaybe Text
"multiple" Maybe Text
mname
{-# INLINEABLE mkMultipleEvent #-}