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

-- | Contains detailed information about a notification
data NotificationType
  = NotificationTypeNewMessage -- ^ New message was received
    { NotificationType -> Maybe Message
message      :: Maybe Message.Message -- ^ The message
    , NotificationType -> Maybe Bool
show_preview :: Maybe Bool            -- ^ True, if message content must be displayed in notifications
    }
  | NotificationTypeNewSecretChat -- ^ New secret chat was created
  | NotificationTypeNewCall -- ^ New call was received
    { NotificationType -> Maybe Int
call_id :: Maybe Int -- ^ Call identifier
    }
  | NotificationTypeNewPushMessage -- ^ New message was received through a push notification
    { NotificationType -> Maybe Int
message_id  :: Maybe Int                                   -- ^ The message identifier. The message will not be available in the chat history, but the identifier can be used in viewMessages, or as a message to be replied in the same chat
    , NotificationType -> Maybe MessageSender
sender_id   :: Maybe MessageSender.MessageSender           -- ^ Identifier of the sender of the message. Corresponding user or chat may be inaccessible
    , NotificationType -> Maybe Text
sender_name :: Maybe T.Text                                -- ^ Name of the sender
    , NotificationType -> Maybe Bool
is_outgoing :: Maybe Bool                                  -- ^ True, if the message is outgoing
    , NotificationType -> Maybe PushMessageContent
content     :: Maybe PushMessageContent.PushMessageContent -- ^ Push message content
    }
  deriving (NotificationType -> NotificationType -> Bool
(NotificationType -> NotificationType -> Bool)
-> (NotificationType -> NotificationType -> Bool)
-> Eq NotificationType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: NotificationType -> NotificationType -> Bool
== :: NotificationType -> NotificationType -> Bool
$c/= :: NotificationType -> NotificationType -> Bool
/= :: NotificationType -> NotificationType -> Bool
Eq, Int -> NotificationType -> ShowS
[NotificationType] -> ShowS
NotificationType -> String
(Int -> NotificationType -> ShowS)
-> (NotificationType -> String)
-> ([NotificationType] -> ShowS)
-> Show NotificationType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NotificationType -> ShowS
showsPrec :: Int -> NotificationType -> ShowS
$cshow :: NotificationType -> String
show :: NotificationType -> String
$cshowList :: [NotificationType] -> ShowS
showList :: [NotificationType] -> ShowS
Show)

instance I.ShortShow NotificationType where
  shortShow :: NotificationType -> String
shortShow NotificationTypeNewMessage
    { message :: NotificationType -> Maybe Message
message      = Maybe Message
message_
    , show_preview :: NotificationType -> Maybe Bool
show_preview = Maybe Bool
show_preview_
    }
      = String
"NotificationTypeNewMessage"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"message"      String -> Maybe Message -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Message
message_
        , String
"show_preview" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
show_preview_
        ]
  shortShow NotificationType
NotificationTypeNewSecretChat
      = String
"NotificationTypeNewSecretChat"
  shortShow NotificationTypeNewCall
    { call_id :: NotificationType -> Maybe Int
call_id = Maybe Int
call_id_
    }
      = String
"NotificationTypeNewCall"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"call_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
call_id_
        ]
  shortShow NotificationTypeNewPushMessage
    { message_id :: NotificationType -> Maybe Int
message_id  = Maybe Int
message_id_
    , sender_id :: NotificationType -> Maybe MessageSender
sender_id   = Maybe MessageSender
sender_id_
    , sender_name :: NotificationType -> Maybe Text
sender_name = Maybe Text
sender_name_
    , is_outgoing :: NotificationType -> Maybe Bool
is_outgoing = Maybe Bool
is_outgoing_
    , content :: NotificationType -> Maybe PushMessageContent
content     = Maybe PushMessageContent
content_
    }
      = String
"NotificationTypeNewPushMessage"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ 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
"is_outgoing" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_outgoing_
        , String
