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 qualified TD.Data.TargetChatTypes as TargetChatTypes
import {-# SOURCE #-} qualified TD.Data.InternalLinkType as InternalLinkType

-- | Describes the target chat to be opened
data TargetChat
  = TargetChatCurrent -- ^ The currently opened chat and forum topic must be kept
  | TargetChatChosen -- ^ The chat needs to be chosen by the user among chats of the specified types
    { TargetChat -> Maybe TargetChatTypes
types :: Maybe TargetChatTypes.TargetChatTypes -- ^ Allowed types for the chat
    }
  | 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
    { types :: TargetChat -> Maybe TargetChatTypes
types = Maybe TargetChatTypes
types_
    }
      = String
"TargetChatChosen"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"types" String -> Maybe TargetChatTypes -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe TargetChatTypes
types_
        ]
  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 TargetChatTypes
types_ <- Object
o Object -> Key -> Parser (Maybe TargetChatTypes)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"types"
        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
          { types :: Maybe TargetChatTypes
types = Maybe TargetChatTypes
types_
          }
      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
    { types :: TargetChat -> Maybe TargetChatTypes
types = Maybe TargetChatTypes
types_
    }
      = [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
"types" Key -> Maybe TargetChatTypes -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe TargetChatTypes
types_
        ]
  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_
        ]