module TD.Data.FoundStories
  (FoundStories(..)) 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.Story as Story
import qualified Data.Text as T

data FoundStories
  = FoundStories -- ^ Contains a list of stories found by a search
    { FoundStories -> Maybe Int
total_count :: Maybe Int           -- ^ Approximate total number of stories found
    , FoundStories -> Maybe [Story]
stories     :: Maybe [Story.Story] -- ^ List of stories
    , FoundStories -> Maybe Text
next_offset :: Maybe T.Text        -- ^ The offset for the next request. If empty, then there are no more results
    }
  deriving (FoundStories -> FoundStories -> Bool
(FoundStories -> FoundStories -> Bool)
-> (FoundStories -> FoundStories -> Bool) -> Eq FoundStories
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FoundStories -> FoundStories -> Bool
== :: FoundStories -> FoundStories -> Bool
$c/= :: FoundStories -> FoundStories -> Bool
/= :: FoundStories -> FoundStories -> Bool
Eq, Int -> FoundStories -> ShowS
[FoundStories] -> ShowS
FoundStories -> String
(Int -> FoundStories -> ShowS)
-> (FoundStories -> String)
-> ([FoundStories] -> ShowS)
-> Show FoundStories
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FoundStories -> ShowS
showsPrec :: Int -> FoundStories -> ShowS
$cshow :: FoundStories -> String
show :: FoundStories -> String
$cshowList :: [FoundStories] -> ShowS
showList :: [FoundStories] -> ShowS
Show)

instance I.ShortShow FoundStories where
  shortShow :: FoundStories -> String
shortShow FoundStories
    { total_count :: FoundStories -> Maybe Int
total_count = Maybe Int
total_count_
    , stories :: FoundStories -> Maybe [Story]
stories     = Maybe [Story]
stories_
    , next_offset :: FoundStories -> Maybe Text
next_offset = Maybe Text
next_offset_
    }
      = String
"FoundStories"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"total_count" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
total_count_
        , String
"stories"     String -> Maybe [Story] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [Story]
stories_
        , String
"next_offset" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
next_offset_
        ]

instance AT.FromJSON FoundStories where
  parseJSON :: Value -> Parser FoundStories
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
"foundStories" -> Value -> Parser FoundStories
parseFoundStories Value
v
      String
_              -> Parser FoundStories
forall a. Monoid a => a
mempty
    
    where
      parseFoundStories :: A.Value -> AT.Parser FoundStories
      parseFoundStories :: Value -> Parser FoundStories
parseFoundStories = String
-> (Object -> Parser FoundStories) -> Value -> Parser FoundStories
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"FoundStories" ((Object -> Parser FoundStories) -> Value -> Parser FoundStories)
-> (Object -> Parser FoundStories) -> Value -> Parser FoundStories
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
total_count_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"total_count"
        Maybe [Story]
stories_     <- Object
o Object -> Key -> Parser (Maybe [Story])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"stories"
        Maybe Text
next_offset_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"next_offset"
        FoundStories -> Parser FoundStories
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (FoundStories -> Parser FoundStories)
-> FoundStories -> Parser FoundStories
forall a b. (a -> b) -> a -> b
$ FoundStories
          { total_count :: Maybe Int
total_count = Maybe Int
total_count_
          , stories :: Maybe [Story]
stories     = Maybe [Story]
stories_
          , next_offset :: Maybe Text
next_offset = Maybe Text
next_offset_
          }
  parseJSON Value
_ = Parser FoundStories
forall a. Monoid a => a
mempty