module TD.Data.StarTransaction
  (StarTransaction(..)) 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.StarAmount as StarAmount
import qualified TD.Data.StarTransactionType as StarTransactionType

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

instance I.ShortShow StarTransaction where
  shortShow :: StarTransaction -> String
shortShow StarTransaction
    { _id :: StarTransaction -> Maybe Text
_id         = Maybe Text
_id_
    , star_amount :: StarTransaction -> Maybe StarAmount
star_amount = Maybe StarAmount
star_amount_
    , is_refund :: StarTransaction -> Maybe Bool
is_refund   = Maybe Bool
is_refund_
    , date :: StarTransaction -> Maybe Int
date        = Maybe Int
date_
    , _type :: StarTransaction -> Maybe StarTransactionType
_type       = Maybe StarTransactionType
_type_
    }
      = String
"StarTransaction"
        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
"star_amount" String -> Maybe StarAmount -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe StarAmount
star_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 StarTransactionType -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe StarTransactionType
_type_
        ]

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