module TD.Data.MessagePositions
  (MessagePositions(..)) 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.MessagePosition as MessagePosition

data MessagePositions
  = MessagePositions -- ^ Contains a list of message positions
    { MessagePositions -> Maybe Int
total_count :: Maybe Int                               -- ^ Total number of messages found
    , MessagePositions -> Maybe [MessagePosition]
positions   :: Maybe [MessagePosition.MessagePosition] -- ^ List of message positions
    }
  deriving (MessagePositions -> MessagePositions -> Bool
(MessagePositions -> MessagePositions -> Bool)
-> (MessagePositions -> MessagePositions -> Bool)
-> Eq MessagePositions
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MessagePositions -> MessagePositions -> Bool
== :: MessagePositions -> MessagePositions -> Bool
$c/= :: MessagePositions -> MessagePositions -> Bool
/= :: MessagePositions -> MessagePositions -> Bool
Eq, Int -> MessagePositions -> ShowS
[MessagePositions] -> ShowS
MessagePositions -> String
(Int -> MessagePositions -> ShowS)
-> (MessagePositions -> String)
-> ([MessagePositions] -> ShowS)
-> Show MessagePositions
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MessagePositions -> ShowS
showsPrec :: Int -> MessagePositions -> ShowS
$cshow :: MessagePositions -> String
show :: MessagePositions -> String
$cshowList :: [MessagePositions] -> ShowS
showList :: [MessagePositions] -> ShowS
Show)

instance I.ShortShow MessagePositions where
  shortShow :: MessagePositions -> String
shortShow MessagePositions
    { total_count :: MessagePositions -> Maybe Int
total_count = Maybe Int
total_count_
    , positions :: MessagePositions -> Maybe [MessagePosition]
positions   = Maybe [MessagePosition]
positions_
    }
      = String
"MessagePositions"
        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
"positions"   String -> Maybe [MessagePosition] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [MessagePosition]
positions_
        ]

instance AT.FromJSON MessagePositions where
  parseJSON :: Value -> Parser MessagePositions
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
"messagePositions" -> Value -> Parser MessagePositions
parseMessagePositions Value
v
      String
_                  -> Parser MessagePositions
forall a. Monoid a => a
mempty
    
    where
      parseMessagePositions :: A.Value -> AT.Parser MessagePositions
      parseMessagePositions :: Value -> Parser MessagePositions
parseMessagePositions = String
-> (Object -> Parser MessagePositions)
-> Value
-> Parser MessagePositions
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"MessagePositions" ((Object -> Parser MessagePositions)
 -> Value -> Parser MessagePositions)
-> (Object -> Parser MessagePositions)
-> Value
-> Parser MessagePositions
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 [MessagePosition]
positions_   <- Object
o Object -> Key -> Parser (Maybe [MessagePosition])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"positions"
        MessagePositions -> Parser MessagePositions
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MessagePositions -> Parser MessagePositions)
-> MessagePositions -> Parser MessagePositions
forall a b. (a -> b) -> a -> b
$ MessagePositions
          { total_count :: Maybe Int
total_count = Maybe Int
total_count_
          , positions :: Maybe [MessagePosition]
positions   = Maybe [MessagePosition]
positions_
          }
  parseJSON Value
_ = Parser MessagePositions
forall a. Monoid a => a
mempty