module TD.Data.TonWalletTransaction
  (TonWalletTransaction(..)) 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 TD.Data.TonWalletTransactionState as TonWalletTransactionState
import qualified TD.Data.TonWalletTransactionType as TonWalletTransactionType

data TonWalletTransaction
  = TonWalletTransaction -- ^ Represents a transaction with the TON wallet of the current user
    { TonWalletTransaction -> Maybe Text
_id          :: Maybe T.Text                                              -- ^ Unique identifier of the transaction
    , TonWalletTransaction -> Maybe Text
peer_address :: Maybe T.Text                                              -- ^ Address in the TON blockchain of the transaction peer; may be empty if the transaction isn't a wallet transfer
    , TonWalletTransaction -> Maybe Int
peer_user_id :: Maybe Int                                                 -- ^ Identifier of the other user who participated in the transaction; 0 if unknown
    , TonWalletTransaction -> Maybe Text
peer_domain  :: Maybe T.Text                                              -- ^ Identifier of the domain assigned to the peer address; may be empty if none
    , TonWalletTransaction -> Maybe Int
date         :: Maybe Int                                                 -- ^ Point in time (Unix timestamp) when the transaction was completed
    , TonWalletTransaction -> Maybe Int
fee_amount   :: Maybe Int                                                 -- ^ The fee amount paid for the transaction in TON Grams, in the smallest units of the cryptocurrency
    , TonWalletTransaction -> Maybe TonWalletTransactionState
state        :: Maybe TonWalletTransactionState.TonWalletTransactionState -- ^ State of the transaction
    , TonWalletTransaction -> Maybe TonWalletTransactionType
_type        :: Maybe TonWalletTransactionType.TonWalletTransactionType   -- ^ Type of the transaction
    }
  deriving (TonWalletTransaction -> TonWalletTransaction -> Bool
(TonWalletTransaction -> TonWalletTransaction -> Bool)
-> (TonWalletTransaction -> TonWalletTransaction -> Bool)
-> Eq TonWalletTransaction
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TonWalletTransaction -> TonWalletTransaction -> Bool
== :: TonWalletTransaction -> TonWalletTransaction -> Bool
$c/= :: TonWalletTransaction -> TonWalletTransaction -> Bool
/= :: TonWalletTransaction -> TonWalletTransaction -> Bool
Eq, Int -> TonWalletTransaction -> ShowS
[TonWalletTransaction] -> ShowS
TonWalletTransaction -> String
(Int -> TonWalletTransaction -> ShowS)
-> (TonWalletTransaction -> String)
-> ([TonWalletTransaction] -> ShowS)
-> Show TonWalletTransaction
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TonWalletTransaction -> ShowS
showsPrec :: Int -> TonWalletTransaction -> ShowS
$cshow :: TonWalletTransaction -> String
show :: TonWalletTransaction -> String
$cshowList :: [TonWalletTransaction] -> ShowS
showList :: [TonWalletTransaction] -> ShowS
Show)

instance I.ShortShow TonWalletTransaction where
  shortShow :: TonWalletTransaction -> String
shortShow TonWalletTransaction
    { _id :: TonWalletTransaction -> Maybe Text
_id          = Maybe Text
_id_
    , peer_address :: TonWalletTransaction -> Maybe Text
peer_address = Maybe Text
peer_address_
    , peer_user_id :: TonWalletTransaction -> Maybe Int
peer_user_id = Maybe Int
peer_user_id_
    , peer_domain :: TonWalletTransaction -> Maybe Text
peer_domain  = Maybe Text
peer_domain_
    , date :: TonWalletTransaction -> Maybe Int
date         = Maybe Int
date_
    , fee_amount :: TonWalletTransaction -> Maybe Int
fee_amount   = Maybe Int
fee_amount_
    , state :: TonWalletTransaction -> Maybe TonWalletTransactionState
state        = Maybe TonWalletTransactionState
state_
    , _type :: TonWalletTransaction -> Maybe TonWalletTransactionType
_type        = Maybe TonWalletTransactionType
_type_
    }
      = String
"TonWalletTransaction"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_id"          String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
_id_
        , String
"peer_address" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
peer_address_
        , String
"peer_user_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
peer_user_id_
        , String
"peer_domain"  String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
peer_domain_
        , String
"date"         String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
date_
        , String
"fee_amount"   String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
fee_amount_
        , String
"state"        String -> Maybe TonWalletTransactionState -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe TonWalletTransactionState
state_
        , String
"_type"        String -> Maybe TonWalletTransactionType -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe TonWalletTransactionType
_type_
        ]

instance AT.FromJSON TonWalletTransaction where
  parseJSON :: Value -> Parser TonWalletTransaction
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
"tonWalletTransaction" -> Value -> Parser TonWalletTransaction
parseTonWalletTransaction Value
v
      String
_                      -> Parser TonWalletTransaction
forall a. Monoid a => a
mempty
    
    where
      parseTonWalletTransaction :: A.Value -> AT.Parser TonWalletTransaction
      parseTonWalletTransaction :: Value -> Parser TonWalletTransaction
parseTonWalletTransaction = String
-> (Object -> Parser TonWalletTransaction)
-> Value
-> Parser TonWalletTransaction
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TonWalletTransaction" ((Object -> Parser TonWalletTransaction)
 -> Value -> Parser TonWalletTransaction)
-> (Object -> Parser TonWalletTransaction)
-> Value
-> Parser TonWalletTransaction
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
_id_          <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"id"
        Maybe Text
peer_address_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"peer_address"
        Maybe Int
peer_user_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"peer_user_id"
        Maybe Text
peer_domain_  <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"peer_domain"
        Maybe Int
date_         <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"date"
        Maybe Int
fee_amount_   <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"fee_amount"
        Maybe TonWalletTransactionState
state_        <- Object
o Object -> Key -> Parser (Maybe TonWalletTransactionState)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"state"
        Maybe TonWalletTransactionType
_type_        <- Object
o Object -> Key -> Parser (Maybe TonWalletTransactionType)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"type"
        TonWalletTransaction -> Parser TonWalletTransaction
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TonWalletTransaction -> Parser TonWalletTransaction)
-> TonWalletTransaction -> Parser TonWalletTransaction
forall a b. (a -> b) -> a -> b
$ TonWalletTransaction
          { _id :: Maybe Text
_id          = Maybe Text
_id_
          , peer_address :: Maybe Text
peer_address = Maybe Text
peer_address_
          , peer_user_id :: Maybe Int
peer_user_id = Maybe Int
peer_user_id_
          , peer_domain :: Maybe Text
peer_domain  = Maybe Text
peer_domain_
          , date :: Maybe Int
date         = Maybe Int
date_
          , fee_amount :: Maybe Int
fee_amount   = Maybe Int
fee_amount_
          , state :: Maybe TonWalletTransactionState
state        = Maybe TonWalletTransactionState
state_
          , _type :: Maybe TonWalletTransactionType
_type        = Maybe TonWalletTransactionType
_type_
          }
  parseJSON Value
_ = Parser TonWalletTransaction
forall a. Monoid a => a
mempty