module TD.Data.WidgetQueryType
  (WidgetQueryType(..)) 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.WidgetQueryWallet as WidgetQueryWallet
import qualified Data.Text as T

-- | Describes type of a widget query
data WidgetQueryType
  = WidgetQueryTypeData -- ^ The query contains data for the bot
    { WidgetQueryType -> Maybe Bool
with_wallet :: Maybe Bool -- ^ True, if the TON wallet card of the user who sends the query is sent to the bot along with the query
    }
  | WidgetQueryTypeSignData -- ^ The query contains data signed by a TON wallet of the user who sends the query
    { WidgetQueryType -> Maybe WidgetQueryWallet
wallet    :: Maybe WidgetQueryWallet.WidgetQueryWallet -- ^ The wallet that signed the data
    , WidgetQueryType -> Maybe Text
sign_data :: Maybe T.Text                              -- ^ JSON-serialized signed data
    }
  | WidgetQueryTypeSentTransaction -- ^ The query notifies the bot about a transaction sent from a TON wallet of the user who sends the query
    { wallet   :: Maybe WidgetQueryWallet.WidgetQueryWallet -- ^ The wallet from which the transaction was sent
    , WidgetQueryType -> Maybe Text
msg_hash :: Maybe T.Text                              -- ^ Value of msg_hash of the transaction
    }
  deriving (WidgetQueryType -> WidgetQueryType -> Bool
(WidgetQueryType -> WidgetQueryType -> Bool)
-> (WidgetQueryType -> WidgetQueryType -> Bool)
-> Eq WidgetQueryType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: WidgetQueryType -> WidgetQueryType -> Bool
== :: WidgetQueryType -> WidgetQueryType -> Bool
$c/= :: WidgetQueryType -> WidgetQueryType -> Bool
/= :: WidgetQueryType -> WidgetQueryType -> Bool
Eq, Int -> WidgetQueryType -> ShowS
[WidgetQueryType] -> ShowS
WidgetQueryType -> String
(Int -> WidgetQueryType -> ShowS)
-> (WidgetQueryType -> String)
-> ([WidgetQueryType] -> ShowS)
-> Show WidgetQueryType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> WidgetQueryType -> ShowS
showsPrec :: Int -> WidgetQueryType -> ShowS
$cshow :: WidgetQueryType -> String
show :: WidgetQueryType -> String
$cshowList :: [WidgetQueryType] -> ShowS
showList :: [WidgetQueryType] -> ShowS
Show)

instance I.ShortShow WidgetQueryType where
  shortShow :: WidgetQueryType -> String
shortShow WidgetQueryTypeData
    { with_wallet :: WidgetQueryType -> Maybe Bool
with_wallet = Maybe Bool
with_wallet_
    }
      = String
"WidgetQueryTypeData"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"with_wallet" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
with_wallet_
        ]
  shortShow WidgetQueryTypeSignData
    { wallet :: WidgetQueryType -> Maybe WidgetQueryWallet
wallet    = Maybe WidgetQueryWallet
wallet_
    , sign_data :: WidgetQueryType -> Maybe Text
sign_data = Maybe Text
sign_data_
    }
      = String
"WidgetQueryTypeSignData"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"wallet"    String -> Maybe WidgetQueryWallet -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe WidgetQueryWallet
wallet_
        , String
"sign_data" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
sign_data_
        ]
  shortShow WidgetQueryTypeSentTransaction
    { wallet :: WidgetQueryType -> Maybe WidgetQueryWallet
wallet   = Maybe WidgetQueryWallet
wallet_
    , msg_hash :: WidgetQueryType -> Maybe Text
msg_hash = Maybe Text
msg_hash_
    }
      = String
"WidgetQueryTypeSentTransaction"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"wallet"   String -> Maybe WidgetQueryWallet -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe WidgetQueryWallet
wallet_
        , String
"msg_hash" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
msg_hash_
        ]

instance AT.FromJSON WidgetQueryType where
  parseJSON :: Value -> Parser WidgetQueryType
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
"widgetQueryTypeData"            -> Value -> Parser WidgetQueryType
parseWidgetQueryTypeData Value
v
      String
"widgetQueryTypeSignData"        -> Value -> Parser WidgetQueryType
parseWidgetQueryTypeSignData Value
v
      String
"widgetQueryTypeSentTransaction" -> Value -> Parser WidgetQueryType
parseWidgetQueryTypeSentTransaction Value
v
      String
_                                -> Parser WidgetQueryType
forall a. Monoid a => a
mempty
    
    where
      parseWidgetQueryTypeData :: A.Value -> AT.Parser WidgetQueryType
      parseWidgetQueryTypeData :: Value -> Parser WidgetQueryType
