module TD.Data.ReactionNotificationSettings
  ( ReactionNotificationSettings(..)    
  , defaultReactionNotificationSettings 
  ) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified TD.Data.ReactionNotificationSource as ReactionNotificationSource

data ReactionNotificationSettings
  = ReactionNotificationSettings -- ^ Contains information about notification settings for reactions
    { ReactionNotificationSettings -> Maybe ReactionNotificationSource
message_reaction_source :: Maybe ReactionNotificationSource.ReactionNotificationSource -- ^ Source of message reactions for which notifications are shown
    , ReactionNotificationSettings -> Maybe ReactionNotificationSource
story_reaction_source   :: Maybe ReactionNotificationSource.ReactionNotificationSource -- ^ Source of story reactions for which notifications are shown
    , ReactionNotificationSettings -> Maybe Int
sound_id                :: Maybe Int                                                   -- ^ Identifier of the notification sound to be played; 0 if sound is disabled
    , ReactionNotificationSettings -> Maybe Bool
show_preview            :: Maybe Bool                                                  -- ^ True, if reaction sender and emoji must be displayed in notifications
    }
  deriving (ReactionNotificationSettings
-> ReactionNotificationSettings -> Bool
(ReactionNotificationSettings
 -> ReactionNotificationSettings -> Bool)
-> (ReactionNotificationSettings
    -> ReactionNotificationSettings -> Bool)
-> Eq ReactionNotificationSettings
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ReactionNotificationSettings
-> ReactionNotificationSettings -> Bool
== :: ReactionNotificationSettings
-> ReactionNotificationSettings -> Bool
$c/= :: ReactionNotificationSettings
-> ReactionNotificationSettings -> Bool
/= :: ReactionNotificationSettings
-> ReactionNotificationSettings -> Bool
Eq, Int -> ReactionNotificationSettings -> ShowS
[ReactionNotificationSettings] -> ShowS
ReactionNotificationSettings -> String
(Int -> ReactionNotificationSettings -> ShowS)
-> (ReactionNotificationSettings -> String)
-> ([ReactionNotificationSettings] -> ShowS)
-> Show ReactionNotificationSettings
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ReactionNotificationSettings -> ShowS
showsPrec :: Int -> ReactionNotificationSettings -> ShowS
$cshow :: ReactionNotificationSettings -> String
show :: ReactionNotificationSettings -> String
$cshowList :: [ReactionNotificationSettings] -> ShowS
showList :: [ReactionNotificationSettings] -> ShowS
Show)

instance I.ShortShow ReactionNotificationSettings where
  shortShow :: ReactionNotificationSettings -> String
shortShow ReactionNotificationSettings
    { message_reaction_source :: ReactionNotificationSettings -> Maybe ReactionNotificationSource
message_reaction_source = Maybe ReactionNotificationSource
message_reaction_source_
    , story_reaction_source :: ReactionNotificationSettings -> Maybe ReactionNotificationSource
story_reaction_source   = Maybe ReactionNotificationSource
story_reaction_source_
    , sound_id :: ReactionNotificationSettings -> Maybe Int
sound_id                = Maybe Int
sound_id_
    , show_preview :: ReactionNotificationSettings -> Maybe Bool
show_preview            = Maybe Bool
show_preview_
    }
      = String
"ReactionNotificationSettings"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"message_reaction_source" String -> Maybe ReactionNotificationSource -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe ReactionNotificationSource
message_reaction_source_
        , String
"story_reaction_source"   String -> Maybe ReactionNotificationSource -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe ReactionNotificationSource
story_reaction_source_
        , String
"sound_id"                String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
sound_id_
        , String
"show_preview"            String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
show_preview_
        ]

instance AT.FromJSON ReactionNotificationSettings where
  parseJSON :: Value -> Parser ReactionNotificationSettings
parseJSON v :: Value
v@(AT.Object Object
obj) = do
    String
t <- Object
obj Object -> Key -> Parser String
forall a. FromJSON a => Object -> Key -> Parser a
A..: Key
"@type" :: AT.Parser String

    case String
t of
      String
"reactionNotificationSettings" -> Value -> Parser ReactionNotificationSettings
parseReactionNotificationSettings Value
v
      String
