module TD.Data.MessageSendingState
  (MessageSendingState(..)) 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.Error as Error

-- | Contains information about the sending state of the message
data MessageSendingState
  = MessageSendingStatePending -- ^ The message is being sent now, but has not yet been delivered to the server
    { MessageSendingState -> Maybe Int
sending_id :: Maybe Int -- ^ Non-persistent message sending identifier, specified by the application
    }
  | MessageSendingStateFailed -- ^ The message failed to be sent
    { MessageSendingState -> Maybe Error
_error                   :: Maybe Error.Error -- ^ The cause of the message sending failure
    , MessageSendingState -> Maybe Bool
can_retry                :: Maybe Bool        -- ^ True, if the message can be re-sent using resendMessages or readdQuickReplyShortcutMessages
    , MessageSendingState -> Maybe Bool
need_another_sender      :: Maybe Bool        -- ^ True, if the message can be re-sent only on behalf of a different sender
    , MessageSendingState -> Maybe Bool
need_another_reply_quote :: Maybe Bool        -- ^ True, if the message can be re-sent only if another quote is chosen in the message that is replied by the given message
    , MessageSendingState -> Maybe Bool
need_drop_reply          :: Maybe Bool        -- ^ True, if the message can be re-sent only if the message to be replied is removed. This will be done automatically by resendMessages
    , MessageSendingState -> Maybe Double
retry_after              :: Maybe Double      -- ^ Time left before the message can be re-sent, in seconds. No update is sent when this field changes
    }
  deriving (MessageSendingState -> MessageSendingState -> Bool
(MessageSendingState -> MessageSendingState -> Bool)
-> (MessageSendingState -> MessageSendingState -> Bool)
-> Eq MessageSendingState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MessageSendingState -> MessageSendingState -> Bool
== :: MessageSendingState -> MessageSendingState -> Bool
$c/= :: MessageSendingState -> MessageSendingState -> Bool
/= :: MessageSendingState -> MessageSendingState -> Bool
Eq, Int -> MessageSendingState -> ShowS
[MessageSendingState] -> ShowS
MessageSendingState -> String
(Int -> MessageSendingState -> ShowS)
-> (MessageSendingState -> String)
-> ([MessageSendingState] -> ShowS)
-> Show MessageSendingState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MessageSendingState -> ShowS
showsPrec :: Int -> MessageSendingState -> ShowS
$cshow :: MessageSendingState -> String
show :: MessageSendingState -> String
$cshowList :: [MessageSendingState] -> ShowS
showList :: [MessageSendingState] -> ShowS
Show)

instance I.ShortShow MessageSendingState where
  shortShow :: MessageSendingState -> String
shortShow MessageSendingStatePending
    { sending_id :: MessageSendingState -> Maybe Int
sending_id = Maybe Int
sending_id_
    }
      = String
"MessageSendingStatePending"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"sending_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
sending_id_
        ]
  shortShow MessageSendingStateFailed
    { _error :: MessageSendingState -> Maybe Error
_error                   = Maybe Error
_error_
    , can_retry :: MessageSendingState -> Maybe Bool
can_retry                = Maybe Bool
can_retry_
    , need_another_sender :: MessageSendingState -> Maybe Bool
need_another_sender      = Maybe Bool
need_another_sender_
    , need_another_reply_quote :: MessageSendingState -> Maybe Bool
need_another_reply_quote = Maybe Bool
need_another_reply_quote_
    , need_drop_reply :: MessageSendingState -> Maybe Bool
need_drop_reply          = Maybe Bool
need_drop_reply_
    , retry_after :: MessageSendingState -> Maybe Double
retry_after              = Maybe Double
retry_after_
    }
      = String
"MessageSendingStateFailed"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_error"                   String -> Maybe Error -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Error
_error_
        , String
"can_retry"                String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
can_retry_
        , String
"need_another_sender"      String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
need_another_sender_
        , String
"need_another_reply_quote" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
need_another_reply_quote_
        , String
"need_drop_reply"          String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
need_drop_reply_
        , String
"retry_after"              String -> Maybe Double -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Double
retry_after_
        ]

instance AT.FromJSON MessageSendingState where
  parseJSON :: Value -> Parser MessageSendingState
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
"messageSendingStatePending" -> Value -> Parser MessageSendingState
parseMessageSendingStatePending Value
v
      String
"messageSendingStateFailed"  -> Value -> Parser MessageSendingState
parseMessageSendingStateFailed Value
v
      String
_                            -> Parser MessageSendingState
forall a. Monoid a => a
mempty
    
    where
      parseMessageSendingStatePending :: A.Value -> AT.Parser MessageSendingState
      parseMessageSendingStatePending :: Value -> Parser MessageSendingState
parseMessageSendingStatePending = String
-> (Object -> Parser MessageSendingState)
-> Value
-> Parser MessageSendingState
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"MessageSendingStatePending" ((Object -> Parser MessageSendingState)
 -> Value -> Parser MessageSendingState)
-> (Object -> Parser MessageSendingState)
-> Value
-> Parser MessageSendingState
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
sending_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"sending_id"
        MessageSendingState -> Parser MessageSendingState
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MessageSendingState -> Parser MessageSendingState)
-> MessageSendingState -> Parser MessageSendingState
forall a b. (a -> b) -> a -> b
$ MessageSendingStatePending
          { sending_id :: Maybe Int
sending_id = Maybe Int
sending_id_
          }
      parseMessageSendingStateFailed :: A.Value -> AT.Parser MessageSendingState
      parseMessageSendingStateFailed :: Value -> Parser MessageSendingState
parseMessageSendingStateFailed = String
-> (Object -> Parser MessageSendingState)
-> Value
-> Parser MessageSendingState
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"MessageSendingStateFailed" ((Object -> Parser MessageSendingState)
 -> Value -> Parser MessageSendingState)
-> (Object -> Parser MessageSendingState)
-> Value
-> Parser MessageSendingState
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Error
_error_                   <- Object
o Object -> Key -> Parser (Maybe Error)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"error"
        Maybe Bool
can_retry_                <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"can_retry"
        Maybe Bool
need_another_sender_      <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"need_another_sender"
        Maybe Bool
need_another_reply_quote_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"need_another_reply_quote"
        Maybe Bool
need_drop_reply_          <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"need_drop_reply"
        Maybe Double
retry_after_              <- Object
o Object -> Key -> Parser (Maybe Double)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"retry_after"
        MessageSendingState -> Parser MessageSendingState
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MessageSendingState -> Parser MessageSendingState)
-> MessageSendingState -> Parser MessageSendingState
forall a b. (a -> b) -> a -> b
$ MessageSendingStateFailed
          { _error :: Maybe Error
_error                   = Maybe Error
_error_
          , can_retry :: Maybe Bool
can_retry                = Maybe Bool
can_retry_
          , need_another_sender :: Maybe Bool
need_another_sender      = Maybe Bool
need_another_sender_
          , need_another_reply_quote :: Maybe Bool
need_another_reply_quote = Maybe Bool
need_another_reply_quote_
          , need_drop_reply :: Maybe Bool
need_drop_reply          = Maybe Bool
need_drop_reply_
          , retry_after :: Maybe Double
retry_after              = Maybe Double
retry_after_
          }
  parseJSON Value
_ = Parser MessageSendingState
forall a. Monoid a => a
mempty