module TD.Data.TonTransaction
  (TonTransaction(..)) 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.TonTransactionType as TonTransactionType

data TonTransaction
  = TonTransaction -- ^ Represents a transaction changing the amount of owned Toncoins
    { TonTransaction -> Maybe Text
_id        :: Maybe T.Text                                -- ^ Unique identifier of the transaction
    , TonTransaction -> Maybe Int
ton_amount :: Maybe Int                                   -- ^ The amount of added owned Toncoins; negative for outgoing transactions
    , TonTransaction -> Maybe Bool
is_refund  :: Maybe Bool                                  -- ^ True, if the transaction is a refund of a previous transaction
    , TonTransaction -> Maybe Int
date       :: Maybe Int                                   -- ^ Point in time (Unix timestamp) when the transaction was completed
    , TonTransaction -> Maybe TonTransactionType
_type      :: Maybe TonTransactionType.TonTransactionType -- ^ Type of the transaction
    }
  deriving (TonTransaction -> TonTransaction -> Bool
(TonTransaction -> TonTransaction -> Bool)
-> (TonTransaction -> TonTransaction -> Bool) -> Eq TonTransaction
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TonTransaction -> TonTransaction -> Bool
== :: TonTransaction -> TonTransaction -> Bool
$c/= :: TonTransaction -> TonTransaction -> Bool
/= :: TonTransaction -> TonTransaction -> Bool
Eq, Int -> TonTransaction -> ShowS
[TonTransaction] -> ShowS
TonTransaction -> String
(Int -> TonTransaction -> ShowS)
-> (TonTransaction -> String)
-> ([TonTransaction] -> ShowS)
-> Show TonTransaction
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TonTransaction -> ShowS
showsPrec :: Int -> TonTransaction -> ShowS
$cshow :: TonTransaction -> String
show :: TonTransaction -> String
$cshowList :: [TonTransaction] -> ShowS
showList :: [TonTransaction] -> ShowS
Show)

instance I.ShortShow TonTransaction where
  shortShow :: TonTransaction -> String
shortShow TonTransaction
    { _id :: TonTransaction -> Maybe Text
_id        = Maybe Text
_id_
    , ton_amount :: TonTransaction -> Maybe Int
ton_amount = Maybe Int
ton_amount_
    , is_refund :: TonTransaction -> Maybe Bool
is_refund  = Maybe Bool
is_refund_
    , date :: TonTransaction -> Maybe Int
date       = Maybe Int
date_
    , _type :: TonTransaction -> Maybe TonTransactionType
_type      = Maybe TonTransactionType
_type_
    }
      = String
"TonTransaction"
        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
"ton_amount" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
ton_amount_
        , String
"is_refund"  String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_refund_
        , String
"date"       String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
date_
        , String
"_type"      String -> Maybe TonTransactionType -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe TonTransactionType
_type_
        ]

instance AT.FromJSON TonTransaction where
  parseJSON :: Value -> Parser TonTransaction
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
"tonTransaction" -> Value -> Parser TonTransaction
parseTonTransaction Value
v
      String
_                -> Parser TonTransaction
forall a. Monoid a => a
mempty
    
    where
      parseTonTransaction :: A.Value -> AT.Parser TonTransaction
      parseTonTransaction :: Value -> Parser TonTransaction
parseTonTransaction = String
-> (Object -> Parser TonTransaction)
-> Value
-> Parser TonTransaction
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TonTransaction" ((Object -> Parser TonTransaction)
 -> Value -> Parser TonTransaction)
-> (Object -> Parser TonTransaction)
-> Value
-> Parser TonTransaction
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 Int
ton_amount_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"ton_amount"
        Maybe Bool
is_refund_  <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_refund"
        Maybe Int
date_       <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"date"
        Maybe TonTransactionType
_type_      <- Object
o Object -> Key -> Parser (Maybe TonTransactionType)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"type"
        TonTransaction -> Parser TonTransaction
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TonTransaction -> Parser TonTransaction)
-> TonTransaction -> Parser TonTransaction
forall a b. (a -> b) -> a -> b
$ TonTransaction
          { _id :: Maybe Text
_id        = Maybe Text
_id_
          , ton_amount :: Maybe Int
ton_amount = Maybe Int
ton_amount_
          , is_refund :: Maybe Bool
is_refund  = Maybe Bool
is_refund_
          , date :: Maybe Int
date       = Maybe Int
date_
          , _type :: Maybe TonTransactionType
_type      = Maybe TonTransactionType
_type_
          }
  parseJSON Value
_ = Parser TonTransaction
forall a. Monoid a => a
mempty