_                              -> Parser ReactionNotificationSettings
forall a. Monoid a => a
mempty
    
    where
      parseReactionNotificationSettings :: A.Value -> AT.Parser ReactionNotificationSettings
      parseReactionNotificationSettings :: Value -> Parser ReactionNotificationSettings
parseReactionNotificationSettings = String
-> (Object -> Parser ReactionNotificationSettings)
-> Value
-> Parser ReactionNotificationSettings
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ReactionNotificationSettings" ((Object -> Parser ReactionNotificationSettings)
 -> Value -> Parser ReactionNotificationSettings)
-> (Object -> Parser ReactionNotificationSettings)
-> Value
-> Parser ReactionNotificationSettings
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe ReactionNotificationSource
message_reaction_source_ <- Object
o Object -> Key -> Parser (Maybe ReactionNotificationSource)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"message_reaction_source"
        Maybe ReactionNotificationSource
story_reaction_source_   <- Object
o Object -> Key -> Parser (Maybe ReactionNotificationSource)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"story_reaction_source"
        Maybe Int
sound_id_                <- (String -> Int) -> Maybe String -> Maybe Int
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Int
I.readInt64 (Maybe String -> Maybe Int)
-> Parser (Maybe String) -> Parser (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser (Maybe String)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"sound_id"
        Maybe Bool
show_preview_            <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"show_preview"
        ReactionNotificationSettings -> Parser ReactionNotificationSettings
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ReactionNotificationSettings
 -> Parser ReactionNotificationSettings)
-> ReactionNotificationSettings
-> Parser ReactionNotificationSettings
forall a b. (a -> b) -> a -> b
$ ReactionNotificationSettings
          { message_reaction_source :: Maybe ReactionNotificationSource
message_reaction_source = Maybe ReactionNotificationSource
message_reaction_source_
          , story_reaction_source :: Maybe ReactionNotificationSource
story_reaction_source   = Maybe ReactionNotificationSource
story_reaction_source_
          , sound_id :: Maybe Int
sound_id                = Maybe Int
sound_id_
          , show_preview :: Maybe Bool
show_preview            = Maybe Bool
show_preview_
          }
  parseJSON Value
_ = Parser ReactionNotificationSettings
forall a. Monoid a => a
mempty

instance AT.ToJSON ReactionNotificationSettings where
  toJSON :: ReactionNotificationSettings -> Value
toJSON ReactionNotificationSettings
    { message_reaction_source :: ReactionNotificationSettings -> Maybe ReactionNotificationSource
message_reaction_source = Maybe ReactionNotificationSource
message_reaction_source_
    , story_reaction_source :: ReactionNotificationSettings -> Maybe ReactionNotificationSource
story_reaction_source   = Maybe ReactionNotificationSource
story_reaction_source_
    , sound_id :: ReactionNotificationSettings -> Maybe Int
sound_id                = Maybe Int
sound_id_
    , show_preview :: ReactionNotificationSettings -> Maybe Bool
show_preview            = Maybe Bool
show_preview_
    }
      = [Pair] -> Value
A.object
        [ Key
"@type"                   Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"reactionNotificationSettings"
        , Key
"message_reaction_source" Key -> Maybe ReactionNotificationSource -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe ReactionNotificationSource
message_reaction_source_
        , Key
"story_reaction_source"   Key -> Maybe ReactionNotificationSource -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe ReactionNotificationSource
story_reaction_source_
        , Key
"sound_id"                Key -> Maybe Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= (Int -> Value) -> Maybe Int -> Maybe Value
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int -> Value
I.writeInt64  Maybe Int
sound_id_
        , Key
"show_preview"            Key -> Maybe Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Bool
show_preview_
        ]

defaultReactionNotificationSettings :: ReactionNotificationSettings
defaultReactionNotificationSettings :: ReactionNotificationSettings
defaultReactionNotificationSettings =
  ReactionNotificationSettings
    { message_reaction_source :: Maybe ReactionNotificationSource
message_reaction_source = Maybe ReactionNotificationSource
forall a. Maybe a
Nothing
    , story_reaction_source :: Maybe ReactionNotificationSource
story_reaction_source   = Maybe ReactionNotificationSource
forall a. Maybe a
Nothing
    , sound_id :: Maybe Int
sound_id                = Maybe Int
forall a. Maybe a
Nothing
    , show_preview :: Maybe Bool
show_preview            = Maybe Bool
forall a. Maybe a
Nothing
    }