module TD.Data.GiftOnChainSaleTransaction
  (GiftOnChainSaleTransaction(..)) 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.TonWalletMessage as TonWalletMessage

-- | Describes a transaction that must be sent from the TON wallet of the current user to buy an upgraded gift on-chain, put it up for sale, change its price, or cancel its sale
data GiftOnChainSaleTransaction
  = GiftOnChainSaleTransactionFragment -- ^ The transaction for a gift sold on-chain on Fragment. The transaction consists of one message, which must be sent to nft_address with gram_amount attached and with payload as the message body. Use sendTonWalletTransaction to send the transaction
    { GiftOnChainSaleTransaction -> Maybe Text
nft_address                   :: Maybe T.Text        -- ^ Address on the TON blockchain to which the transaction must be sent
    , GiftOnChainSaleTransaction -> Maybe ByteString
payload                       :: Maybe BS.ByteString -- ^ Body of the transaction message; may be empty
    , GiftOnChainSaleTransaction -> Maybe Int
gram_amount                   :: Maybe Int           -- ^ The amount that must be sent with the transaction, in the smallest units of TON Grams
    , GiftOnChainSaleTransaction -> Maybe Bool
is_gasless_transfer_available :: Maybe Bool          -- ^ True, if the transaction can be sent as a gasless transfer
    }
  | GiftOnChainSaleTransactionFixedPrice -- ^ The transaction for a gift sold on-chain using a fixed-price sale contract. All messages must be signed and sent in one transfer in the specified order. Use sendTonWalletTransaction with regular_transaction_data and gasless_transaction_data created from messages and gasless_messages respectively to send the transaction
    { GiftOnChainSaleTransaction -> Maybe Text
sale_address     :: Maybe T.Text                              -- ^ Address of the sale smart contract on the TON blockchain
    , GiftOnChainSaleTransaction -> Maybe [TonWalletMessage]
messages         :: Maybe [TonWalletMessage.TonWalletMessage] -- ^ The messages that must be sent for a regular transfer
    , GiftOnChainSaleTransaction -> Maybe [TonWalletMessage]
gasless_messages :: Maybe [TonWalletMessage.TonWalletMessage] -- ^ The messages that must be sent for a gasless transfer instead of messages; empty if gasless transfer isn't available
    }
  deriving (GiftOnChainSaleTransaction -> GiftOnChainSaleTransaction -> Bool
(GiftOnChainSaleTransaction -> GiftOnChainSaleTransaction -> Bool)
-> (GiftOnChainSaleTransaction
    -> GiftOnChainSaleTransaction -> Bool)
-> Eq GiftOnChainSaleTransaction
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GiftOnChainSaleTransaction -> GiftOnChainSaleTransaction -> Bool
== :: GiftOnChainSaleTransaction -> GiftOnChainSaleTransaction -> Bool
$c/= :: GiftOnChainSaleTransaction -> GiftOnChainSaleTransaction -> Bool
/= :: GiftOnChainSaleTransaction -> GiftOnChainSaleTransaction -> Bool
Eq, Int -> GiftOnChainSaleTransaction -> ShowS
[GiftOnChainSaleTransaction] -> ShowS
GiftOnChainSaleTransaction -> String
(Int -> GiftOnChainSaleTransaction -> ShowS)
-> (GiftOnChainSaleTransaction -> String)
-> ([GiftOnChainSaleTransaction] -> ShowS)
-> Show GiftOnChainSaleTransaction
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GiftOnChainSaleTransaction -> ShowS
showsPrec :: Int -> GiftOnChainSaleTransaction -> ShowS
$cshow :: GiftOnChainSaleTransaction -> String
show :: GiftOnChainSaleTransaction -> String
$cshowList :: [GiftOnChainSaleTransaction] -> ShowS
showList :: [GiftOnChainSaleTransaction] -> ShowS
Show)

instance I.ShortShow GiftOnChainSaleTransaction where
  shortShow :: GiftOnChainSaleTransaction -> String
shortShow GiftOnChainSaleTransactionFragment
    { nft_address :: GiftOnChainSaleTransaction -> Maybe Text
nft_address                   = Maybe Text
nft_address_
    , payload :: GiftOnChainSaleTransaction -> Maybe ByteString
payload                       = Maybe ByteString
payload_
    , gram_amount :: GiftOnChainSaleTransaction -> Maybe Int
gram_amount                   = Maybe Int
gram_amount_
    , is_gasless_transfer_available :: GiftOnChainSaleTransaction -> Maybe Bool
is_gasless_transfer_available = Maybe Bool
is_gasless_transfer_available_
    }
      = String
"GiftOnChainSaleTransactionFragment"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"nft_address"                   String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
nft_address_
        , String
"payload"                       String -> Maybe ByteString -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe ByteString
payload_
        , String
"gram_amount"                   String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
gram_amount_
        , String