"content"     String -> Maybe PushMessageContent -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe PushMessageContent
content_
        ]

instance AT.FromJSON NotificationType where
  parseJSON :: Value -> Parser NotificationType
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
"notificationTypeNewMessage"     -> Value -> Parser NotificationType
parseNotificationTypeNewMessage Value
v
      String
"notificationTypeNewSecretChat"  -> NotificationType -> Parser NotificationType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure NotificationType
NotificationTypeNewSecretChat
      String
"notificationTypeNewCall"        -> Value -> Parser NotificationType
parseNotificationTypeNewCall Value
v
      String
"notificationTypeNewPushMessage" -> Value -> Parser NotificationType
parseNotificationTypeNewPushMessage Value
v
      String
_                                -> Parser NotificationType
forall a. Monoid a => a
mempty
    
    where
      parseNotificationTypeNewMessage :: A.Value -> AT.Parser NotificationType
      parseNotificationTypeNewMessage :: Value -> Parser NotificationType
parseNotificationTypeNewMessage = String
-> (Object -> Parser NotificationType)
-> Value
-> Parser NotificationType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"NotificationTypeNewMessage" ((Object -> Parser NotificationType)
 -> Value -> Parser NotificationType)
-> (Object -> Parser NotificationType)
-> Value
-> Parser NotificationType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Message
message_      <- Object
o Object -> Key -> Parser (Maybe Message)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"message"
        Maybe Bool
show_preview_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"show_preview"
        NotificationType -> Parser NotificationType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NotificationType -> Parser NotificationType)
-> NotificationType -> Parser NotificationType
forall a b. (a -> b) -> a -> b
$ NotificationTypeNewMessage
          { message :: Maybe Message
message      = Maybe Message
message_
          , show_preview :: Maybe Bool
show_preview = Maybe Bool
show_preview_
          }
      parseNotificationTypeNewCall :: A.Value -> AT.Parser NotificationType
      parseNotificationTypeNewCall :: Value -> Parser NotificationType
parseNotificationTypeNewCall = String
-> (Object -> Parser NotificationType)
-> Value
-> Parser NotificationType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"NotificationTypeNewCall" ((Object -> Parser NotificationType)
 -> Value -> Parser NotificationType)
-> (Object -> Parser NotificationType)
-> Value
-> Parser NotificationType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
call_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"call_id"
        NotificationType -> Parser NotificationType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NotificationType -> Parser NotificationType)
-> NotificationType -> Parser NotificationType
forall a b. (a -> b) -> a -> b
$ NotificationTypeNewCall
          { call_id :: Maybe Int
call_id = Maybe Int
call_id_
          }
      parseNotificationTypeNewPushMessage :: A.Value -> AT.Parser NotificationType
      parseNotificationTypeNewPushMessage :: Value -> Parser NotificationType
parseNotificationTypeNewPushMessage = String
-> (Object -> Parser NotificationType)
-> Value
-> Parser NotificationType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"NotificationTypeNewPushMessage" ((Object -> Parser NotificationType)
 -> Value -> Parser NotificationType)
-> (Object -> Parser NotificationType)
-> Value
-> Parser NotificationType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        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 Bool
is_outgoing_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_outgoing"
        Maybe PushMessageContent
content_     <- Object
o Object -> Key -> Parser (Maybe PushMessageContent)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"content"
        NotificationType -> Parser NotificationType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NotificationType -> Parser NotificationType)
-> NotificationType -> Parser NotificationType
forall a b. (a -> b) -> a -> b
$ NotificationTypeNewPushMessage
          { 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_
          , is_outgoing :: Maybe Bool
is_outgoing = Maybe Bool
is_outgoing_
          , content :: Maybe PushMessageContent
content     = Maybe PushMessageContent
content_
          }
  parseJSON Value
_ = Parser NotificationType
forall a. Monoid a => a
mempty