module TD.Data.TonTransactionType
  (TonTransactionType(..)) 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.Sticker as Sticker
import qualified TD.Data.RevenueWithdrawalState as RevenueWithdrawalState
import qualified TD.Data.UpgradedGift as UpgradedGift

-- | Describes type of transaction with Toncoins
data TonTransactionType
  = TonTransactionTypeFragmentDeposit -- ^ The transaction is a deposit of Toncoins from Fragment
    { TonTransactionType -> Maybe Bool
is_gift :: Maybe Bool            -- ^ True, if the transaction is a gift from another user
    , TonTransactionType -> Maybe Sticker
sticker :: Maybe Sticker.Sticker -- ^ The sticker to be shown in the transaction information; may be null if unknown
    }
  | TonTransactionTypeFragmentWithdrawal -- ^ The transaction is a withdrawal of earned Toncoins to Fragment
    { TonTransactionType -> Maybe RevenueWithdrawalState
withdrawal_state :: Maybe RevenueWithdrawalState.RevenueWithdrawalState -- ^ State of the withdrawal; may be null for refunds from Fragment
    }
  | TonTransactionTypeSuggestedPostPayment -- ^ The transaction is a payment for a suggested post
    { TonTransactionType -> Maybe Int
chat_id :: Maybe Int -- ^ Identifier of the channel chat that posted the post
    }
  | TonTransactionTypeGiftPurchaseOffer -- ^ The transaction is an offer of gift purchase
    { TonTransactionType -> Maybe UpgradedGift
gift :: Maybe UpgradedGift.UpgradedGift -- ^ The gift
    }
  | TonTransactionTypeUpgradedGiftPurchase -- ^ The transaction is a purchase of an upgraded gift for some user or channel
    { TonTransactionType -> Maybe Int
user_id :: Maybe Int                       -- ^ Identifier of the user who sold the gift
    , gift    :: Maybe UpgradedGift.UpgradedGift -- ^ The gift
    }
  | TonTransactionTypeUpgradedGiftSale -- ^ The transaction is a sale of an upgraded gift
    { user_id                   :: Maybe Int                       -- ^ Identifier of the user who bought the gift
    , gift                      :: Maybe UpgradedGift.UpgradedGift -- ^ The gift
    , TonTransactionType -> Maybe Int
commission_per_mille      :: Maybe Int                       -- ^ The number of Toncoins received by the Telegram for each 1000 Toncoins received by the seller of the gift
    , TonTransactionType -> Maybe Int
commission_toncoin_amount :: Maybe Int                       -- ^ The Toncoin amount that was received by the Telegram; in the smallest units of the currency
    , TonTransactionType -> Maybe Bool
via_offer                 :: Maybe Bool                      -- ^ True, if the gift was sold through a purchase offer
    }
  | TonTransactionTypeStakeDiceStake -- ^ The transaction is a payment for stake dice throw
  | TonTransactionTypeStakeDicePayout -- ^ The transaction is a payment for successful stake dice throw
  | TonTransactionTypeUnsupported -- ^ The transaction is a transaction of an unsupported type
  deriving (TonTransactionType -> TonTransactionType -> Bool
(TonTransactionType -> TonTransactionType -> Bool)
-> (TonTransactionType -> TonTransactionType -> Bool)
-> Eq TonTransactionType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TonTransactionType -> TonTransactionType -> Bool
== :: TonTransactionType -> TonTransactionType -> Bool
$c/= :: TonTransactionType -> TonTransactionType -> Bool
/= :: TonTransactionType -> TonTransactionType -> Bool
Eq, Int -> TonTransactionType -> ShowS
[TonTransactionType] -> ShowS
TonTransactionType -> String
(Int -> TonTransactionType -> ShowS)
-> (TonTransactionType -> String)
-> ([TonTransactionType] -> ShowS)
-> Show TonTransactionType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TonTransactionType -> ShowS
showsPrec :: Int -> TonTransactionType -> ShowS
$cshow :: TonTransactionType -> String
show :: TonTransactionType -> String
$cshowList :: [TonTransactionType] -> ShowS
showList :: [TonTransactionType] -> ShowS
Show)

instance I.ShortShow TonTransactionType where
  shortShow :: TonTransactionType -> String