parseWidgetQueryTypeData = String
-> (Object -> Parser WidgetQueryType)
-> Value
-> Parser WidgetQueryType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"WidgetQueryTypeData" ((Object -> Parser WidgetQueryType)
 -> Value -> Parser WidgetQueryType)
-> (Object -> Parser WidgetQueryType)
-> Value
-> Parser WidgetQueryType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Bool
with_wallet_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"with_wallet"
        WidgetQueryType -> Parser WidgetQueryType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (WidgetQueryType -> Parser WidgetQueryType)
-> WidgetQueryType -> Parser WidgetQueryType
forall a b. (a -> b) -> a -> b
$ WidgetQueryTypeData
          { with_wallet :: Maybe Bool
with_wallet = Maybe Bool
with_wallet_
          }
      parseWidgetQueryTypeSignData :: A.Value -> AT.Parser WidgetQueryType
      parseWidgetQueryTypeSignData :: Value -> Parser WidgetQueryType
parseWidgetQueryTypeSignData = String
-> (Object -> Parser WidgetQueryType)
-> Value
-> Parser WidgetQueryType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"WidgetQueryTypeSignData" ((Object -> Parser WidgetQueryType)
 -> Value -> Parser WidgetQueryType)
-> (Object -> Parser WidgetQueryType)
-> Value
-> Parser WidgetQueryType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe WidgetQueryWallet
wallet_    <- Object
o Object -> Key -> Parser (Maybe WidgetQueryWallet)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"wallet"
        Maybe Text
sign_data_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"sign_data"
        WidgetQueryType -> Parser WidgetQueryType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (WidgetQueryType -> Parser WidgetQueryType)
-> WidgetQueryType -> Parser WidgetQueryType
forall a b. (a -> b) -> a -> b
$ WidgetQueryTypeSignData
          { wallet :: Maybe WidgetQueryWallet
wallet    = Maybe WidgetQueryWallet
wallet_
          , sign_data :: Maybe Text
sign_data = Maybe Text
sign_data_
          }
      parseWidgetQueryTypeSentTransaction :: A.Value -> AT.Parser WidgetQueryType
      parseWidgetQueryTypeSentTransaction :: Value -> Parser WidgetQueryType
parseWidgetQueryTypeSentTransaction = String
-> (Object -> Parser WidgetQueryType)
-> Value
-> Parser WidgetQueryType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"WidgetQueryTypeSentTransaction" ((Object -> Parser WidgetQueryType)
 -> Value -> Parser WidgetQueryType)
-> (Object -> Parser WidgetQueryType)
-> Value
-> Parser WidgetQueryType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe WidgetQueryWallet
wallet_   <- Object
o Object -> Key -> Parser (Maybe WidgetQueryWallet)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"wallet"
        Maybe Text
msg_hash_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"msg_hash"
        WidgetQueryType -> Parser WidgetQueryType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (WidgetQueryType -> Parser WidgetQueryType)
-> WidgetQueryType -> Parser WidgetQueryType
forall a b. (a -> b) -> a -> b
$ WidgetQueryTypeSentTransaction
          { wallet :: Maybe WidgetQueryWallet
wallet   = Maybe WidgetQueryWallet
wallet_
          , msg_hash :: Maybe Text
msg_hash = Maybe Text
msg_hash_
          }
  parseJSON Value
_ = Parser WidgetQueryType
forall a. Monoid a => a
mempty

instance AT.ToJSON WidgetQueryType where
  toJSON :: WidgetQueryType -> Value
toJSON WidgetQueryTypeData
    { with_wallet :: WidgetQueryType -> Maybe Bool
with_wallet = Maybe Bool
with_wallet_
    }
      = [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
"widgetQueryTypeData"
        , Key
"with_wallet" Key -> Maybe Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Bool
with_wallet_
        ]
  toJSON WidgetQueryTypeSignData
    { wallet :: WidgetQueryType -> Maybe WidgetQueryWallet
wallet    = Maybe WidgetQueryWallet
wallet_
    , sign_data :: WidgetQueryType -> Maybe Text
sign_data = Maybe Text
sign_data_
    }
      = [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
"widgetQueryTypeSignData"
        , Key
"wallet"    Key -> Maybe WidgetQueryWallet -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe WidgetQueryWallet
wallet_
        , Key
"sign_data" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
sign_data_
        ]
  toJSON WidgetQueryTypeSentTransaction
    { wallet :: WidgetQueryType -> Maybe WidgetQueryWallet
wallet   = Maybe WidgetQueryWallet
wallet_
    , msg_hash :: WidgetQueryType -> Maybe Text
msg_hash = Maybe Text
msg_hash_
    }
      = [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
"widgetQueryTypeSentTransaction"
        , Key
"wallet"   Key -> Maybe WidgetQueryWallet -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe WidgetQueryWallet
wallet_
        , Key
"msg_hash" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
msg_hash_
        ]