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

data ForwardSource
  = ForwardSource -- ^ Contains information about the last message from which a new message was forwarded last time
    { ForwardSource -> Maybe Int
chat_id     :: Maybe Int                         -- ^ Identifier of the chat to which the message that was forwarded belonged; may be 0 if unknown
    , ForwardSource -> Maybe Int
message_id  :: Maybe Int                         -- ^ Identifier of the message; may be 0 if unknown
    , ForwardSource -> Maybe MessageSender
sender_id   :: Maybe MessageSender.MessageSender -- ^ Identifier of the sender of the message; may be null if unknown or the new message was forwarded not to Saved Messages
    , ForwardSource -> Maybe Text
sender_name :: Maybe T.Text                      -- ^ Name of the sender of the message if the sender is hidden by their privacy settings
    , ForwardSource -> Maybe Int
date        :: Maybe Int                         -- ^ Point in time (Unix timestamp) when the message is sent; 0 if unknown
    , ForwardSource -> Maybe Bool
is_outgoing :: Maybe Bool                        -- ^ True, if the message that was forwarded is outgoing; always false if sender is unknown
    }
  deriving (ForwardSource -> ForwardSource -> Bool
(ForwardSource -> ForwardSource -> Bool)
-> (ForwardSource -> ForwardSource -> Bool) -> Eq ForwardSource
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ForwardSource -> ForwardSource -> Bool
== :: ForwardSource -> ForwardSource -> Bool
$c/= :: ForwardSource -> ForwardSource -> Bool
/= :: ForwardSource -> ForwardSource -> Bool
Eq, Int -> ForwardSource -> ShowS
[ForwardSource] -> ShowS
ForwardSource -> String
(Int -> ForwardSource -> ShowS)
-> (ForwardSource -> String)
-> ([ForwardSource] -> ShowS)
-> Show ForwardSource
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ForwardSource -> ShowS
showsPrec :: Int -> ForwardSource -> ShowS
$cshow :: ForwardSource -> String
show :: ForwardSource -> String
$cshowList :: [ForwardSource] -> ShowS
showList :: [ForwardSource] -> ShowS
Show)

instance I.ShortShow ForwardSource where
  shortShow :: ForwardSource -> String
shortShow ForwardSource
    { chat_id :: ForwardSource -> Maybe Int
chat_id     = Maybe Int
chat_id_
    , message_id :: ForwardSource -> Maybe Int
message_id  = Maybe Int
message_id_
    , sender_id :: ForwardSource -> Maybe MessageSender
sender_id   = Maybe MessageSender
sender_id_
    , sender_name :: ForwardSource -> Maybe Text
sender_name = Maybe Text
sender_name_
    , date :: ForwardSource -> Maybe Int
date        = Maybe Int
date_
    , is_outgoing :: ForwardSource -> Maybe Bool
is_outgoing = Maybe Bool
is_outgoing_
    }
      = String
"ForwardSource"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"chat_id"     String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
chat_id_
        , String
"message_id"  String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
message_id_
        , String
"sender_id"   String -> Maybe MessageSender -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe MessageSender
sender_id_
        , String
"sender_name" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
sender_name_
        , String
"date"        String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
date_
        , String
"is_outgoing" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_outgoing_
        ]

instance AT.FromJSON ForwardSource where
  parseJSON :: Value -> Parser ForwardSource
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
"forwardSource" -> Value -> Parser ForwardSource
parseForwardSource Value
v
      String
_               -> Parser ForwardSource
forall a. Monoid a => a
mempty
    
    where
      parseForwardSource :: A.Value -> AT.Parser ForwardSource
      parseForwardSource :: Value -> Parser ForwardSource
parseForwardSource = String
-> (Object -> Parser ForwardSource)
-> Value
-> Parser ForwardSource
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ForwardSource" ((Object -> Parser ForwardSource) -> Value -> Parser ForwardSource)
-> (Object -> Parser ForwardSource)
-> Value
-> Parser ForwardSource
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
chat_id_     <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"chat_id"
        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 MessageSender
sender_id_   <- Object
o Object -> Key -> Parser (Maybe MessageSender)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"sender_id"
        Maybe Text
sender_name_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"sender_name"
        Maybe Int
date_        <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"date"
        Maybe Bool
is_outgoing_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_outgoing"
        ForwardSource -> Parser ForwardSource
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ForwardSource -> Parser ForwardSource)
-> ForwardSource -> Parser ForwardSource
forall a b. (a -> b) -> a -> b
$ ForwardSource
          { chat_id :: Maybe Int
chat_id     = Maybe Int
chat_id_
          , message_id :: Maybe Int
message_id  = Maybe Int
message_id_
          , sender_id :: Maybe MessageSender
sender_id   = Maybe MessageSender
sender_id_
          , sender_name :: Maybe Text
sender_name = Maybe Text
sender_name_
          , date :: Maybe Int
date        = Maybe Int
date_
          , is_outgoing :: Maybe Bool
is_outgoing = Maybe Bool
is_outgoing_
          }
  parseJSON Value
_ = Parser ForwardSource
forall a. Monoid a => a
mempty