shortShow TonTransactionTypeFragmentDeposit
    { is_gift :: TonTransactionType -> Maybe Bool
is_gift = Maybe Bool
is_gift_
    , sticker :: TonTransactionType -> Maybe Sticker
sticker = Maybe Sticker
sticker_
    }
      = String
"TonTransactionTypeFragmentDeposit"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"is_gift" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_gift_
        , String
"sticker" String -> Maybe Sticker -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Sticker
sticker_
        ]
  shortShow TonTransactionTypeFragmentWithdrawal
    { withdrawal_state :: TonTransactionType -> Maybe RevenueWithdrawalState
withdrawal_state = Maybe RevenueWithdrawalState
withdrawal_state_
    }
      = String
"TonTransactionTypeFragmentWithdrawal"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"withdrawal_state" String -> Maybe RevenueWithdrawalState -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe RevenueWithdrawalState
withdrawal_state_
        ]
  shortShow TonTransactionTypeSuggestedPostPayment
    { chat_id :: TonTransactionType -> Maybe Int
chat_id = Maybe Int
chat_id_
    }
      = String
"TonTransactionTypeSuggestedPostPayment"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"chat_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
chat_id_
        ]
  shortShow TonTransactionTypeGiftPurchaseOffer
    { gift :: TonTransactionType -> Maybe UpgradedGift
gift = Maybe UpgradedGift
gift_
    }
      = String
"TonTransactionTypeGiftPurchaseOffer"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"gift" String -> Maybe UpgradedGift -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe UpgradedGift
gift_
        ]
  shortShow TonTransactionTypeUpgradedGiftPurchase
    { user_id :: TonTransactionType -> Maybe Int
user_id = Maybe Int
user_id_
    , gift :: TonTransactionType -> Maybe UpgradedGift
gift    = Maybe UpgradedGift
gift_
    }
      = String
"TonTransactionTypeUpgradedGiftPurchase"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"user_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
user_id_
        , String
"gift"    String -> Maybe UpgradedGift -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe UpgradedGift
gift_
        ]
  shortShow TonTransactionTypeUpgradedGiftSale
    { user_id :: TonTransactionType -> Maybe Int
user_id                   = Maybe Int
user_id_
    , gift :: TonTransactionType -> Maybe UpgradedGift
gift                      = Maybe UpgradedGift
gift_
    , commission_per_mille :: TonTransactionType -> Maybe Int
commission_per_mille      = Maybe Int
commission_per_mille_
    , commission_toncoin_amount :: TonTransactionType -> Maybe Int
commission_toncoin_amount = Maybe Int
commission_toncoin_amount_
    , via_offer :: TonTransactionType -> Maybe Bool
via_offer                 = Maybe Bool
via_offer_
    }
      = String
"TonTransactionTypeUpgradedGiftSale"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"user_id"                   String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
user_id_
        , String
"gift"                      String -> Maybe UpgradedGift -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe UpgradedGift
gift_
        , String
"commission_per_mille"      String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
commission_per_mille_
        , String
"commission_toncoin_amount" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
commission_toncoin_amount_
        , String
"via_offer"                 String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
via_offer_
        ]
  shortShow TonTransactionType
TonTransactionTypeStakeDiceStake
      = String
"TonTransactionTypeStakeDiceStake"
  shortShow TonTransactionType
TonTransactionTypeStakeDicePayout
      = String
"TonTransactionTypeStakeDicePayout"
  shortShow TonTransactionType
TonTransactionTypeUnsupported
      = String
"TonTransactionTypeUnsupported"

instance AT.FromJSON TonTransactionType where
  parseJSON :: Value -> Parser TonTransactionType
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
"tonTransactionTypeFragmentDeposit"      -> Value -> Parser TonTransactionType
parseTonTransactionTypeFragmentDeposit Value
v
      String
"tonTransactionTypeFragmentWithdrawal"   -> Value -> Parser TonTransactionType
parseTonTransactionTypeFragmentWithdrawal Value
v
      String
"tonTransactionTypeSuggestedPostPayment" -> Value -> Parser TonTransactionType
parseTonTransactionTypeSuggestedPostPayment Value
v
      String
"tonTransactionTypeGiftPurchaseOffer"    -> Value -> Parser TonTransactionType
parseTonTransactionTypeGiftPurchaseOffer Value
v
      String
