module TD.Data.ChatSource
  (ChatSource(..)) where

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

-- | Describes a reason why an external chat is shown in a chat list
data ChatSource
  = ChatSourceMtprotoProxy -- ^ The chat is sponsored by the user's MTProxy server
  | ChatSourcePublicServiceAnnouncement -- ^ The chat contains a public service announcement
    { ChatSource -> Maybe Text
_type :: Maybe T.Text -- ^ The type of the announcement
    , ChatSource -> Maybe Text
text  :: Maybe T.Text -- ^ The text of the announcement
    }
  deriving (ChatSource -> ChatSource -> Bool
(ChatSource -> ChatSource -> Bool)
-> (ChatSource -> ChatSource -> Bool) -> Eq ChatSource
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ChatSource -> ChatSource -> Bool
== :: ChatSource -> ChatSource -> Bool
$c/= :: ChatSource -> ChatSource -> Bool
/= :: ChatSource -> ChatSource -> Bool
Eq, Int -> ChatSource -> ShowS
[ChatSource] -> ShowS
ChatSource -> String
(Int -> ChatSource -> ShowS)
-> (ChatSource -> String)
-> ([ChatSource] -> ShowS)
-> Show ChatSource
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ChatSource -> ShowS
showsPrec :: Int -> ChatSource -> ShowS
$cshow :: ChatSource -> String
show :: ChatSource -> String
$cshowList :: [ChatSource] -> ShowS
showList :: [ChatSource] -> ShowS
Show)

instance I.ShortShow ChatSource where
  shortShow :: ChatSource -> String
shortShow ChatSource
ChatSourceMtprotoProxy
      = String
"ChatSourceMtprotoProxy"
  shortShow ChatSourcePublicServiceAnnouncement
    { _type :: ChatSource -> Maybe Text
_type = Maybe Text
_type_
    , text :: ChatSource -> Maybe Text
text  = Maybe Text
text_
    }
      = String
"ChatSourcePublicServiceAnnouncement"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_type" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
_type_
        , String
"text"  String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
text_
        ]

instance AT.FromJSON ChatSource where
  parseJSON :: Value -> Parser ChatSource
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
"chatSourceMtprotoProxy"              -> ChatSource -> Parser ChatSource
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ChatSource
ChatSourceMtprotoProxy
      String
"chatSourcePublicServiceAnnouncement" -> Value -> Parser ChatSource
parseChatSourcePublicServiceAnnouncement Value
v
      String
_                                     -> Parser ChatSource
forall a. Monoid a => a
mempty
    
    where
      parseChatSourcePublicServiceAnnouncement :: A.Value -> AT.Parser ChatSource
      parseChatSourcePublicServiceAnnouncement :: Value -> Parser ChatSource
parseChatSourcePublicServiceAnnouncement = String
-> (Object -> Parser ChatSource) -> Value -> Parser ChatSource
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ChatSourcePublicServiceAnnouncement" ((Object -> Parser ChatSource) -> Value -> Parser ChatSource)
-> (Object -> Parser ChatSource) -> Value -> Parser ChatSource
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
_type_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"type"
        Maybe Text
text_  <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"text"
        ChatSource -> Parser ChatSource
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ChatSource -> Parser ChatSource)
-> ChatSource -> Parser ChatSource
forall a b. (a -> b) -> a -> b
$ ChatSourcePublicServiceAnnouncement
          { _type :: Maybe Text
_type = Maybe Text
_type_
          , text :: Maybe Text
text  = Maybe Text
text_
          }
  parseJSON Value
_ = Parser ChatSource
forall a. Monoid a => a
mempty