{-# 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.Pinpoint.Types.Condition
-- 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.Pinpoint.Types.Condition where

import qualified Amazonka.Core as Core
import qualified Amazonka.Lens as Lens
import Amazonka.Pinpoint.Types.Operator
import Amazonka.Pinpoint.Types.SimpleCondition
import qualified Amazonka.Prelude as Prelude

-- | Specifies the conditions to evaluate for an activity in a journey, and
-- how to evaluate those conditions.
--
-- /See:/ 'newCondition' smart constructor.
data Condition = Condition'
  { -- | Specifies how to handle multiple conditions for the activity. For
    -- example, if you specify two conditions for an activity, whether both or
    -- only one of the conditions must be met for the activity to be performed.
    Condition -> Maybe Operator
operator :: Prelude.Maybe Operator,
    -- | The conditions to evaluate for the activity.
    Condition -> Maybe [SimpleCondition]
conditions :: Prelude.Maybe [SimpleCondition]
  }
  deriving (Condition -> Condition -> Bool
(Condition -> Condition -> Bool)
-> (Condition -> Condition -> Bool) -> Eq Condition
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Condition -> Condition -> Bool
$c/= :: Condition -> Condition -> Bool
== :: Condition -> Condition -> Bool
$c== :: Condition -> Condition -> Bool
Prelude.Eq, ReadPrec [Condition]
ReadPrec Condition
Int -> ReadS Condition
ReadS [Condition]
(Int -> ReadS Condition)
-> ReadS [Condition]
-> ReadPrec Condition
-> ReadPrec [Condition]
-> Read Condition
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Condition]
$creadListPrec :: ReadPrec [Condition]
readPrec :: ReadPrec Condition
$creadPrec :: ReadPrec Condition
readList :: ReadS [Condition]
$creadList :: ReadS [Condition]
readsPrec :: Int -> ReadS Condition
$creadsPrec :: Int -> ReadS Condition
Prelude.Read, Int -> Condition -> ShowS
[Condition] -> ShowS
Condition -> String
(Int -> Condition -> ShowS)
-> (Condition -> String)
-> ([Condition] -> ShowS)
-> Show Condition
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Condition] -> ShowS
$cshowList :: [Condition] -> ShowS
show :: Condition -> String
$cshow :: Condition -> String
showsPrec :: Int -> Condition -> ShowS
$cshowsPrec :: Int -> Condition -> ShowS
Prelude.Show, (forall x. Condition -> Rep Condition x)
-> (forall x. Rep Condition x -> Condition) -> Generic Condition
forall x. Rep Condition x -> Condition
forall x. Condition -> Rep Condition x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Condition x -> Condition
$cfrom :: forall x. Condition -> Rep Condition x
Prelude.Generic)

-- |
-- Create a value of 'Condition' 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:
--
-- 'operator', 'condition_operator' - Specifies how to handle multiple conditions for the activity. For
-- example, if you specify two conditions for an activity, whether both or
-- only one of the conditions must be met for the activity to be performed.
--
-- 'conditions', 'condition_conditions' - The conditions to evaluate for the activity.
newCondition ::
  Condition
newCondition :: Condition
newCondition =
  Condition' :: Maybe Operator -> Maybe [SimpleCondition] -> Condition
Condition'
    { $sel:operator:Condition' :: Maybe Operator
operator = Maybe Operator
forall a. Maybe a
Prelude.Nothing,
      $sel:conditions:Condition' :: Maybe [SimpleCondition]
conditions = Maybe [SimpleCondition]
forall a. Maybe a
Prelude.Nothing
    }

-- | Specifies how to handle multiple conditions for the activity. For
-- example, if you specify two conditions for an activity, whether both or
-- only one of the conditions must be met for the activity to be performed.
condition_operator :: Lens.Lens' Condition (Prelude.Maybe Operator)
condition_operator :: (Maybe Operator -> f (Maybe Operator)) -> Condition -> f Condition
condition_operator = (Condition -> Maybe Operator)
-> (Condition -> Maybe Operator -> Condition)
-> Lens Condition Condition (Maybe Operator) (Maybe Operator)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Condition' {Maybe Operator
operator :: Maybe Operator
$sel:operator:Condition' :: Condition -> Maybe Operator
operator} -> Maybe Operator
operator) (\s :: Condition
s@Condition' {} Maybe Operator
a -> Condition
s {$sel:operator:Condition' :: Maybe Operator
operator = Maybe Operator
a} :: Condition)

