module TD.Data.KeyboardButtonSource
  (KeyboardButtonSource(..)) 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 source of a keyboard button
data KeyboardButtonSource
  = KeyboardButtonSourceMessage -- ^ The button is from a bot's message
    { KeyboardButtonSource -> Maybe Int
chat_id    :: Maybe Int -- ^ Identifier of the chat with the message
    , KeyboardButtonSource -> Maybe Int
message_id :: Maybe Int -- ^ Identifier of the message with the button
    }
  | KeyboardButtonSourceWebApp -- ^ The button is a prepared keyboard button from a Mini App received via getPreparedKeyboardButton
    { KeyboardButtonSource -> Maybe Int
bot_user_id        :: Maybe Int    -- ^ Identifier of the bot that created the button
    , KeyboardButtonSource -> Maybe Text
prepared_button_id :: Maybe T.Text -- ^ Identifier of the prepared button
    }
  deriving (KeyboardButtonSource -> KeyboardButtonSource -> Bool
(KeyboardButtonSource -> KeyboardButtonSource -> Bool)
-> (KeyboardButtonSource -> KeyboardButtonSource -> Bool)
-> Eq KeyboardButtonSource
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: KeyboardButtonSource -> KeyboardButtonSource -> Bool
== :: KeyboardButtonSource -> KeyboardButtonSource -> Bool
$c/= :: KeyboardButtonSource -> KeyboardButtonSource -> Bool
/= :: KeyboardButtonSource -> KeyboardButtonSource -> Bool
Eq, Int -> KeyboardButtonSource -> ShowS
[KeyboardButtonSource] -> ShowS
KeyboardButtonSource -> String
(Int -> KeyboardButtonSource -> ShowS)
-> (KeyboardButtonSource -> String)
-> ([KeyboardButtonSource] -> ShowS)
-> Show KeyboardButtonSource
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> KeyboardButtonSource -> ShowS
showsPrec :: Int -> KeyboardButtonSource -> ShowS
$cshow :: KeyboardButtonSource -> String
show :: KeyboardButtonSource -> String
$cshowList :: [KeyboardButtonSource] -> ShowS
showList :: [KeyboardButtonSource] -> ShowS
Show)

instance I.ShortShow KeyboardButtonSource where
  shortShow :: KeyboardButtonSource -> String
shortShow KeyboardButtonSourceMessage
    { chat_id :: KeyboardButtonSource -> Maybe Int
chat_id    = Maybe Int
chat_id_
    , message_id :: KeyboardButtonSource -> Maybe Int
message_id = Maybe Int
message_id_
    }
      = String
"KeyboardButtonSourceMessage"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"chat_id"    String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
chat_id_
        , String
"message_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
message_id_
        ]
  shortShow KeyboardButtonSourceWebApp
    { bot_user_id :: KeyboardButtonSource -> Maybe Int
bot_user_id        = Maybe Int
bot_user_id_
    , prepared_button_id :: KeyboardButtonSource -> Maybe Text
prepared_button_id = Maybe Text
prepared_button_id_
    }
      = String
"KeyboardButtonSourceWebApp"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"bot_user_id"        String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
bot_user_id_
        , String
"prepared_button_id" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
prepared_button_id_
        ]

instance AT.FromJSON KeyboardButtonSource where
  parseJSON :: Value -> Parser KeyboardButtonSource
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
"keyboardButtonSourceMessage" -> Value -> Parser KeyboardButtonSource
parseKeyboardButtonSourceMessage Value
v
      String
"keyboardButtonSourceWebApp"  -> Value -> Parser KeyboardButtonSource
parseKeyboardButtonSourceWebApp Value
v
      String
_                             -> Parser KeyboardButtonSource
forall a. Monoid a => a
mempty
    
    where
      parseKeyboardButtonSourceMessage :: A.Value -> AT.Parser KeyboardButtonSource
      parseKeyboardButtonSourceMessage :: Value -> Parser KeyboardButtonSource
parseKeyboardButtonSourceMessage = String
-> (Object -> Parser KeyboardButtonSource)
-> Value
-> Parser KeyboardButtonSource
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"KeyboardButtonSourceMessage" ((Object -> Parser KeyboardButtonSource)
 -> Value -> Parser KeyboardButtonSource)
-> (Object -> Parser KeyboardButtonSource)
-> Value
-> Parser KeyboardButtonSource
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
chat_id_    <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"chat_id"
        Maybe Int
message_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"message_id"
        KeyboardButtonSource -> Parser KeyboardButtonSource
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (KeyboardButtonSource -> Parser KeyboardButtonSource)
-> KeyboardButtonSource -> Parser KeyboardButtonSource
forall a b. (a -> b) -> a -> b
$ KeyboardButtonSourceMessage
          { chat_id :: Maybe Int
chat_id    = Maybe Int
chat_id_
          , message_id :: Maybe Int
message_id = Maybe Int
message_id_
          }
      parseKeyboardButtonSourceWebApp :: A.Value -> AT.Parser KeyboardButtonSource
      parseKeyboardButtonSourceWebApp :: Value -> Parser KeyboardButtonSource
parseKeyboardButtonSourceWebApp = String
-> (Object -> Parser KeyboardButtonSource)
-> Value
-> Parser KeyboardButtonSource
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"KeyboardButtonSourceWebApp" ((Object -> Parser KeyboardButtonSource)
 -> Value -> Parser KeyboardButtonSource)
-> (Object -> Parser KeyboardButtonSource)
-> Value
-> Parser KeyboardButtonSource
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
bot_user_id_        <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"bot_user_id"
        Maybe Text
prepared_button_id_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"prepared_button_id"
        KeyboardButtonSource -> Parser KeyboardButtonSource
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (KeyboardButtonSource -> Parser KeyboardButtonSource)
-> KeyboardButtonSource -> Parser KeyboardButtonSource
forall a b. (a -> b) -> a -> b
$ KeyboardButtonSourceWebApp
          { bot_user_id :: Maybe Int
bot_user_id        = Maybe Int
bot_user_id_
          , prepared_button_id :: Maybe Text
prepared_button_id = Maybe Text
prepared_button_id_
          }
  parseJSON Value
_ = Parser KeyboardButtonSource
forall a. Monoid a => a
mempty

instance AT.ToJSON KeyboardButtonSource where
  toJSON :: KeyboardButtonSource -> Value
toJSON KeyboardButtonSourceMessage
    { chat_id :: KeyboardButtonSource -> Maybe Int
chat_id    = Maybe Int
chat_id_
    , message_id :: KeyboardButtonSource -> Maybe Int
message_id = Maybe Int
message_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
"keyboardButtonSourceMessage"
        , Key
"chat_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
chat_id_
        , Key
"message_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
message_id_
        ]
  toJSON KeyboardButtonSourceWebApp
    { bot_user_id :: KeyboardButtonSource -> Maybe Int
bot_user_id        = Maybe Int
bot_user_id_
    , prepared_button_id :: KeyboardButtonSource -> Maybe Text
prepared_button_id = Maybe Text
prepared_button_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
"keyboardButtonSourceWebApp"
        , Key
"bot_user_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
bot_user_id_
        , Key
"prepared_button_id" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
prepared_button_id_
        ]