module TD.Data.MessageTopic
  (MessageTopic(..)) where

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

-- | Describes a topic of messages in a chat
data MessageTopic
  = MessageTopicForum -- ^ A topic in a forum supergroup chat
    { MessageTopic -> Maybe Int
forum_topic_id :: Maybe Int -- ^ Unique identifier of the forum topic; all messages in a non-forum supergroup chats belongs to the General topic
    }
  | MessageTopicDirectMessages -- ^ A topic in a channel direct messages chat administered by the current user
    { MessageTopic -> Maybe Int
direct_messages_chat_topic_id :: Maybe Int -- ^ Unique identifier of the topic
    }
  | MessageTopicSavedMessages -- ^ A topic in Saved Messages chat
    { MessageTopic -> Maybe Int
saved_messages_topic_id :: Maybe Int -- ^ Unique identifier of the Saved Messages topic
    }
  deriving (MessageTopic -> MessageTopic -> Bool
(MessageTopic -> MessageTopic -> Bool)
-> (MessageTopic -> MessageTopic -> Bool) -> Eq MessageTopic
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MessageTopic -> MessageTopic -> Bool
== :: MessageTopic -> MessageTopic -> Bool
$c/= :: MessageTopic -> MessageTopic -> Bool
/= :: MessageTopic -> MessageTopic -> Bool
Eq, Int -> MessageTopic -> ShowS
[MessageTopic] -> ShowS
MessageTopic -> String
(Int -> MessageTopic -> ShowS)
-> (MessageTopic -> String)
-> ([MessageTopic] -> ShowS)
-> Show MessageTopic
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MessageTopic -> ShowS
showsPrec :: Int -> MessageTopic -> ShowS
$cshow :: MessageTopic -> String
show :: MessageTopic -> String
$cshowList :: [MessageTopic] -> ShowS
showList :: [MessageTopic] -> ShowS
Show)

instance I.ShortShow MessageTopic where
  shortShow :: MessageTopic -> String
shortShow MessageTopicForum
    { forum_topic_id :: MessageTopic -> Maybe Int
forum_topic_id = Maybe Int
forum_topic_id_
    }
      = String
"MessageTopicForum"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"forum_topic_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
forum_topic_id_
        ]
  shortShow MessageTopicDirectMessages
    { direct_messages_chat_topic_id :: MessageTopic -> Maybe Int
direct_messages_chat_topic_id = Maybe Int
direct_messages_chat_topic_id_
    }
      = String
"MessageTopicDirectMessages"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"direct_messages_chat_topic_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
direct_messages_chat_topic_id_
        ]
  shortShow MessageTopicSavedMessages
    { saved_messages_topic_id :: MessageTopic -> Maybe Int
saved_messages_topic_id = Maybe Int
saved_messages_topic_id_
    }
      = String
"MessageTopicSavedMessages"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"saved_messages_topic_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
saved_messages_topic_id_
        ]

instance AT.FromJSON MessageTopic where
  parseJSON :: Value -> Parser MessageTopic
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
"messageTopicForum"          -> Value -> Parser MessageTopic
parseMessageTopicForum Value
v
      String
"messageTopicDirectMessages" -> Value -> Parser MessageTopic
parseMessageTopicDirectMessages Value
v
      String
"messageTopicSavedMessages"  -> Value -> Parser MessageTopic
parseMessageTopicSavedMessages Value
v
      String
_                            -> Parser MessageTopic
forall a. Monoid a => a
mempty
    
    where
      parseMessageTopicForum :: A.Value -> AT.Parser MessageTopic
      parseMessageTopicForum :: Value -> Parser MessageTopic
parseMessageTopicForum = String
-> (Object -> Parser MessageTopic) -> Value -> Parser MessageTopic
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"MessageTopicForum" ((Object -> Parser MessageTopic) -> Value -> Parser MessageTopic)
-> (Object -> Parser MessageTopic) -> Value -> Parser MessageTopic
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
forum_topic_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"forum_topic_id"
        MessageTopic -> Parser MessageTopic
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MessageTopic -> Parser MessageTopic)
-> MessageTopic -> Parser MessageTopic
forall a b. (a -> b) -> a -> b
$ MessageTopicForum
          { forum_topic_id :: Maybe Int
forum_topic_id = Maybe Int
forum_topic_id_
          }
      parseMessageTopicDirectMessages :: A.Value -> AT.Parser MessageTopic
      parseMessageTopicDirectMessages :: Value -> Parser MessageTopic
parseMessageTopicDirectMessages = String
-> (Object -> Parser MessageTopic) -> Value -> Parser MessageTopic
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"MessageTopicDirectMessages" ((Object -> Parser MessageTopic) -> Value -> Parser MessageTopic)
-> (Object -> Parser MessageTopic) -> Value -> Parser MessageTopic
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
direct_messages_chat_topic_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"direct_messages_chat_topic_id"
        MessageTopic -> Parser MessageTopic
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MessageTopic -> Parser MessageTopic)
-> MessageTopic -> Parser MessageTopic
forall a b. (a -> b) -> a -> b
$ MessageTopicDirectMessages
          { direct_messages_chat_topic_id :: Maybe Int
direct_messages_chat_topic_id = Maybe Int
direct_messages_chat_topic_id_
          }
      parseMessageTopicSavedMessages :: A.Value -> AT.Parser MessageTopic
      parseMessageTopicSavedMessages :: Value -> Parser MessageTopic
parseMessageTopicSavedMessages = String
-> (Object -> Parser MessageTopic) -> Value -> Parser MessageTopic
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"MessageTopicSavedMessages" ((Object -> Parser MessageTopic) -> Value -> Parser MessageTopic)
-> (Object -> Parser MessageTopic) -> Value -> Parser MessageTopic
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
saved_messages_topic_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"saved_messages_topic_id"
        MessageTopic -> Parser MessageTopic
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MessageTopic -> Parser MessageTopic)
-> MessageTopic -> Parser MessageTopic
forall a b. (a -> b) -> a -> b
$ MessageTopicSavedMessages
          { saved_messages_topic_id :: Maybe Int
saved_messages_topic_id = Maybe Int
saved_messages_topic_id_
          }
  parseJSON Value
_ = Parser MessageTopic
forall a. Monoid a => a
mempty

instance AT.ToJSON MessageTopic where
  toJSON :: MessageTopic -> Value
toJSON MessageTopicForum
    { forum_topic_id :: MessageTopic -> Maybe Int
forum_topic_id = Maybe Int
forum_topic_id_
    }
      = [Pair] -> Value
A.object
        [ Key
"@type"          Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"messageTopicForum"
        , Key
"forum_topic_id" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
forum_topic_id_
        ]
  toJSON MessageTopicDirectMessages
    { direct_messages_chat_topic_id :: MessageTopic -> Maybe Int
direct_messages_chat_topic_id = Maybe Int
direct_messages_chat_topic_id_
    }
      = [Pair] -> Value
A.object
        [ Key
"@type"                         Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"messageTopicDirectMessages"
        , Key
"direct_messages_chat_topic_id" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
direct_messages_chat_topic_id_
        ]
  toJSON MessageTopicSavedMessages
    { saved_messages_topic_id :: MessageTopic -> Maybe Int
saved_messages_topic_id = Maybe Int
saved_messages_topic_id_
    }
      = [Pair] -> Value
A.object
        [ Key
"@type"                   Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"messageTopicSavedMessages"
        , Key
"saved_messages_topic_id" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
saved_messages_topic_id_
        ]