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.StarTransactionPartner as StarTransactionPartner

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 Int
star_count :: Maybe Int                                           -- ^ 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 StarTransactionPartner
partner    :: Maybe StarTransactionPartner.StarTransactionPartner -- ^ Source of the incoming transaction, or its recipient for outgoing transactions
    }
  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_count :: StarTransaction -> Maybe Int
star_count = Maybe Int
star_count_
    , is_refund :: StarTransaction -> Maybe Bool
is_refund  = Maybe Bool
is_refund_
    , date :: StarTransaction -> Maybe Int
date       = Maybe Int
date_
    , partner :: StarTransaction -> Maybe StarTransactionPartner
partner    = Maybe StarTransactionPartner
partner_
    }
      = 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_count" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
star_count_
        , 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
"partner"    String -> Maybe StarTransactionPartner -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe StarTransactionPartner
partner_
        ]

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 Int
star_count_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"star_count"
        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 StarTransactionPartner
partner_    <- Object
o Object -> Key -> Parser (Maybe StarTransactionPartner)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"partner"
        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_count :: Maybe Int
star_count = Maybe Int
star_count_
          , is_refund :: Maybe Bool
is_refund  = Maybe Bool
is_refund_
          , date :: Maybe Int
date       = Maybe Int
date_
          , partner :: Maybe StarTransactionPartner
partner    = Maybe StarTransactionPartner
partner_
          }
  parseJSON Value
_ = Parser StarTransaction
forall a. Monoid a => a
mempty