module TD.Data.MessagePosition
  (MessagePosition(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I

data MessagePosition
  = MessagePosition -- ^ Contains information about a message in a specific position
    { MessagePosition -> Maybe Int
position   :: Maybe Int -- ^ 0-based message position in the full list of suitable messages
    , MessagePosition -> Maybe Int
message_id :: Maybe Int -- ^ Message identifier
    , MessagePosition -> Maybe Int
date       :: Maybe Int -- ^ Point in time (Unix timestamp) when the message was sent
    }
  deriving (MessagePosition -> MessagePosition -> Bool
(MessagePosition -> MessagePosition -> Bool)
-> (MessagePosition -> MessagePosition -> Bool)
-> Eq MessagePosition
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MessagePosition -> MessagePosition -> Bool
== :: MessagePosition -> MessagePosition -> Bool
$c/= :: MessagePosition -> MessagePosition -> Bool
/= :: MessagePosition -> MessagePosition -> Bool
Eq, Int -> MessagePosition -> ShowS
[MessagePosition] -> ShowS
MessagePosition -> String
(Int -> MessagePosition -> ShowS)
-> (MessagePosition -> String)
-> ([MessagePosition] -> ShowS)
-> Show MessagePosition
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MessagePosition -> ShowS
showsPrec :: Int -> MessagePosition -> ShowS
$cshow :: MessagePosition -> String
show :: MessagePosition -> String
$cshowList :: [MessagePosition] -> ShowS
showList :: [MessagePosition] -> ShowS
Show)

instance I.ShortShow MessagePosition where
  shortShow :: MessagePosition -> String
shortShow MessagePosition
    { position :: MessagePosition -> Maybe Int
position   = Maybe Int
position_
    , message_id :: MessagePosition -> Maybe Int
message_id = Maybe Int
message_id_
    , date :: MessagePosition -> Maybe Int
date       = Maybe Int
date_
    }
      = String
"MessagePosition"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"position"   String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
position_
        , String
"message_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
message_id_
        , String
"date"       String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
date_
        ]

instance AT.FromJSON MessagePosition where
  parseJSON :: Value -> Parser MessagePosition
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
"messagePosition" -> Value -> Parser MessagePosition
parseMessagePosition Value
v
      String
_                 -> Parser MessagePosition
forall a. Monoid a => a
mempty
    
    where
      parseMessagePosition :: A.Value -> AT.Parser MessagePosition
      parseMessagePosition :: Value -> Parser MessagePosition
parseMessagePosition = String
-> (Object -> Parser MessagePosition)
-> Value
-> Parser MessagePosition
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"MessagePosition" ((Object -> Parser MessagePosition)
 -> Value -> Parser MessagePosition)
-> (Object -> Parser MessagePosition)
-> Value
-> Parser MessagePosition
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
position_   <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"position"
        Maybe Int
message_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"message_id"
        Maybe Int
date_       <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"date"
        MessagePosition -> Parser MessagePosition
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MessagePosition -> Parser MessagePosition)
-> MessagePosition -> Parser MessagePosition
forall a b. (a -> b) -> a -> b
$ MessagePosition
          { position :: Maybe Int
position   = Maybe Int
position_
          , message_id :: Maybe Int
message_id = Maybe Int
message_id_
          , date :: Maybe Int
date       = Maybe Int
date_
          }
  parseJSON Value
_ = Parser MessagePosition
forall a. Monoid a => a
mempty