module TD.Data.BotMediaPreview
  (BotMediaPreview(..)) 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.StoryContent as StoryContent

data BotMediaPreview
  = BotMediaPreview -- ^ Describes media previews of a bot
    { BotMediaPreview -> Maybe Int
date    :: Maybe Int                       -- ^ Point in time (Unix timestamp) when the preview was added or changed last time
    , BotMediaPreview -> Maybe StoryContent
content :: Maybe StoryContent.StoryContent -- ^ Content of the preview
    }
  deriving (BotMediaPreview -> BotMediaPreview -> Bool
(BotMediaPreview -> BotMediaPreview -> Bool)
-> (BotMediaPreview -> BotMediaPreview -> Bool)
-> Eq BotMediaPreview
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BotMediaPreview -> BotMediaPreview -> Bool
== :: BotMediaPreview -> BotMediaPreview -> Bool
$c/= :: BotMediaPreview -> BotMediaPreview -> Bool
/= :: BotMediaPreview -> BotMediaPreview -> Bool
Eq, Int -> BotMediaPreview -> ShowS
[BotMediaPreview] -> ShowS
BotMediaPreview -> String
(Int -> BotMediaPreview -> ShowS)
-> (BotMediaPreview -> String)
-> ([BotMediaPreview] -> ShowS)
-> Show BotMediaPreview
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BotMediaPreview -> ShowS
showsPrec :: Int -> BotMediaPreview -> ShowS
$cshow :: BotMediaPreview -> String
show :: BotMediaPreview -> String
$cshowList :: [BotMediaPreview] -> ShowS
showList :: [BotMediaPreview] -> ShowS
Show)

instance I.ShortShow BotMediaPreview where
  shortShow :: BotMediaPreview -> String
shortShow BotMediaPreview
    { date :: BotMediaPreview -> Maybe Int
date    = Maybe Int
date_
    , content :: BotMediaPreview -> Maybe StoryContent
content = Maybe StoryContent
content_
    }
      = String
"BotMediaPreview"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"date"    String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
date_
        , String
"content" String -> Maybe StoryContent -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe StoryContent
content_
        ]

instance AT.FromJSON BotMediaPreview where
  parseJSON :: Value -> Parser BotMediaPreview
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
"botMediaPreview" -> Value -> Parser BotMediaPreview
parseBotMediaPreview Value
v
      String
_                 -> Parser BotMediaPreview
forall a. Monoid a => a
mempty
    
    where
      parseBotMediaPreview :: A.Value -> AT.Parser BotMediaPreview
      parseBotMediaPreview :: Value -> Parser BotMediaPreview
parseBotMediaPreview = String
-> (Object -> Parser BotMediaPreview)
-> Value
-> Parser BotMediaPreview
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"BotMediaPreview" ((Object -> Parser BotMediaPreview)
 -> Value -> Parser BotMediaPreview)
-> (Object -> Parser BotMediaPreview)
-> Value
-> Parser BotMediaPreview
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
date_    <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"date"
        Maybe StoryContent
content_ <- Object
o Object -> Key -> Parser (Maybe StoryContent)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"content"
        BotMediaPreview -> Parser BotMediaPreview
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BotMediaPreview -> Parser BotMediaPreview)
-> BotMediaPreview -> Parser BotMediaPreview
forall a b. (a -> b) -> a -> b
$ BotMediaPreview
          { date :: Maybe Int
date    = Maybe Int
date_
          , content :: Maybe StoryContent
content = Maybe StoryContent
content_
          }
  parseJSON Value
_ = Parser BotMediaPreview
forall a. Monoid a => a
mempty