module TD.Data.StoryContent
  (StoryContent(..)) 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.Photo as Photo
import qualified TD.Data.StoryVideo as StoryVideo

-- | Contains the content of a story
data StoryContent
  = StoryContentPhoto -- ^ A photo story
    { StoryContent -> Maybe Photo
photo :: Maybe Photo.Photo -- ^ The photo
    }
  | StoryContentVideo -- ^ A video story
    { StoryContent -> Maybe StoryVideo
video             :: Maybe StoryVideo.StoryVideo -- ^ The video in MPEG4 format
    , StoryContent -> Maybe StoryVideo
alternative_video :: Maybe StoryVideo.StoryVideo -- ^ Alternative version of the video in MPEG4 format, encoded with H.264 codec; may be null
    }
  | StoryContentUnsupported -- ^ A story content that is not supported in the current TDLib version
  deriving (StoryContent -> StoryContent -> Bool
(StoryContent -> StoryContent -> Bool)
-> (StoryContent -> StoryContent -> Bool) -> Eq StoryContent
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StoryContent -> StoryContent -> Bool
== :: StoryContent -> StoryContent -> Bool
$c/= :: StoryContent -> StoryContent -> Bool
/= :: StoryContent -> StoryContent -> Bool
Eq, Int -> StoryContent -> ShowS
[StoryContent] -> ShowS
StoryContent -> String
(Int -> StoryContent -> ShowS)
-> (StoryContent -> String)
-> ([StoryContent] -> ShowS)
-> Show StoryContent
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StoryContent -> ShowS
showsPrec :: Int -> StoryContent -> ShowS
$cshow :: StoryContent -> String
show :: StoryContent -> String
$cshowList :: [StoryContent] -> ShowS
showList :: [StoryContent] -> ShowS
Show)

instance I.ShortShow StoryContent where
  shortShow :: StoryContent -> String
shortShow StoryContentPhoto
    { photo :: StoryContent -> Maybe Photo
photo = Maybe Photo
photo_
    }
      = String
"StoryContentPhoto"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"photo" String -> Maybe Photo -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Photo
photo_
        ]
  shortShow StoryContentVideo
    { video :: StoryContent -> Maybe StoryVideo
video             = Maybe StoryVideo
video_
    , alternative_video :: StoryContent -> Maybe StoryVideo
alternative_video = Maybe StoryVideo
alternative_video_
    }
      = String
"StoryContentVideo"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"video"             String -> Maybe StoryVideo -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe StoryVideo
video_
        , String
"alternative_video" String -> Maybe StoryVideo -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe StoryVideo
alternative_video_
        ]
  shortShow StoryContent
StoryContentUnsupported
      = String
"StoryContentUnsupported"

instance AT.FromJSON StoryContent where
  parseJSON :: Value -> Parser StoryContent
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
"storyContentPhoto"       -> Value -> Parser StoryContent
parseStoryContentPhoto Value
v
      String
"storyContentVideo"       -> Value -> Parser StoryContent
parseStoryContentVideo Value
v
      String
"storyContentUnsupported" -> StoryContent -> Parser StoryContent
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure StoryContent
StoryContentUnsupported
      String
_                         -> Parser StoryContent
forall a. Monoid a => a
mempty
    
    where
      parseStoryContentPhoto :: A.Value -> AT.Parser StoryContent
      parseStoryContentPhoto :: Value -> Parser StoryContent
parseStoryContentPhoto = String
-> (Object -> Parser StoryContent) -> Value -> Parser StoryContent
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"StoryContentPhoto" ((Object -> Parser StoryContent) -> Value -> Parser StoryContent)
-> (Object -> Parser StoryContent) -> Value -> Parser StoryContent
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Photo
photo_ <- Object
o Object -> Key -> Parser (Maybe Photo)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"photo"
        StoryContent -> Parser StoryContent
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StoryContent -> Parser StoryContent)
-> StoryContent -> Parser StoryContent
forall a b. (a -> b) -> a -> b
$ StoryContentPhoto
          { photo :: Maybe Photo
photo = Maybe Photo
photo_
          }
      parseStoryContentVideo :: A.Value -> AT.Parser StoryContent
      parseStoryContentVideo :: Value -> Parser StoryContent
parseStoryContentVideo = String
-> (Object -> Parser StoryContent) -> Value -> Parser StoryContent
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"StoryContentVideo" ((Object -> Parser StoryContent) -> Value -> Parser StoryContent)
-> (Object -> Parser StoryContent) -> Value -> Parser StoryContent
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe StoryVideo
video_             <- Object
o Object -> Key -> Parser (Maybe StoryVideo)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"video"
        Maybe StoryVideo
alternative_video_ <- Object
o Object -> Key -> Parser (Maybe StoryVideo)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"alternative_video"
        StoryContent -> Parser StoryContent
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StoryContent -> Parser StoryContent)
-> StoryContent -> Parser StoryContent
forall a b. (a -> b) -> a -> b
$ StoryContentVideo
          { video :: Maybe StoryVideo
video             = Maybe StoryVideo
video_
          , alternative_video :: Maybe StoryVideo
alternative_video = Maybe StoryVideo
alternative_video_
          }
  parseJSON Value
_ = Parser StoryContent
forall a. Monoid a => a
mempty