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

data TonTransactions
  = TonTransactions -- ^ Represents a list of Toncoin transactions
    { TonTransactions -> Maybe Int
ton_amount   :: Maybe Int                             -- ^ The total amount of owned Toncoins
    , TonTransactions -> Maybe [TonTransaction]
transactions :: Maybe [TonTransaction.TonTransaction] -- ^ List of Toncoin transactions
    , TonTransactions -> Maybe Text
next_offset  :: Maybe T.Text                          -- ^ The offset for the next request. If empty, then there are no more results
    }
  deriving (TonTransactions -> TonTransactions -> Bool
(TonTransactions -> TonTransactions -> Bool)
-> (TonTransactions -> TonTransactions -> Bool)
-> Eq TonTransactions
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TonTransactions -> TonTransactions -> Bool
== :: TonTransactions -> TonTransactions -> Bool
$c/= :: TonTransactions -> TonTransactions -> Bool
/= :: TonTransactions -> TonTransactions -> Bool
Eq, Int -> TonTransactions -> ShowS
[TonTransactions] -> ShowS
TonTransactions -> String
(Int -> TonTransactions -> ShowS)
-> (TonTransactions -> String)
-> ([TonTransactions] -> ShowS)
-> Show TonTransactions
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TonTransactions -> ShowS
showsPrec :: Int -> TonTransactions -> ShowS
$cshow :: TonTransactions -> String
show :: TonTransactions -> String
$cshowList :: [TonTransactions] -> ShowS
showList :: [TonTransactions] -> ShowS
Show)

instance I.ShortShow TonTransactions where
  shortShow :: TonTransactions -> String
shortShow TonTransactions
    { ton_amount :: TonTransactions -> Maybe Int
ton_amount   = Maybe Int
ton_amount_
    , transactions :: TonTransactions -> Maybe [TonTransaction]
transactions = Maybe [TonTransaction]
transactions_
    , next_offset :: TonTransactions -> Maybe Text
next_offset  = Maybe Text
next_offset_
    }
      = String
"TonTransactions"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"ton_amount"   String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
ton_amount_
        , String
"transactions" String -> Maybe [TonTransaction] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [TonTransaction]
transactions_
        , String
"next_offset"  String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
next_offset_
        ]

instance AT.FromJSON TonTransactions where
  parseJSON :: Value -> Parser TonTransactions
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
"tonTransactions" -> Value -> Parser TonTransactions
parseTonTransactions Value
v
      String
_                 -> Parser TonTransactions
forall a. Monoid a => a
mempty
    
    where
      parseTonTransactions :: A.Value -> AT.Parser TonTransactions
      parseTonTransactions :: Value -> Parser TonTransactions
parseTonTransactions = String
-> (Object -> Parser TonTransactions)
-> Value
-> Parser TonTransactions
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TonTransactions" ((Object -> Parser TonTransactions)
 -> Value -> Parser TonTransactions)
-> (Object -> Parser TonTransactions)
-> Value
-> Parser TonTransactions
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        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 [TonTransaction]
transactions_ <- Object
o Object -> Key -> Parser (Maybe [TonTransaction])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"transactions"
        Maybe Text
next_offset_  <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"next_offset"
        TonTransactions -> Parser TonTransactions
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TonTransactions -> Parser TonTransactions)
-> TonTransactions -> Parser TonTransactions
forall a b. (a -> b) -> a -> b
$ TonTransactions
          { ton_amount :: Maybe Int
ton_amount   = Maybe Int
ton_amount_
          , transactions :: Maybe [TonTransaction]
transactions = Maybe [TonTransaction]
transactions_
          , next_offset :: Maybe Text
next_offset  = Maybe Text
next_offset_
          }
  parseJSON Value
_ = Parser TonTransactions
forall a. Monoid a => a
mempty