{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}

-- Derived from AWS service descriptions, licensed under Apache 2.0.

-- |
-- Module      : Amazonka.IoTEvents.Types.State
-- Copyright   : (c) 2013-2021 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay <brendan.g.hay+amazonka@gmail.com>
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
module Amazonka.IoTEvents.Types.State where

import qualified Amazonka.Core as Core
import Amazonka.IoTEvents.Types.OnEnterLifecycle
import Amazonka.IoTEvents.Types.OnExitLifecycle
import Amazonka.IoTEvents.Types.OnInputLifecycle
import qualified Amazonka.Lens as Lens
import qualified Amazonka.Prelude as Prelude

-- | Information that defines a state of a detector.
--
-- /See:/ 'newState' smart constructor.
data State = State'
  { -- | When entering this state, perform these @actions@ if the @condition@ is
    -- TRUE.
    State -> Maybe OnEnterLifecycle
onEnter :: Prelude.Maybe OnEnterLifecycle,
    -- | When an input is received and the @condition@ is TRUE, perform the
    -- specified @actions@.
    State -> Maybe OnInputLifecycle
onInput :: Prelude.Maybe OnInputLifecycle,
    -- | When exiting this state, perform these @actions@ if the specified
    -- @condition@ is @TRUE@.
    State -> Maybe OnExitLifecycle
onExit :: Prelude.Maybe OnExitLifecycle,
    -- | The name of the state.
    State -> Text
stateName :: Prelude.Text
  }
  deriving (State -> State -> Bool
(State -> State -> Bool) -> (State -> State -> Bool) -> Eq State
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: State -> State -> Bool
$c/= :: State -> State -> Bool
== :: State -> State -> Bool
$c== :: State -> State -> Bool
Prelude.Eq, ReadPrec [State]
ReadPrec State
Int -> ReadS State
ReadS [State]
(Int -> ReadS State)
-> ReadS [State]
-> ReadPrec State
-> ReadPrec [State]
-> Read State
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [State]
$creadListPrec :: ReadPrec [State]
readPrec :: ReadPrec State
$creadPrec :: ReadPrec State
readList :: ReadS [State]
$creadList :: ReadS [State]
readsPrec :: Int -> ReadS State
$creadsPrec :: Int -> ReadS State
Prelude.Read, Int -> State -> ShowS
[State] -> ShowS
State -> String
(Int -> State -> ShowS)
-> (State -> String) -> ([State] -> ShowS) -> Show State
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [State] -> ShowS
$cshowList :: [State] -> ShowS
show :: State -> String
$cshow :: State -> String
showsPrec :: Int -> State -> ShowS
$cshowsPrec :: Int -> State -> ShowS
Prelude.Show, (forall x. State -> Rep State x)
-> (forall x. Rep State x -> State) -> Generic State
forall x. Rep State x -> State
forall x. State -> Rep State x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep State x -> State
$cfrom :: forall x. State -> Rep State x
Prelude.Generic)

-- |
-- Create a value of 'State' with all optional fields omitted.
--
-- Use <https://hackage.haskell.org/package/generic-lens generic-lens> or <https://hackage.haskell.org/package/optics optics> to modify other optional fields.
--
-- The following record fields are available, with the corresponding lenses provided
-- for backwards compatibility:
--
-- 'onEnter', 'state_onEnter' - When entering this state, perform these @actions@ if the @condition@ is
-- TRUE.
--
-- 'onInput', 'state_onInput' - When an input is received and the @condition@ is TRUE, perform the
-- specified @actions@.
--
-- 'onExit', 'state_onExit' - When exiting this state, perform these @actions@ if the specified
-- @condition@ is @TRUE@.
--
-- 'stateName', 'state_stateName' - The name of the state.
newState ::
  -- | 'stateName'
  Prelude.Text ->
  State
newState :: Text -> State
newState Text
pStateName_ =
  State' :: Maybe OnEnterLifecycle
-> Maybe OnInputLifecycle -> Maybe OnExitLifecycle -> Text -> State
State'
    { $sel:onEnter:State' :: Maybe OnEnterLifecycle
onEnter = Maybe OnEnterLifecycle
forall a. Maybe a
Prelude.Nothing,
      $sel:onInput:State' :: Maybe OnInputLifecycle
onInput = Maybe OnInputLifecycle
forall a. Maybe a
Prelude.Nothing,
      $sel:onExit:State' :: Maybe OnExitLifecycle
onExit = Maybe OnExitLifecycle
forall a. Maybe a
Prelude.Nothing,
      $sel:stateName:State' :: Text
stateName = Text
pStateName_
    }

-- | When entering this state, perform these @actions@ if the @condition@ is
-- TRUE.
state_onEnter :: Lens.Lens' State (Prelude.Maybe OnEnterLifecycle)
state_onEnter :: (Maybe OnEnterLifecycle -> f (Maybe OnEnterLifecycle))
-> State -> f State
state_onEnter = (State -> Maybe OnEnterLifecycle)
-> (State -> Maybe OnEnterLifecycle -> State)
-> Lens
     State State (Maybe OnEnterLifecycle) (Maybe OnEnterLifecycle)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\State' {Maybe OnEnterLifecycle