"tonTransactionTypeUpgradedGiftPurchase" -> Value -> Parser TonTransactionType
parseTonTransactionTypeUpgradedGiftPurchase Value
v
      String
"tonTransactionTypeUpgradedGiftSale"     -> Value -> Parser TonTransactionType
parseTonTransactionTypeUpgradedGiftSale Value
v
      String
"tonTransactionTypeStakeDiceStake"       -> TonTransactionType -> Parser TonTransactionType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TonTransactionType
TonTransactionTypeStakeDiceStake
      String
"tonTransactionTypeStakeDicePayout"      -> TonTransactionType -> Parser TonTransactionType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TonTransactionType
TonTransactionTypeStakeDicePayout
      String
"tonTransactionTypeUnsupported"          -> TonTransactionType -> Parser TonTransactionType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TonTransactionType
TonTransactionTypeUnsupported
      String
_                                        -> Parser TonTransactionType
forall a. Monoid a => a
mempty
    
    where
      parseTonTransactionTypeFragmentDeposit :: A.Value -> AT.Parser TonTransactionType
      parseTonTransactionTypeFragmentDeposit :: Value -> Parser TonTransactionType
parseTonTransactionTypeFragmentDeposit = String
-> (Object -> Parser TonTransactionType)
-> Value
-> Parser TonTransactionType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TonTransactionTypeFragmentDeposit" ((Object -> Parser TonTransactionType)
 -> Value -> Parser TonTransactionType)
-> (Object -> Parser TonTransactionType)
-> Value
-> Parser TonTransactionType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Bool
is_gift_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_gift"
        Maybe Sticker
sticker_ <- Object
o Object -> Key -> Parser (Maybe Sticker)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"sticker"
        TonTransactionType -> Parser TonTransactionType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TonTransactionType -> Parser TonTransactionType)
-> TonTransactionType -> Parser TonTransactionType
forall a b. (a -> b) -> a -> b
$ TonTransactionTypeFragmentDeposit
          { is_gift :: Maybe Bool
is_gift = Maybe Bool
is_gift_
          , sticker :: Maybe Sticker
sticker = Maybe Sticker
sticker_
          }
      parseTonTransactionTypeFragmentWithdrawal :: A.Value -> AT.Parser TonTransactionType
      parseTonTransactionTypeFragmentWithdrawal :: Value -> Parser TonTransactionType
parseTonTransactionTypeFragmentWithdrawal = String
-> (Object -> Parser TonTransactionType)
-> Value
-> Parser TonTransactionType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TonTransactionTypeFragmentWithdrawal" ((Object -> Parser TonTransactionType)
 -> Value -> Parser TonTransactionType)
-> (Object -> Parser TonTransactionType)
-> Value
-> Parser TonTransactionType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe RevenueWithdrawalState
withdrawal_state_ <- Object
o Object -> Key -> Parser (Maybe RevenueWithdrawalState)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"withdrawal_state"
        TonTransactionType -> Parser TonTransactionType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TonTransactionType -> Parser TonTransactionType)
-> TonTransactionType -> Parser TonTransactionType
forall a b. (a -> b) -> a -> b
$ TonTransactionTypeFragmentWithdrawal
          { withdrawal_state :: Maybe RevenueWithdrawalState
withdrawal_state = Maybe RevenueWithdrawalState
withdrawal_state_
          }
      parseTonTransactionTypeSuggestedPostPayment :: A.Value -> AT.Parser TonTransactionType
      parseTonTransactionTypeSuggestedPostPayment :: Value -> Parser TonTransactionType
parseTonTransactionTypeSuggestedPostPayment = String
-> (Object -> Parser TonTransactionType)
-> Value
-> Parser TonTransactionType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TonTransactionTypeSuggestedPostPayment" ((Object -> Parser TonTransactionType)
 -> Value -> Parser TonTransactionType)
-> (Object -> Parser TonTransactionType)
-> Value
-> Parser TonTransactionType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
chat_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"chat_id"
        TonTransactionType -> Parser TonTransactionType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TonTransactionType -> Parser TonTransactionType)
