module TD.Data.ChatEvents
  (ChatEvents(..)) 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.ChatEvent as ChatEvent

data ChatEvents
  = ChatEvents -- ^ Contains a list of chat events
    { ChatEvents -> Maybe [ChatEvent]
events :: Maybe [ChatEvent.ChatEvent] -- ^ List of events
    }
  deriving (ChatEvents -> ChatEvents -> Bool
(ChatEvents -> ChatEvents -> Bool)
-> (ChatEvents -> ChatEvents -> Bool) -> Eq ChatEvents
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ChatEvents -> ChatEvents -> Bool
== :: ChatEvents -> ChatEvents -> Bool
$c/= :: ChatEvents -> ChatEvents -> Bool
/= :: ChatEvents -> ChatEvents -> Bool
Eq, Int -> ChatEvents -> ShowS
[ChatEvents] -> ShowS
ChatEvents -> String
(Int -> ChatEvents -> ShowS)
-> (ChatEvents -> String)
-> ([ChatEvents] -> ShowS)
-> Show ChatEvents
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ChatEvents -> ShowS
showsPrec :: Int -> ChatEvents -> ShowS
$cshow :: ChatEvents -> String
show :: ChatEvents -> String
$cshowList :: [ChatEvents] -> ShowS
showList :: [ChatEvents] -> ShowS
Show)

instance I.ShortShow ChatEvents where
  shortShow :: ChatEvents -> String
shortShow ChatEvents
    { events :: ChatEvents -> Maybe [ChatEvent]
events = Maybe [ChatEvent]
events_
    }
      = String
"ChatEvents"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"events" String -> Maybe [ChatEvent] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [ChatEvent]
events_
        ]

instance AT.FromJSON ChatEvents where
  parseJSON :: Value -> Parser ChatEvents
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
"chatEvents" -> Value -> Parser ChatEvents
parseChatEvents Value
v
      String
_            -> Parser ChatEvents
forall a. Monoid a => a
mempty
    
    where
      parseChatEvents :: A.Value -> AT.Parser ChatEvents
      parseChatEvents :: Value -> Parser ChatEvents
parseChatEvents = String
-> (Object -> Parser ChatEvents) -> Value -> Parser ChatEvents
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ChatEvents" ((Object -> Parser ChatEvents) -> Value -> Parser ChatEvents)
-> (Object -> Parser ChatEvents) -> Value -> Parser ChatEvents
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe [ChatEvent]
events_ <- Object
o Object -> Key -> Parser (Maybe [ChatEvent])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"events"
        ChatEvents -> Parser ChatEvents
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ChatEvents -> Parser ChatEvents)
-> ChatEvents -> Parser ChatEvents
forall a b. (a -> b) -> a -> b
$ ChatEvents
          { events :: Maybe [ChatEvent]
events = Maybe [ChatEvent]
events_
          }
  parseJSON Value
_ = Parser ChatEvents
forall a. Monoid a => a
mempty