onEnter :: Maybe OnEnterLifecycle
$sel:onEnter:State' :: State -> Maybe OnEnterLifecycle
onEnter} -> Maybe OnEnterLifecycle
onEnter) (\s :: State
s@State' {} Maybe OnEnterLifecycle
a -> State
s {$sel:onEnter:State' :: Maybe OnEnterLifecycle
onEnter = Maybe OnEnterLifecycle
a} :: State)

-- | When an input is received and the @condition@ is TRUE, perform the
-- specified @actions@.
state_onInput :: Lens.Lens' State (Prelude.Maybe OnInputLifecycle)
state_onInput :: (Maybe OnInputLifecycle -> f (Maybe OnInputLifecycle))
-> State -> f State
state_onInput = (State -> Maybe OnInputLifecycle)
-> (State -> Maybe OnInputLifecycle -> State)
-> Lens
     State State (Maybe OnInputLifecycle) (Maybe OnInputLifecycle)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\State' {Maybe OnInputLifecycle
onInput :: Maybe OnInputLifecycle
$sel:onInput:State' :: State -> Maybe OnInputLifecycle
onInput} -> Maybe OnInputLifecycle
onInput) (\s :: State
s@State' {} Maybe OnInputLifecycle
a -> State
s {$sel:onInput:State' :: Maybe OnInputLifecycle
onInput = Maybe OnInputLifecycle
a} :: State)

-- | When exiting this state, perform these @actions@ if the specified
-- @condition@ is @TRUE@.
state_onExit :: Lens.Lens' State (Prelude.Maybe OnExitLifecycle)
state_onExit :: (Maybe OnExitLifecycle -> f (Maybe OnExitLifecycle))
-> State -> f State
state_onExit = (State -> Maybe OnExitLifecycle)
-> (State -> Maybe OnExitLifecycle -> State)
-> Lens State State (Maybe OnExitLifecycle) (Maybe OnExitLifecycle)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\State' {Maybe OnExitLifecycle
onExit :: Maybe OnExitLifecycle
$sel:onExit:State' :: State -> Maybe OnExitLifecycle
onExit} -> Maybe OnExitLifecycle
onExit) (\s :: State
s@State' {} Maybe OnExitLifecycle
a -> State
s {$sel:onExit:State' :: Maybe OnExitLifecycle
onExit = Maybe OnExitLifecycle
a} :: State)

-- | The name of the state.
state_stateName :: Lens.Lens' State Prelude.Text
state_stateName :: (Text -> f Text) -> State -> f State
state_stateName = (State -> Text)
-> (State -> Text -> State) -> Lens State State Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\State' {Text
stateName :: Text
$sel:stateName:State' :: State -> Text
stateName} -> Text
stateName) (\s :: State
s@State' {} Text
a -> State
s {$sel:stateName:State' :: Text
stateName = Text
a} :: State)

instance Core.FromJSON State where
  parseJSON :: Value -> Parser State
parseJSON =
    String -> (Object -> Parser State) -> Value -> Parser State
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Core.withObject
      String
"State"
      ( \Object
x ->
          Maybe OnEnterLifecycle
-> Maybe OnInputLifecycle -> Maybe OnExitLifecycle -> Text -> State
State'
            (Maybe OnEnterLifecycle
 -> Maybe OnInputLifecycle
 -> Maybe OnExitLifecycle
 -> Text
 -> State)
-> Parser (Maybe OnEnterLifecycle)
-> Parser
     (Maybe OnInputLifecycle -> Maybe OnExitLifecycle -> Text -> State)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Text -> Parser (Maybe OnEnterLifecycle)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"onEnter")
            Parser
  (Maybe OnInputLifecycle -> Maybe OnExitLifecycle -> Text -> State)
-> Parser (Maybe OnInputLifecycle)
-> Parser (Maybe OnExitLifecycle -> Text -> State)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser (Maybe OnInputLifecycle)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"onInput")
            Parser (Maybe OnExitLifecycle -> Text -> State)
-> Parser (Maybe OnExitLifecycle) -> Parser (Text -> State)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser (Maybe OnExitLifecycle)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"onExit")
            Parser (Text -> State) -> Parser Text -> Parser State
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser Text
forall a. FromJSON a => Object -> Text -> Parser a
Core..: Text
"stateName")
      )

instance Prelude.Hashable State

instance Prelude.NFData State

instance Core.ToJSON State where
  toJSON :: State -> Value
toJSON State' {Maybe OnExitLifecycle
Maybe OnEnterLifecycle
Maybe OnInputLifecycle
Text
stateName :: Text
onExit :: Maybe OnExitLifecycle
onInput :: Maybe OnInputLifecycle
onEnter :: Maybe OnEnterLifecycle
$sel:stateName:State' :: State -> Text
$sel:onExit:State' :: State -> Maybe OnExitLifecycle
$sel:onInput:State' :: State -> Maybe OnInputLifecycle
$sel:onEnter:State' :: State -> Maybe OnEnterLifecycle
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"onEnter" Text -> OnEnterLifecycle -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) (OnEnterLifecycle -> Pair) -> Maybe OnEnterLifecycle -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe OnEnterLifecycle
onEnter,
            (Text
"onInput" Text -> OnInputLifecycle -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) (OnInputLifecycle -> Pair) -> Maybe OnInputLifecycle -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe OnInputLifecycle
onInput,
            (Text
"onExit" Text -> OnExitLifecycle -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) (OnExitLifecycle -> Pair) -> Maybe OnExitLifecycle -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe OnExitLifecycle
onExit,
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"stateName" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Text
stateName)
          ]
      )