module TD.Data.TonWalletMessage
  (TonWalletMessage(..)) 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
import qualified Data.ByteString as BS
import qualified TD.Data.TonSmartContractInitialState as TonSmartContractInitialState

data TonWalletMessage
  = TonWalletMessage -- ^ Describes an internal message that must be sent from the TON wallet of the current user. The message must be signed and sent without any changes
    { TonWalletMessage -> Maybe Text
destination_address :: Maybe T.Text                                                    -- ^ Address on the TON blockchain to which the message must be sent
    , TonWalletMessage -> Maybe Int
gram_amount         :: Maybe Int                                                       -- ^ The amount that must be attached to the message, in the smallest units of TON Grams; already includes all the required amounts
    , TonWalletMessage -> Maybe Int
send_mode           :: Maybe Int                                                       -- ^ Send mode of the message
    , TonWalletMessage -> Maybe ByteString
body                :: Maybe BS.ByteString                                             -- ^ Body of the message
    , TonWalletMessage -> Maybe TonSmartContractInitialState
initial_state       :: Maybe TonSmartContractInitialState.TonSmartContractInitialState -- ^ Initial state of the smart contract that must be deployed with the message; may be null if none
    }
  deriving (TonWalletMessage -> TonWalletMessage -> Bool
(TonWalletMessage -> TonWalletMessage -> Bool)
-> (TonWalletMessage -> TonWalletMessage -> Bool)
-> Eq TonWalletMessage
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TonWalletMessage -> TonWalletMessage -> Bool
== :: TonWalletMessage -> TonWalletMessage -> Bool
$c/= :: TonWalletMessage -> TonWalletMessage -> Bool
/= :: TonWalletMessage -> TonWalletMessage -> Bool
Eq, Int -> TonWalletMessage -> ShowS
[TonWalletMessage] -> ShowS
TonWalletMessage -> String
(Int -> TonWalletMessage -> ShowS)
-> (TonWalletMessage -> String)
-> ([TonWalletMessage] -> ShowS)
-> Show TonWalletMessage
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TonWalletMessage -> ShowS
showsPrec :: Int -> TonWalletMessage -> ShowS
$cshow :: TonWalletMessage -> String
show :: TonWalletMessage -> String
$cshowList :: [TonWalletMessage] -> ShowS
showList :: [TonWalletMessage] -> ShowS
Show)

instance I.ShortShow TonWalletMessage where
  shortShow :: TonWalletMessage -> String
shortShow TonWalletMessage
    { destination_address :: TonWalletMessage -> Maybe Text
destination_address = Maybe Text
destination_address_
    , gram_amount :: TonWalletMessage -> Maybe Int
gram_amount         = Maybe Int
gram_amount_
    , send_mode :: TonWalletMessage -> Maybe Int
send_mode           = Maybe Int
send_mode_
    , body :: TonWalletMessage -> Maybe ByteString
body                = Maybe ByteString
body_
    , initial_state :: TonWalletMessage -> Maybe TonSmartContractInitialState
initial_state       = Maybe TonSmartContractInitialState
initial_state_
    }
      = String
"TonWalletMessage"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"destination_address" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
destination_address_
        , String
"gram_amount"         String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
gram_amount_
        , String
"send_mode"           String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
send_mode_
        , String
"body"                String -> Maybe ByteString -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe ByteString
body_
        , String
"initial_state"       String -> Maybe TonSmartContractInitialState -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe TonSmartContractInitialState
initial_state_
        ]

instance AT.FromJSON TonWalletMessage where
  parseJSON :: Value -> Parser TonWalletMessage
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
"tonWalletMessage" -> Value -> Parser TonWalletMessage
parseTonWalletMessage Value
v
      String
_                  -> Parser TonWalletMessage
forall a. Monoid a => a
mempty
    
    where
      parseTonWalletMessage :: A.Value -> AT.Parser TonWalletMessage
      parseTonWalletMessage :: Value -> Parser TonWalletMessage
parseTonWalletMessage = String
-> (Object -> Parser TonWalletMessage)
-> Value
-> Parser TonWalletMessage
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TonWalletMessage" ((Object -> Parser TonWalletMessage)
 -> Value -> Parser TonWalletMessage)
-> (Object -> Parser TonWalletMessage)
-> Value
-> Parser TonWalletMessage
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
destination_address_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"destination_address"
        Maybe Int
gram_amount_         <- (String -> Int) -> Maybe String -> Maybe Int
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Int
I.readInt64 (Maybe String -> Maybe Int)
-> Parser (Maybe String) -> Parser (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser (Maybe String)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"gram_amount"
        Maybe Int
send_mode_           <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"send_mode"
        Maybe ByteString
body_                <- (String -> ByteString) -> Maybe String -> Maybe ByteString
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> ByteString
I.readBytes (Maybe String -> Maybe ByteString)
-> Parser (Maybe String) -> Parser (Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser (Maybe String)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"body"
        Maybe TonSmartContractInitialState
initial_state_       <- Object
o Object -> Key -> Parser (Maybe TonSmartContractInitialState)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"initial_state"
        TonWalletMessage -> Parser TonWalletMessage
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TonWalletMessage -> Parser TonWalletMessage)
-> TonWalletMessage -> Parser TonWalletMessage
forall a b. (a -> b) -> a -> b
$ TonWalletMessage
          { destination_address :: Maybe Text
destination_address = Maybe Text
destination_address_
          , gram_amount :: Maybe Int
gram_amount         = Maybe Int
gram_amount_
          , send_mode :: Maybe Int
send_mode           = Maybe Int
send_mode_
          , body :: Maybe ByteString
body                = Maybe ByteString
body_
          , initial_state :: Maybe TonSmartContractInitialState
initial_state       = Maybe TonSmartContractInitialState
initial_state_
          }
  parseJSON Value
_ = Parser TonWalletMessage
forall a. Monoid a => a
mempty