-- | The conditions to evaluate for the activity.
condition_conditions :: Lens.Lens' Condition (Prelude.Maybe [SimpleCondition])
condition_conditions :: (Maybe [SimpleCondition] -> f (Maybe [SimpleCondition]))
-> Condition -> f Condition
condition_conditions = (Condition -> Maybe [SimpleCondition])
-> (Condition -> Maybe [SimpleCondition] -> Condition)
-> Lens
     Condition
     Condition
     (Maybe [SimpleCondition])
     (Maybe [SimpleCondition])
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Condition' {Maybe [SimpleCondition]
conditions :: Maybe [SimpleCondition]
$sel:conditions:Condition' :: Condition -> Maybe [SimpleCondition]
conditions} -> Maybe [SimpleCondition]
conditions) (\s :: Condition
s@Condition' {} Maybe [SimpleCondition]
a -> Condition
s {$sel:conditions:Condition' :: Maybe [SimpleCondition]
conditions = Maybe [SimpleCondition]
a} :: Condition) ((Maybe [SimpleCondition] -> f (Maybe [SimpleCondition]))
 -> Condition -> f Condition)
-> ((Maybe [SimpleCondition] -> f (Maybe [SimpleCondition]))
    -> Maybe [SimpleCondition] -> f (Maybe [SimpleCondition]))
-> (Maybe [SimpleCondition] -> f (Maybe [SimpleCondition]))
-> Condition
-> f Condition
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. AnIso
  [SimpleCondition]
  [SimpleCondition]
  [SimpleCondition]
  [SimpleCondition]
-> Iso
     (Maybe [SimpleCondition])
     (Maybe [SimpleCondition])
     (Maybe [SimpleCondition])
     (Maybe [SimpleCondition])
forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
Lens.mapping AnIso
  [SimpleCondition]
  [SimpleCondition]
  [SimpleCondition]
  [SimpleCondition]
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

instance Core.FromJSON Condition where
  parseJSON :: Value -> Parser Condition
parseJSON =
    String -> (Object -> Parser Condition) -> Value -> Parser Condition
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Core.withObject
      String
"Condition"
      ( \Object
x ->
          Maybe Operator -> Maybe [SimpleCondition] -> Condition
Condition'
            (Maybe Operator -> Maybe [SimpleCondition] -> Condition)
-> Parser (Maybe Operator)
-> Parser (Maybe [SimpleCondition] -> Condition)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Text -> Parser (Maybe Operator)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"Operator")
            Parser (Maybe [SimpleCondition] -> Condition)
-> Parser (Maybe [SimpleCondition]) -> Parser Condition
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser (Maybe (Maybe [SimpleCondition]))
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"Conditions" Parser (Maybe (Maybe [SimpleCondition]))
-> Maybe [SimpleCondition] -> Parser (Maybe [SimpleCondition])
forall a. Parser (Maybe a) -> a -> Parser a
Core..!= Maybe [SimpleCondition]
forall a. Monoid a => a
Prelude.mempty)
      )

instance Prelude.Hashable Condition

instance Prelude.NFData Condition

instance Core.ToJSON Condition where
  toJSON :: Condition -> Value
toJSON Condition' {Maybe [SimpleCondition]
Maybe Operator
conditions :: Maybe [SimpleCondition]
operator :: Maybe Operator
$sel:conditions:Condition' :: Condition -> Maybe [SimpleCondition]
$sel:operator:Condition' :: Condition -> Maybe Operator
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"Operator" Text -> Operator -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) (Operator -> Pair) -> Maybe Operator -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Operator
operator,
            (Text
"Conditions" Text -> [SimpleCondition] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) ([SimpleCondition] -> Pair)
-> Maybe [SimpleCondition] -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe [SimpleCondition]
conditions
          ]
      )