"is_gasless_transfer_available" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_gasless_transfer_available_
        ]
  shortShow GiftOnChainSaleTransactionFixedPrice
    { sale_address :: GiftOnChainSaleTransaction -> Maybe Text
sale_address     = Maybe Text
sale_address_
    , messages :: GiftOnChainSaleTransaction -> Maybe [TonWalletMessage]
messages         = Maybe [TonWalletMessage]
messages_
    , gasless_messages :: GiftOnChainSaleTransaction -> Maybe [TonWalletMessage]
gasless_messages = Maybe [TonWalletMessage]
gasless_messages_
    }
      = String
"GiftOnChainSaleTransactionFixedPrice"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"sale_address"     String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
sale_address_
        , String
"messages"         String -> Maybe [TonWalletMessage] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [TonWalletMessage]
messages_
        , String
"gasless_messages" String -> Maybe [TonWalletMessage] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [TonWalletMessage]
gasless_messages_
        ]

instance AT.FromJSON GiftOnChainSaleTransaction where
  parseJSON :: Value -> Parser GiftOnChainSaleTransaction
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
"giftOnChainSaleTransactionFragment"   -> Value -> Parser GiftOnChainSaleTransaction
parseGiftOnChainSaleTransactionFragment Value
v
      String
"giftOnChainSaleTransactionFixedPrice" -> Value -> Parser GiftOnChainSaleTransaction
parseGiftOnChainSaleTransactionFixedPrice Value
v
      String
_                                      -> Parser GiftOnChainSaleTransaction
forall a. Monoid a => a
mempty
    
    where
      parseGiftOnChainSaleTransactionFragment :: A.Value -> AT.Parser GiftOnChainSaleTransaction
      parseGiftOnChainSaleTransactionFragment :: Value -> Parser GiftOnChainSaleTransaction
parseGiftOnChainSaleTransactionFragment = String
-> (Object -> Parser GiftOnChainSaleTransaction)
-> Value
-> Parser GiftOnChainSaleTransaction
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"GiftOnChainSaleTransactionFragment" ((Object -> Parser GiftOnChainSaleTransaction)
 -> Value -> Parser GiftOnChainSaleTransaction)
-> (Object -> Parser GiftOnChainSaleTransaction)
-> Value
-> Parser GiftOnChainSaleTransaction
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
nft_address_                   <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"nft_address"
        Maybe ByteString
payload_                       <- (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
"payload"
        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 Bool
is_gasless_transfer_available_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"is_gasless_transfer_available"
        GiftOnChainSaleTransaction -> Parser GiftOnChainSaleTransaction
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (GiftOnChainSaleTransaction -> Parser GiftOnChainSaleTransaction)
-> GiftOnChainSaleTransaction -> Parser GiftOnChainSaleTransaction
forall a b. (a -> b) -> a -> b
$ GiftOnChainSaleTransactionFragment
          { nft_address :: Maybe Text
nft_address                   = Maybe Text
nft_address_
          , payload :: Maybe ByteString
payload                       = Maybe ByteString
payload_
          , gram_amount :: Maybe Int
gram_amount                   = Maybe Int
gram_amount_
          , is_gasless_transfer_available :: Maybe Bool
is_gasless_transfer_available = Maybe Bool
is_gasless_transfer_available_
          }
      parseGiftOnChainSaleTransactionFixedPrice :: A.Value -> AT.Parser GiftOnChainSaleTransaction
      parseGiftOnChainSaleTransactionFixedPrice :: Value -> Parser GiftOnChainSaleTransaction
parseGiftOnChainSaleTransactionFixedPrice = String
-> (Object -> Parser GiftOnChainSaleTransaction)
-> Value
-> Parser GiftOnChainSaleTransaction
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"GiftOnChainSaleTransactionFixedPrice" ((Object -> Parser GiftOnChainSaleTransaction)
 -> Value -> Parser GiftOnChainSaleTransaction)
-> (Object -> Parser GiftOnChainSaleTransaction)
-> Value
-> Parser GiftOnChainSaleTransaction
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
sale_address_     <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"sale_address"
        Maybe [TonWalletMessage]
messages_         <- Object
o Object -> Key -> Parser (Maybe [TonWalletMessage])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"messages"
        Maybe [TonWalletMessage]
gasless_messages_ <- Object
o Object -> Key -> Parser (Maybe [TonWalletMessage])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"gasless_messages"
        GiftOnChainSaleTransaction -> Parser GiftOnChainSaleTransaction
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (GiftOnChainSaleTransaction -> Parser GiftOnChainSaleTransaction)
-> GiftOnChainSaleTransaction -> Parser GiftOnChainSaleTransaction
forall a b. (a -> b) -> a -> b
$ GiftOnChainSaleTransactionFixedPrice
          { sale_address :: Maybe Text
sale_address     = Maybe Text
sale_address_
          , messages :: Maybe [TonWalletMessage]
messages         = Maybe [TonWalletMessage]
messages_
          , gasless_messages :: Maybe [TonWalletMessage]
gasless_messages = Maybe [TonWalletMessage]
gasless_messages_
          }
  parseJSON Value
_ = Parser GiftOnChainSaleTransaction
forall a. Monoid a => a
mempty