module TD.Data.TargetChat
  (TargetChat(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import {-# SOURCE #-} qualified TD.Data.InternalLinkType as InternalLinkType

-- | Describes the target chat to be opened
data TargetChat
  = TargetChatCurrent -- ^ The currently opened chat needs to be kept
  | TargetChatChosen -- ^ The chat needs to be chosen by the user among chats of the specified types
    { TargetChat -> Maybe Bool
allow_user_chats    :: Maybe Bool -- ^ True, if private chats with ordinary users are allowed
    , TargetChat -> Maybe Bool
allow_bot_chats     :: Maybe Bool -- ^ True, if private chats with other bots are allowed
    , TargetChat -> Maybe Bool
allow_group_chats   :: Maybe Bool -- ^ True, if basic group and supergroup chats are allowed
    , TargetChat -> Maybe Bool
allow_channel_chats :: Maybe Bool -- ^ True, if channel chats are allowed
    }
  | TargetChatInternalLink -- ^ The chat needs to be open with the provided internal link
    { TargetChat -> Maybe InternalLinkType
link :: Maybe InternalLinkType.InternalLinkType -- ^ An internal link pointing to the chat
    }
  deriving (TargetChat -> TargetChat -> Bool
(TargetChat -> TargetChat -> Bool)
-> (TargetChat -> TargetChat -> Bool) -> Eq TargetChat
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TargetChat -> TargetChat -> Bool
== :: TargetChat -> TargetChat -> Bool
$c/= :: TargetChat -> TargetChat -> Bool
/= :: TargetChat -> TargetChat -> Bool
Eq, Int -> TargetChat -> ShowS
[TargetChat] -> ShowS
TargetChat -> String
(Int -> TargetChat -> ShowS)
-> (TargetChat -> String)
-> ([TargetChat] -> ShowS)
-> Show TargetChat
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TargetChat -> ShowS
showsPrec :: Int -> TargetChat -> ShowS
$cshow :: TargetChat -> String
show :: TargetChat -> String
$cshowList :: [TargetChat] -> ShowS
showList :: [TargetChat] -> ShowS
Show)

instance I.ShortShow TargetChat where
  shortShow :: TargetChat -> String
shortShow TargetChat
TargetChatCurrent
      = String
"TargetChatCurrent"
  shortShow TargetChatChosen
    { allow_user_chats :: TargetChat -> Maybe Bool
allow_user_chats    = Maybe Bool
allow_user_chats_
    , allow_bot_chats :: TargetChat -> Maybe Bool
allow_bot_chats     = Maybe Bool
allow_bot_chats_
    , allow_group_chats :: TargetChat -> Maybe Bool
allow_group_chats   = Maybe Bool
allow_group_chats_
    , allow_channel_chats :: TargetChat -> Maybe Bool
allow_channel_chats = Maybe Bool
allow_channel_chats_
    }
      = String
"TargetChatChosen"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"allow_user_chats"    String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
allow_user_chats_
        , String
"allow_bot_chats"     String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
allow_bot_chats_
        , String
"allow_group_chats"   String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
allow_group_chats_
        , String
"allow_channel_chats" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
allow_channel_chats_
        ]
  shortShow TargetChatInternalLink
    { link :: TargetChat -> Maybe InternalLinkType
link = Maybe InternalLinkType
link_
    }
      = String
"TargetChatInternalLink"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"link" String -> Maybe InternalLinkType -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe InternalLinkType
link_
        ]

instance AT.FromJSON TargetChat where
  parseJSON :: Value -> Parser TargetChat
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
"targetChatCurrent"      -> TargetChat -> Parser TargetChat
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TargetChat
TargetChatCurrent
      String
"targetChatChosen"       -> Value -> Parser TargetChat
parseTargetChatChosen Value
v
      String
"targetChatInternalLink" -> Value -> Parser TargetChat
parseTargetChatInternalLink Value
v
      String
_                        -> Parser TargetChat
forall a. Monoid a => a
mempty
    
    where
      parseTargetChatChosen :: A.Value -> AT.Parser TargetChat
      parseTargetChatChosen :: Value -> Parser TargetChat
parseTargetChatChosen = String
-> (Object -> Parser TargetChat) -> Value -> Parser TargetChat
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TargetChatChosen" ((Object -> Parser TargetChat) -> Value -> Parser TargetChat)
-> (Object -> Parser TargetChat) -> Value -> Parser TargetChat
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Bool
allow_user_chats_    <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"allow_user_chats"
        Maybe Bool
allow_bot_chats_     <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"allow_bot_chats"
        Maybe Bool
allow_group_chats_   <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"allow_group_chats"
        Maybe Bool
allow_channel_chats_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"allow_channel_chats"
        TargetChat -> Parser TargetChat
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TargetChat -> Parser TargetChat)
-> TargetChat -> Parser TargetChat
forall a b. (a -> b) -> a -> b
$ TargetChatChosen
          { allow_user_chats :: Maybe Bool
allow_user_chats    = Maybe Bool
allow_user_chats_
          , allow_bot_chats :: Maybe Bool
allow_bot_chats     = Maybe Bool
allow_bot_chats_
          , allow_group_chats :: Maybe Bool
allow_group_chats   = Maybe Bool
allow_group_chats_
          , allow_channel_chats :: Maybe Bool
allow_channel_chats = Maybe Bool
allow_channel_chats_
          }
      parseTargetChatInternalLink :: A.Value -> AT.Parser TargetChat
      parseTargetChatInternalLink :: Value -> Parser TargetChat