-> TonTransactionType -> Parser TonTransactionType
forall a b. (a -> b) -> a -> b
$ TonTransactionTypeSuggestedPostPayment
          { chat_id :: Maybe Int
chat_id = Maybe Int
chat_id_
          }
      parseTonTransactionTypeGiftPurchaseOffer :: A.Value -> AT.Parser TonTransactionType
      parseTonTransactionTypeGiftPurchaseOffer :: Value -> Parser TonTransactionType
parseTonTransactionTypeGiftPurchaseOffer = String
-> (Object -> Parser TonTransactionType)
-> Value
-> Parser TonTransactionType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TonTransactionTypeGiftPurchaseOffer" ((Object -> Parser TonTransactionType)
 -> Value -> Parser TonTransactionType)
-> (Object -> Parser TonTransactionType)
-> Value
-> Parser TonTransactionType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe UpgradedGift
gift_ <- Object
o Object -> Key -> Parser (Maybe UpgradedGift)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"gift"
        TonTransactionType -> Parser TonTransactionType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TonTransactionType -> Parser TonTransactionType)
-> TonTransactionType -> Parser TonTransactionType
forall a b. (a -> b) -> a -> b
$ TonTransactionTypeGiftPurchaseOffer
          { gift :: Maybe UpgradedGift
gift = Maybe UpgradedGift
gift_
          }
      parseTonTransactionTypeUpgradedGiftPurchase :: A.Value -> AT.Parser TonTransactionType
      parseTonTransactionTypeUpgradedGiftPurchase :: Value -> Parser TonTransactionType
parseTonTransactionTypeUpgradedGiftPurchase = String
-> (Object -> Parser TonTransactionType)
-> Value
-> Parser TonTransactionType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TonTransactionTypeUpgradedGiftPurchase" ((Object -> Parser TonTransactionType)
 -> Value -> Parser TonTransactionType)
-> (Object -> Parser TonTransactionType)
-> Value
-> Parser TonTransactionType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
user_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"user_id"
        Maybe UpgradedGift
gift_    <- Object
o Object -> Key -> Parser (Maybe UpgradedGift)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"gift"
        TonTransactionType -> Parser TonTransactionType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TonTransactionType -> Parser TonTransactionType)
-> TonTransactionType -> Parser TonTransactionType
forall a b. (a -> b) -> a -> b
$ TonTransactionTypeUpgradedGiftPurchase
          { user_id :: Maybe Int
user_id = Maybe Int
user_id_
          , gift :: Maybe UpgradedGift
gift    = Maybe UpgradedGift
gift_
          }
      parseTonTransactionTypeUpgradedGiftSale :: A.Value -> AT.Parser TonTransactionType
      parseTonTransactionTypeUpgradedGiftSale :: Value -> Parser TonTransactionType
parseTonTransactionTypeUpgradedGiftSale = String
-> (Object -> Parser TonTransactionType)
-> Value
-> Parser TonTransactionType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TonTransactionTypeUpgradedGiftSale" ((Object -> Parser TonTransactionType)
 -> Value -> Parser TonTransactionType)
-> (Object -> Parser TonTransactionType)
-> Value
-> Parser TonTransactionType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
user_id_                   <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"user_id"
        Maybe UpgradedGift
gift_                      <- Object
o Object -> Key -> Parser (Maybe UpgradedGift)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"gift"
        Maybe Int
commission_per_mille_      <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"commission_per_mille"
        Maybe Int
commission_toncoin_amount_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"commission_toncoin_amount"
        Maybe Bool
via_offer_                 <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"via_offer"
        TonTransactionType -> Parser TonTransactionType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TonTransactionType -> Parser TonTransactionType)
-> TonTransactionType -> Parser TonTransactionType
forall a b. (a -> b) -> a -> b
$ TonTransactionTypeUpgradedGiftSale
          { user_id :: Maybe Int
user_id                   = Maybe Int
user_id_
          , gift :: Maybe UpgradedGift
gift                      = Maybe UpgradedGift
gift_
          , commission_per_mille :: Maybe Int
commission_per_mille      = Maybe Int
commission_per_mille_
          , commission_toncoin_amount :: Maybe Int
commission_toncoin_amount = Maybe Int
commission_toncoin_amount_
          , via_offer :: Maybe Bool
via_offer                 = Maybe Bool
via_offer_
          }
  parseJSON Value
_ = Parser TonTransactionType
forall a. Monoid a => a
mempty