parseTargetChatInternalLink = String
-> (Object -> Parser TargetChat) -> Value -> Parser TargetChat
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TargetChatInternalLink" ((Object -> Parser TargetChat) -> Value -> Parser TargetChat)
-> (Object -> Parser TargetChat) -> Value -> Parser TargetChat
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe InternalLinkType
link_ <- Object
o Object -> Key -> Parser (Maybe InternalLinkType)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"link"
        TargetChat -> Parser TargetChat
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TargetChat -> Parser TargetChat)
-> TargetChat -> Parser TargetChat
forall a b. (a -> b) -> a -> b
$ TargetChatInternalLink
          { link :: Maybe InternalLinkType
link = Maybe InternalLinkType
link_
          }
  parseJSON Value
_ = Parser TargetChat
forall a. Monoid a => a
mempty

instance AT.ToJSON TargetChat where
  toJSON :: TargetChat -> Value
toJSON TargetChat
TargetChatCurrent
      = [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
"targetChatCurrent"
        ]
  toJSON TargetChatChosen
    { allow_user_chats :: TargetChat -> Maybe Bool
allow_user_chats    = Maybe Bool
allow_user_chats_
    , allow_bot_chats :: TargetChat -> Maybe Bool
allow_bot_chats     = Maybe Bool
allow_bot_chats_
    , allow_group_chats :: TargetChat -> Maybe Bool
allow_group_chats   = Maybe Bool
allow_group_chats_
    , allow_channel_chats :: TargetChat -> Maybe Bool
allow_channel_chats = Maybe Bool
allow_channel_chats_
    }
      = [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
"targetChatChosen"
        , Key
"allow_user_chats"    Key -> Maybe Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Bool
allow_user_chats_
        , Key
"allow_bot_chats"     Key -> Maybe Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Bool
allow_bot_chats_
        , Key
"allow_group_chats"   Key -> Maybe Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Bool
allow_group_chats_
        , Key
"allow_channel_chats" Key -> Maybe Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Bool
allow_channel_chats_
        ]
  toJSON TargetChatInternalLink
    { link :: TargetChat -> Maybe InternalLinkType
link = Maybe InternalLinkType
link_
    }
      = [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
"targetChatInternalLink"
        , Key
"link"  Key -> Maybe InternalLinkType -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe InternalLinkType
link_
        ]